2

我可以从 Siri 成功启动我的应用程序,但我在调试器中收到了 NSUserActivity 消息。我不记得在以前的版本中看到过该消息,但也许我没有注意。我错过了什么?有什么我应该从堆栈中弹出的东西吗?设置一些标志?

(我只能在设备上启动应用程序,而不是在模拟器中,但这是另一天的问题。Siri 告诉我出了点问题,然后再试一次。)

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {

    if userActivity.activityType != "com.mydomain.myactivity" {
        os_log("The app was called with an invalid activityType")
        return false
    }

    let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
    
    let tabBarController = self.window!.rootViewController as! UITabBarController
    tabBarController.tabBar.barTintColor = UIColor(red: 184.0/255.0, green: 64.0/255.0, blue: 37.0/255.0, alpha: 0.2)

    guard let tabVCs = tabBarController.viewControllers else {
        os_log("restorationHandler: Can't get tabVCs")
        return false
    }
    
    guard let myTargetViewController = mainStoryboard.instantiateViewController(withIdentifier: "myTargetViewController") as? MyTargetViewController else {
        os_log("restorationHandler: Can't get myTargetViewController from storyboard")
        return false
    }
    
    guard let tab1NavViewController = tabVCs[0] as? UINavigationController else {
        os_log("restorationHandler: Can't get tab1NavViewController from tabVCs")
        return false
    }
    
    tabBarController.selectedIndex = 0
    myTargetViewController.calledFromShortcut = true
    
    tab1NavViewController.pushViewController(myTargetViewController, animated: true)
    
    return true
}
4

1 回答 1

0

出现消息是因为您没有处理 userActivity 的交互。简单的处理程序如下所示:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    userActivity.interaction?.donate(completion: { error in
         if let error = error {
             print("Donate Interaction with error: \(error.localizedDescription)")
         } else {
             print("Donate Interaction Success")
         }
    })
    return true
    }
于 2021-01-18T11:20:38.593 回答