[英]Unable to access viewcontroller from didFinishLaunchingWithOptions when app launches
I am having an issue where I am unable to access a function in my viewcontroller.swft file when lunching from a killed app in didFinishLaunchingWithOptions
. 我遇到了一个问题,当我在
didFinishLaunchingWithOptions
被杀死的应用程序中didFinishLaunchingWithOptions
时,无法访问viewcontroller.swft文件中的功能。
I am trying to access the function with viewController?.loadRequestnotificationwaiting(for: url as! String)
from AppDelegate which I pass data to the viewcontroller where I can do some stuff. 我试图通过
viewController?.loadRequestnotificationwaiting(for: url as! String)
访问该函数,该函数将数据传递给viewcontroller,在其中我可以做一些事情。 But when I place an alert in the function in the viewcontroller loadRequestnotificationwaiting
. 但是当我在视图
loadRequestnotificationwaiting
中的函数中放置警报时。 The data is not being passed. 数据未传递。
Now I use this same method in other areas to pass data to the viewcontroller from the appdelegate and they work fine. 现在,我在其他区域使用相同的方法将数据从appdelegate传递到viewcontroller,并且它们可以正常工作。 It seems to not work when using it in
didFinishLaunchingWithOptions
在
didFinishLaunchingWithOptions
使用它似乎不起作用
Is the viewcontroller not available yet when trying to access it from didFinishLaunchingWithOptions
? 尝试从
didFinishLaunchingWithOptions
访问didFinishLaunchingWithOptions
时,它尚不可用吗?
AppDelegate.swift 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 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)
}
}
You are relying on your AppDelegate
's viewController
property, but you are setting this property in your viewDidLoad
method of your view controller; 您依赖于
AppDelegate
的viewController
属性,但是正在视图控制器的viewDidLoad
方法中设置此属性; This is fine when a notification is received when your app is already running, since the viewController
property is already set. 当您的应用已在运行时收到通知时,这很好,因为已经设置了
viewController
属性。
When a notification causes your application to be launched, the viewController
property is not set and the function isn't called. 通知导致您的应用程序启动时, 未设置
viewController
属性,并且不会调用该函数。
Assuming that the view controller you need is the initial view controller from your storyboard, you can get the root view controller from your app delegate's window
property; 假设所需的视图控制器是情节提要中的初始视图控制器,则可以从应用程序委托的
window
属性中获取根视图控制器。
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"] as? String,
let viewController = self.window?.rootViewController as? ViewController {
viewController.loadRequestnotificationwaiting(for: url)
}
}
return true
}
UIApplication.shared.keyWindow!.rootViewController!
UIApplication.shared.keyWindow!.rootViewController!
来召唤它UIApplication.shared.keyWindow!.rootViewController!
or overwrite it with your viewController
viewController
覆盖它 -[NotificationCenter postNotificationName:object:userInfo:]
and observe this notification name in View Controller -[NotificationCenter postNotificationName:object:userInfo:]
发出通知,并在View Controller中观察此通知名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.