xcode SFSafariViewController 使用 Objective-C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33593670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:10:45  来源:igfitidea点击:

SFSafariViewController using Objective-C

xcodesafarixcode7keychain

提问by Panda

What is the best way to implement SFSafariViewController using Objective-C. I currently have a UITableView and each of them leads to a different website. May I know how can I code each of them to load a different website in SFSafariViewController using Objective-C, not Swift? Thanks in advance!

使用 Objective-C 实现 SFSafariViewController 的最佳方法是什么。我目前有一个 UITableView,每个都指向不同的网站。我可以知道如何使用 Objective-C 而不是 Swift 对它们中的每一个进行编码以在 SFSafariViewController 中加载不同的网站吗?提前致谢!

回答by Mike Flynn

This will work with newer versions like 10.

这将适用于较新的版本,例如 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];
}

回答by John

Here's a version that checks the iOS version to handle iOS 8, and open links in safari if SFSafariViewController isn't compatible

这是一个检查 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];
}

Then also call this to handle the done button

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

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