从iOS 9上的表视图打开safari视图控制器,在iOS 8或7上的safari中打开

时间:2015-11-25 23:19:21

标签: ios objective-c iphone safari sfsafariviewcontroller

如果用户在iOS 9或更高版本上,我想从safari视图控制器中的表格视图单元格打开我的网站。如果用户使用的是iOS 7或8,则该网站应在标准的Safari应用程序中打开。

这是我目前使用的打开safari的代码。

    case 3: { // Follow us section
        switch (indexPath.row) {
            case 0: { //Website
                NSURL *url = [NSURL URLWithString:@"http://www.scanmarksapp.com"];
                if (![[UIApplication sharedApplication] openURL:url]) {
                    NSLog(@"%@%@",@"Failed to open url:",[url description]);
                }
            }
                break;

            default:
                break;
        }

    }
        break;

我相信这段代码应该用我的网站打开safari视图控制器。但我不确定如何组合这两组代码。

- (void)openLink:(NSString *)url {

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]];
if (URL) {
    SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
    sfvc.delegate = self;
    [self presentViewController:sfvc animated:YES completion:nil];
}

#pragma Safari View Controller Delegate

- (void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller {
[controller dismissViewControllerAnimated:YES completion:nil];
}

我知道这是用于确定iOS版本的代码

if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {

我遵循了你的建议

- (void)openLink:(NSString *)url {

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]];
if (URL) {
    SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
    sfvc.delegate = self;
    [self presentViewController:sfvc animated:YES completion:nil];
} else {
    // will have a nice alert displaying soon.
}

if ([SFSafariViewController class] != nil) {
    // Use SFSafariViewController
} else {
    NSURL *url = [NSURL URLWithString:@"http://www.scanmarksapp.com"];
    if (![[UIApplication sharedApplication] openURL:url]) {
        NSLog(@"%@%@",@"Failed to open url:",[url description]);
    }
}

然后在我的表格视图单元格didSelectRowAtIndexPath

下添加了此代码
        case 3: { // Follow us section
        switch (indexPath.row) {
            case 0: { //Website
                NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]];
                if (URL) {
                    SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
                    sfvc.delegate = self;
                    [self presentViewController:sfvc animated:YES completion:nil];
                } else {
                    // will have a nice alert displaying soon.
                }

                if ([SFSafariViewController class] != nil) {
                    // Use SFSafariViewController
                } else {
                    NSURL *url = [NSURL URLWithString:@"http://www.scanmarksapp.com"];
                    if (![[UIApplication sharedApplication] openURL:url]) {
                        NSLog(@"%@%@",@"Failed to open url:",[url description]);
                    }

                }
            }
                break;

            default:
                break;
        }

    }
        break;

我收到错误&#34;使用未声明的标识符网址&#34;在这行代码上

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.scanmarksapp.com", url]];

在NSStringWithFormat末尾删除url使Safari视图控制器工作。但是在iOS 9.0以下,例如8.4应用程序崩溃。

1 个答案:

答案 0 :(得分:18)

标准和推荐的方法是检查功能,而不是操作系统版本。在这种情况下,您可以检查是否存在SFSafariViewController类。

if ([SFSafariViewController class] != nil) {
    // Use SFSafariViewController
} else {
    // Open in Mobile Safari
}

修改

您对openLink:的实施是错误的。

- (void)openLink:(NSString *)url {
    NSURL *URL = [NSURL URLWithString:url];

    if (URL) {
        if ([SFSafariViewController class] != nil) {
            SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
            sfvc.delegate = self;
            [self presentViewController:sfvc animated:YES completion:nil];
        } else {
            if (![[UIApplication sharedApplication] openURL:url]) {
                NSLog(@"%@%@",@"Failed to open url:",[url description]);
            }
        }
    } else {
        // will have a nice alert displaying soon.
    }
}