Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openURL not opening Safari

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]];
    }
}
like image 641
Jon Erickson Avatar asked Dec 05 '22 14:12

Jon Erickson


2 Answers

NSString *strurl = [NSString stringWithFormat:@"http://%@",strMediaIconUrl];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strurl]];
use http:// must.
like image 122
Hardik Mamtora Avatar answered Dec 15 '22 00:12

Hardik Mamtora


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

    }
}
like image 27
Anil Natha Avatar answered Dec 15 '22 00:12

Anil Natha