将应用程序从另一个应用程序打开到特定VC

问题描述 投票:1回答:1

我是iOS开发人员的新手,我没有找到任何具体的答案。

假设我有一个包含2个目标的应用程序,FirstApp(firstTarget)和SecondApp(secondTarget)。我在一些帖子中看到你可以从另一个帖子打开一个应用程序,我确实这样做:

if UIApplication.shared.canOpenURL(aUrl as! URL) {
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(aUrl!)
    } else {
        // Fallback on earlier versions
    }

但是我可以将它打开到SecondApp中的特定VC吗?

我在我的项目中包含了UniversalLink,还有AppDelegate方法,其中包含警报:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    print("UNIVERSAL LINK")
    let alert = UIAlertController(title: "Test", message:"Message", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    self.window?.rootViewController?.present(alert, animated: true, completion: nil)

    return true
}

问题是,只有在我再重复一次动作之后它才会起作用。这是第一次只打开应用程序。如果我返回FirstApp并重复操作,则弹出警报。

我可以用其他方式来做,或者你有这样的解决方案吗?

通知是否以相同的方式工作?当我点击它时,它会将您重定向到特定的VC。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    let noNetworkView = UIView()
    var reachability:Reachability!

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
    }

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
        let alert = UIAlertController(title: "Test", message:"Message", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
        self.window?.rootViewController?.present(alert, animated: true, completion: nil)

        return true
    }

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        return SDKApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
    }

    func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        return true
    }

    func applicationWillTerminate(_ application: UIApplication) {
        self.reachability.stopNotifier()
        self.saveContext()
    }
ios swift ios-universal-links
1个回答
1
投票

应用程序启动时不会调用continueUserActivity。您必须从didFinishLaunchingOptions的启动选项中拉出Universal Link。看到这个post.我推荐使用Branch,因为他们会把这一切都捆绑到一个回调中。

© www.soinside.com 2019 - 2024. All rights reserved.