iOS 11/Xcode 9.0中的SFSafariViewController为空

gen*_*aks 3 objective-c ios ios11 xcode9

单击UIActionSheet内的按钮,我有一个SFSafariViewController打开.它已经工作正常,并且在除iOS 11之外的所有iOS版本上仍然可以正常工作.对于iOS 11或Xcode 9.0中可能导致此问题的SFSafariViewController,他们有什么改变吗?

更新 - 所以它的Xcode 9.0似乎导致了这个问题.我尝试在不同的iOS版本上运行它,所有这些似乎都在提出这个问题.当我使用Xcode 8.3.3运行它时,它曾经工作正常,我不再拥有它:(

这是代码 -

- (void)presentWebView:(NSString *)url {
url = [url stringByReplacingOccurrencesOfString:@" " withString:@"+"];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString:url];

if (URL) {
    if ([SFSafariViewController class] != nil) {
        SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
        sfvc.delegate = self;
        [self.tabBarController 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.
}
Run Code Online (Sandbox Code Playgroud)

}

lar*_*mba 6

我已经设法在我的代码中修复此问题.我希望这可以帮助其他有类似问题的人.

我有与此处描述的完全相同的问题.我尝试了以上所有内容,但遗憾的是没有任

在我的应用程序中有不同的窗口.修复是为了确保在显示SFSafariViewController之前显示的窗口是"关键".例如:

class MyViewController: UIViewcontroller {
    func showSafariViewController() {
        // imagine we have 2 windows, one for 'normal' content (key window) and one for 'other' content. lets say we're showing the 'other' content window, and have hidden the 'normal' window. you can see the 'other' content window in the app, but it won't be the key window!
        let window = // get the 'other' content window

        // make sure we make it the key window before presenting safari
        window.makeKey()

        // now present safari and celebrate victory by triumphantly slurping on your hot black coffee
        let mySafariViewController = SFSafariViewController(...)
        self.present(mySafariViewController ...)
    }
}
Run Code Online (Sandbox Code Playgroud)

我怀疑Apple正在SFSafariViewController窗口中搜索一个实例UIApplication.shared.keyWindow.也许他们正在从其他地方添加一个儿童视图.在它所述的文档中The user's activity and interaction with SFSafariViewController are not visible to your app,所以也许这个bug与增加的安全级别有关https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller


Sur*_*put 5

我尝试使用延迟来完成它并查看控制器加载。两者都为我工作。

方法一。使用延迟。

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
      let controller = SFSafariViewController(url: url)
      controller.modalPresentationStyle = .overFullScreen
      self.present(controller, animated: true, completion: nil)
      controller.delegate = self
    }
Run Code Online (Sandbox Code Playgroud)

方法 2.加载视图。

      let controller = SFSafariViewController(url: url)
      let _ = controller.view
      controller.modalPresentationStyle = .overFullScreen
      self.present(controller, animated: true, completion: nil)
      controller.delegate = self
Run Code Online (Sandbox Code Playgroud)

  • 方法二使用 Xcode 11 修复了 iOS 12 上的问题。该问题在 iOS 13 上不会出现。 (3认同)