4

我正在尝试使用操作表通过链接打开 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]];
    }
}
4

2 回答 2

15
NSString *strurl = [NSString stringWithFormat:@"http://%@",strMediaIconUrl];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strurl]];
use http:// must.
于 2013-04-08T08:44:35.860 回答
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]);
        }

    }
}
于 2012-09-08T00:19:18.873 回答