openURL没有打开Safari

时间:2012-09-08 00:02:28

标签: ios safari uiactionsheet openurl

我正在尝试使用操作表打开带有链接的safari。该变量设置正确并相应地显示链接,但由于某种原因,Safari将无法打开,我无法弄清楚为什么......

以下是代码:

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

2 个答案:

答案 0 :(得分:15)

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

答案 1 :(得分:1)

要在Safari中打开链接,您只需执行以下操作即可。 urlAddressNSString,您可以在需要设置的任何位置设置urlAddress。或者,您可以将@"someString"替换为[[UIApplication sharedApplication] openURL:[NSURL URLWithString: urlAddress]];

UIActionSheetDelegate

另外,您是否检查过您的头文件是否正在实施-(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]); } } } 协议?

编辑:

在通话中尝试以下操作,看看是否有错误:

{{1}}