要解决这个问题,您可以使用以下方法关闭 WebView:
在 App Delegate 中,使用 NSNotificationCenter
来接收 OAuth 完成的通知。在通知的回调方法中,关闭 WebView。
以下是一个示例代码:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 注册通知观察者
NotificationCenter.default.addObserver(self, selector: #selector(oauthCompleted(notification:)), name: NSNotification.Name(rawValue: "OAuthCompletedNotification"), object: nil)
return true
}
@objc func oauthCompleted(notification: Notification) {
// 关闭 WebView
if let webView = notification.object as? UIWebView {
webView.removeFromSuperview()
}
}
// ...
func applicationWillTerminate(_ application: UIApplication) {
// 移除观察者
NotificationCenter.default.removeObserver(self)
}
}
在 OAuth 完成后,发送一个通知来关闭 WebView。在你的 OAuth 的回调方法中,添加以下代码:
// OAuth 完成后发送通知
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "OAuthCompletedNotification"), object: webView)
这样,当接收到该通知时,App Delegate 将会关闭 WebView。请确保通知的名称在发送和接收的地方一致。
请注意,这只是一个示例代码,您需要根据您的具体情况进行适当的修改和调整。