2017-06-13 46 views
2

我有一個應用程序,當用戶與另一個用戶共享應用程序中的特定內容時,使用深層鏈接導航到頁面。當第二個用戶的應用程序已經運行時,這可以工作,但如果應用程序沒有運行,它只會打開應用程序並保留在主屏幕上。我知道我必須在這裏錯過一些非常簡單的東西,但我無法弄清楚,在谷歌上找不到任何關於此的答案。深層鏈接只在應用程序運行時才起作用

我在AppDelegate.swift代碼:

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { 
     let urlPath : String = url.path as String! 
     let urlHost : String = url.host as String! 
     let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 

     if(urlHost != "example.com") 
     { 
      print("Call Not From Correct Host. Not Continuing...") 
      return false 
     } 

     if(urlPath == "/articles"){ 

      let article: ArticleDetailsViewController = mainStoryboard.instantiateViewController(withIdentifier: "ArticleDetailsViewController") as! ArticleDetailsViewController 
      self.window?.rootViewController = article 
     } 
     self.window?.makeKeyAndVisible() 
     return true 
    } 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 

     return true 
    } 
+0

您已經添加URL方案的.plist文件? – PiterPan

+0

是的。就像我說的那樣,如果應用程序在手機上運行,​​它可以很好地工作。唯一的問題是,當我完全退出應用程序(而不是最小化)時,它將無法工作。該應用程序必須運行才能正常工作。如果應用程序未運行,它將不會執行。它只啓動應用程序,但不執行代碼導航到文章 –

回答

6

這是正確的行爲。 你應該處理它在appliction(_: didFinishLaunchingWithOptions:)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 
    if let url = launchOptions[.url] as? URL, let annotation = launchOptions[.annotation] { 
     return self.application(application, open: url, sourceApplication: launchOptions[.sourceApplication] as? String, annotation: annotation) 
    } 
    return true 
} 
+1

完美!當之無愧的接受答案!按預期工作! –

相關問題