如果在使用 SafariViewController 打开 url 时不成功,则处理完成
Handle completion if no success when opening url with SafariViewController
如果 SafariViewController
无法像 UIApplication.shared.open
那样打开 url
,是否有办法处理?
这是我的功能:
if ["http", "https"].contains(url.scheme?.lowercased() ?? "") {
// Can open with SFSafariViewController
let safariViewController = SFSafariViewController(url: url)
self.present(safariViewController, animated: true) {
// no bool given?
}
} else {
// Scheme is not supported or no scheme is given, use openURL
guard let url = URL(string: urlString) else {
self.showInvalidUrlAlert()
return
}
UIApplication.shared.open(url, completionHandler: { success in
if !success {
print("failed")
self.showInvalidUrlAlert()
}
})
}
我认为 SFSafariViewController
没有为您的应用程序提供多少 interaction/feedback。
根据 Apple 文档:
The user's activity and interaction with SFSafariViewController are
not visible to your app, which cannot access AutoFill data, browsing
history, or website data.
Choosing the Best Web Viewing Class
If your app lets users view
websites from anywhere on the Internet, use the SFSafariViewController
class. If your app customizes, interacts with, or controls the display
of web content, use the WKWebView class.
正如 Apple 所建议的,您可能想看看 WKWebView class。
使您的演示者视图控制器符合 SFSafariViewControllerDelegate
。
class ViewController: SFSafariViewControllerDelegate
并设置 ViewController
为 SafariViewController
的代表
safariViewController.delegate = self
然后处理加载状态是否成功使用
didCompleteInitialLoad代表。
func safariViewController(_ controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
// Handle loadStatus.
print(didLoadSuccessfully)
// if load unsuccessful, dismiss SFSafariViewController and try to open using openURL
if !didLoadSuccessfully {
controller.dismiss(animated: true) {
// UIApplication.shared.open(..
}
}
}
如果 SafariViewController
无法像 UIApplication.shared.open
那样打开 url
,是否有办法处理?
这是我的功能:
if ["http", "https"].contains(url.scheme?.lowercased() ?? "") {
// Can open with SFSafariViewController
let safariViewController = SFSafariViewController(url: url)
self.present(safariViewController, animated: true) {
// no bool given?
}
} else {
// Scheme is not supported or no scheme is given, use openURL
guard let url = URL(string: urlString) else {
self.showInvalidUrlAlert()
return
}
UIApplication.shared.open(url, completionHandler: { success in
if !success {
print("failed")
self.showInvalidUrlAlert()
}
})
}
我认为 SFSafariViewController
没有为您的应用程序提供多少 interaction/feedback。
根据 Apple 文档:
The user's activity and interaction with SFSafariViewController are not visible to your app, which cannot access AutoFill data, browsing history, or website data.
Choosing the Best Web Viewing Class
If your app lets users view websites from anywhere on the Internet, use the SFSafariViewController class. If your app customizes, interacts with, or controls the display of web content, use the WKWebView class.
正如 Apple 所建议的,您可能想看看 WKWebView class。
使您的演示者视图控制器符合 SFSafariViewControllerDelegate
。
class ViewController: SFSafariViewControllerDelegate
并设置 ViewController
为 SafariViewController
safariViewController.delegate = self
然后处理加载状态是否成功使用 didCompleteInitialLoad代表。
func safariViewController(_ controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
// Handle loadStatus.
print(didLoadSuccessfully)
// if load unsuccessful, dismiss SFSafariViewController and try to open using openURL
if !didLoadSuccessfully {
controller.dismiss(animated: true) {
// UIApplication.shared.open(..
}
}
}