【问题标题】:Open safari view controller from table view on iOS 9 and open in safari on iOS 8 or 7在 iOS 9 上从表视图打开 safari 视图控制器并在 iOS 8 或 7 上在 safari 中打开
【发布时间】:2015-11-25 23:19:20
【问题描述】:

您好,如果用户使用的是 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;

我在这行代码中收到错误“使用未声明的标识符 url”

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

删除 NSStringWithFormat 末尾的 url 使 Safari 视图控制器工作。但是在低于 9.0 的 iOS 上,例如8.4 应用崩溃。

【问题讨论】:

    标签: ios objective-c iphone safari sfsafariviewcontroller


    【解决方案1】:

    标准和推荐的方法是检查功能,而不是操作系统版本。在这种情况下,您可以检查是否存在 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.
        }
    }
    

    【讨论】:

    • 我已经更新了问题,显示我做了什么以及它显示的错误。
    • 你没有使用你的方法来安全地使用SFSafariViewController。不仅如此,你的openLink: 方法是错误的。您尝试使用 SFSafariViewController 而不检查该类是否可用。
    • @Avi 如果您为 iOS 9 及更高版本制作应用程序,那么您不需要检查 Api 可用性对吗?
    猜你喜欢
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 2020-11-29
    • 2022-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多