您好,如果用户使用的是iOS 9或更高版本,我想从 Safari View Controller 中的表格 View 单元打开我的网站。如果用户使用的是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检视 Controller 。但是我不确定如何组合这两组代码。
- (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]);
    }
}

然后将此代码添加到我的表 View 单元中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 View Controller 工作。但是在9.0以下的iOS上,例如8.4应用程序崩溃。

最佳答案

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

关于ios - 在iOS 9上从表格 View 打开Safari浏览器 View Controller ,并在iOS 8或7上在Safari浏览器中打开,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33928119/

10-14 19:57