【问题标题】:Unable to access viewcontroller from didFinishLaunchingWithOptions when app launches应用启动时无法从 didFinishLaunchingWithOptions 访问视图控制器
【发布时间】: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
  • 我在didRegisterForRemoteNotificationsWithDeviceTokendidReceiveRemoteNotification 中使用了同样的方法,它适用于那些
  • 因此您的代码在某处为应用程序委托viewController 属性分配了一个值,但这发生在didFinishLaunching 之后。

标签: ios swift uiviewcontroller appdelegate


【解决方案1】:

您依赖于您的AppDelegateviewController 属性,但是您在视图控制器的viewDidLoad 方法中设置了此属性;当您的应用已在运行时收到通知时,这很好,因为 viewController 属性已设置。

当通知导致您的应用程序启动时,viewController 属性未设置并且该函数未被调用。

假设您需要的视图控制器是故事板中的初始视图控制器,您可以从应用代理的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
}

【讨论】:

  • 当通知从应用程序的关闭状态进入时应用程序崩溃Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
  • 你需要确保你没有强制解开可能是nil的东西。在视图控制器中处理通知可能不是最好的方法;当您的应用收到通知并且它不在前台时,可能不会加载视图控制器。如果您确实使用视图控制器,请准备好将网点设为nil
  • 我不一定会在视图控制器中处理通知。我在 FirebaseMessagingService 中这样做。我只是将有效负载中的 url 发送到 viewcontroller,这样我就可以在我的 webview 中加载该 url
  • 您对通过 Intent 和 LocalBroadcastManager 发送网址有何看法?
【解决方案2】:
  1. 如果您的视图控制器是 root,您可以通过调用 UIApplication.shared.keyWindow!.rootViewController! 来召唤它或使用您的 viewController 覆盖它
  2. 您可以使用-[NotificationCenter postNotificationName:object:userInfo:] 发出通知并在视图控制器中观察此通知名称

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    相关资源
    最近更新 更多