Title: openURL not opening Safari
Tags: ios;safari;uiactionsheet;openurl
Question: I am trying to use an action sheet to open safari with a link. The variable is set correct and displays the link accordingly, but for some reason, Safari won't open and I cannot figure out why...
Here's the code:
-(void)actionSheet {
sheet = [[UIActionSheet alloc] initWithTitle:@"Options"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Open in Safari", nil];
[sheet showInView:[UIApplication sharedApplication].keyWindow];
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != -1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.url]];
Comment: If you `NSLog(@"%@",self.url)` what do you get?

Comment: I get the link as it should be.

Comment: self.url is passed from the previous view controller

Comment: Where are you creating "self.url"?

Here is the accepted answer: To open a link in Safari, all you should have to do is the following. urlAddress is a NSString that you set wherever you need it to be set. Alternatively you could replace urlAddress with @"someString".
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: urlAddress]];
Also, have you checked that your header file is implementing the UIActionSheetDelegate protocol?
Try the following in your call to see if an error is generated:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != -1) {
NSUrl *myURL = [NSURL URLWithString:self.url];
if (![[UIApplication sharedApplication] openURL:myURL]) {
NSLog(@"%@%@",@"Failed to open url:",[myURL description]);
}
Comment for this answer: Yeah, I have implemented the delegate, let me make urlAddress a string and see if it works.
Comment for this answer: so I changed it to: NSString *urlAddress = self.url;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlAddress]]; and still does not work.
Comment for this answer: I set a breakpoint on the if statement and it worked when I clicked the button, and the NSLog of self.url is the link as it should be.
Comment for this answer: So the program is getting there but just won't open Safari.
Comment for this answer: Currently I am using the urlAddress which is set by self.url and they both return the full url address correctly. Also, I assume the if statement is true because I put the NSLog at the end of my if statement and it still returns the link when I click on the button in the action sheet. If I set my if statement as anything else, I don't get the NSLog of the url, which makes sense.
Comment for this answer: [UIApplication sharedApplication] returns  and my project has no Issues. It has to be something to do with the variable because when I replace the call openURL with a straight string (@"http://www.google.com"), it works fine but when I use urlAddress, which returns a valid link every time, it won't work.
Comment for this answer: It generated: Failed to open url:(null)
Comment for this answer: So I encoded like you said using this:
NSString *rawURL = self.url;
NSString *urlAddress = [rawURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
and it worked finally! thanks a lot.
Comment for this answer: I guess I should have figured that considering the URL i was trying to pass had a # and ? in it...
Comment for this answer: If you have implemented the protocol, put a NSLog statement inside your actionSheet:clickedButtonAtIndex method call to see if your program is even getting there or set a breakpoint in that method to see if it's getting there.  If it's not getting to that point, then you'll be able to find the problem faster.
Comment for this answer: You stated you set the breakpoint on the if statement, does the condition evaluate to true?  Also, you stated that your NSLog of self.url is the link, but you also stated you changed your openURL call to use a variable urlAddress.  Which one are you using now?
Comment for this answer: What does [UIApplication sharedApplication] return in your function?  Perhaps it's returning nil?  Also, when you compile your code are there any warnings that may be related to this?
Comment for this answer: Just edited my original post, take a look at my addition and try to implement that to see if it generates an error.  Also, if your URL includes spaces or special characters, you may want to encode the string before passing it to the openURL function.
Comment for this answer: There you have it, my hunch is that whatever URL you are passing to NSUrl is probably the cause of failure.  As I mentioned before, encode your URL before creating the NSUrl.

Here is another answer: NSString *strurl = [NSString stringWithFormat:@"http://%@",strMediaIconUrl];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strurl]];
use http:// must.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:09/08/12', 'FavoriteCount': 2.0, 'PostTypeId': 1, 'Tags': '<ios><safari><uiactionsheet><openurl>', 'Title': 'openURL not opening Safari', 'ViewCount': 9752, 'id': 12326840, 'user_AboutMe': None, 'user_AccountId': 1780876.0, 'user_DisplayName': 'Jon Erickson', 'user_Reputation': 1816.0, 'user_Views': 178.0, 'user_id': 1623671.0}}