【发布时间】:2019-06-01 02:44:57
【问题描述】:
我遇到了一个问题,当我从 didFinishLaunchingWithOptions 中的已终止应用程序中享用午餐时,我无法访问我的 viewcontroller.swft 文件中的函数。
我正在尝试使用 AppDelegate 中的viewController?.loadRequestnotificationwaiting(for: url as! String) 访问该函数,我将数据传递给视图控制器,在那里我可以做一些事情。但是当我在视图控制器loadRequestnotificationwaiting 的函数中放置警报时。数据没有被传递。
现在我在其他领域使用相同的方法将数据从 appdelegate 传递给 viewcontroller,它们工作正常。在didFinishLaunchingWithOptions中使用时似乎不起作用
尝试从didFinishLaunchingWithOptions 访问时,视图控制器是否不可用?
AppDelegate.swift
class AppDelegate : UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
weak var viewController : ViewController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
ConnectionManager.sharedInstance.observeReachability()
// Override point for customization after application launch.
FirebaseApp.configure()
registerForPushNotifications()
// opened from a push notification when the app is closed
if let userInfo = launchOptions?[.remoteNotification] as? [String : AnyObject] {
if let object = userInfo["aps"] {
let url = object["url"]
viewController?.loadRequestnotificationwaiting(for: url as! String)
}
}
return true
}
}
ViewController.swift
class ViewController: UIViewController, WKNavigationDelegate {
func loadRequestnotificationwaiting(for notification_url_wait : String) {
notification_url_final_wait = notification_url_wait
let url = URL(string: notification_url_final_wait!)
let URLrequest = URLRequest(url: url!)
self.webView.load(URLrequest)
}
}
【问题讨论】:
-
viewController设置在哪里? -
你应该在我的代码顶部看到它
weak var viewController : ViewController? -
声明了它,但它没有给它赋值,所以它将是
nil。您可能想要应用程序的window属性中的rootViewController -
我在
didRegisterForRemoteNotificationsWithDeviceToken和didReceiveRemoteNotification中使用了同样的方法,它适用于那些 -
因此您的代码在某处为应用程序委托
viewController属性分配了一个值,但这发生在didFinishLaunching之后。
标签: ios swift uiviewcontroller appdelegate