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]];
}
}
NSString *strurl = [NSString stringWithFormat:@"http://%@",strMediaIconUrl];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strurl]];
use http:// must.
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]);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With