4

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]];
    }
}
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Jon Erickson
  • 1,876
  • 4
  • 30
  • 73

2 Answers2

15
NSString *strurl = [NSString stringWithFormat:@"http://%@",strMediaIconUrl];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strurl]];
use http:// must.
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Hardik Mamtora
  • 1,642
  • 17
  • 23
1

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?

Edit:

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]);
        }

    }
}
Anil
  • 2,539
  • 6
  • 33
  • 42
  • Yeah, I have implemented the delegate, let me make urlAddress a string and see if it works. – Jon Erickson Sep 08 '12 at 00:27
  • so I changed it to: NSString *urlAddress = self.url; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlAddress]]; and still does not work. – Jon Erickson Sep 08 '12 at 00:30
  • 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. – Anil Sep 08 '12 at 00:30
  • 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. – Jon Erickson Sep 08 '12 at 00:34
  • So the program is getting there but just won't open Safari. – Jon Erickson Sep 08 '12 at 00:34
  • 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? – Anil Sep 08 '12 at 02:21
  • 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. – Jon Erickson Sep 08 '12 at 02:29
  • 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? – Anil Sep 08 '12 at 02:32
  • [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. – Jon Erickson Sep 08 '12 at 02:37
  • 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. – Anil Sep 08 '12 at 02:48
  • It generated: Failed to open url:(null) – Jon Erickson Sep 08 '12 at 02:58
  • 2
    So I encoded like you said using this: NSString *rawURL = self.url; NSString *urlAddress = [rawURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; and it worked finally! thanks a lot. – Jon Erickson Sep 08 '12 at 03:04
  • 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. – Anil Sep 08 '12 at 03:05
  • I guess I should have figured that considering the URL i was trying to pass had a # and ? in it... – Jon Erickson Sep 08 '12 at 03:09