使用Objective-C的SFSafariViewController

Pan*_*nda 2 safari xcode keychain xcode7

使用Objective-C实现SFSafariViewController的最佳方法是什么.我目前有一个UITableView,每个都导致一个不同的网站.我是否可以知道如何使用Objective-C而不是Swift在SFSafariViewController中加载其中的每个网站?提前致谢!

Mik*_*ynn 18

这适用于像10这样的新版本.

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://google.com"]];
if ([SFSafariViewController class] != nil) {
    SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
    [self presentViewController:sfvc animated:YES completion:nil];
} else {
    [[UIApplication sharedApplication] openURL:URL];
}
Run Code Online (Sandbox Code Playgroud)


小智 8

这是一个版本,用于检查iOS版本以处理iOS 8,如果SFSafariViewController不兼容,则在Safari中打开链接

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://google.com"]];
if ([[UIDevice currentDevice].systemVersion hasPrefix:@"9"]) {
    SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
    [self presentViewController:sfvc animated:YES completion:nil];
} else {
    [[UIApplication sharedApplication] openURL:URL];
}
Run Code Online (Sandbox Code Playgroud)

然后还调用它来处理完成按钮

- (void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller {
[controller dismissViewControllerAnimated:YES completion:nil];}
Run Code Online (Sandbox Code Playgroud)

  • 不要这样做,它甚至不能在iOS 10上工作.做`if([SFSafariViewController class]!= nil){` (9认同)
  • [[UIDevice currentDevice] .systemVersion hasPrefix:@"9"]*facepalm*at that (3认同)