菜鸟教程小白 发表于 2022-12-11 18:51:55

iOS 在应用程序处于非事件状态时处理动态链接


                                            <p><p>我的应用程序委托(delegate):</p>

<pre><code>let customURLScheme = &#34;dlscheme&#34;

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -&gt; Bool {

FIROptions.default().deepLinkURLScheme = self.customURLScheme
FIRApp.configure()

return true
}

func application(_ app: UIApplication, open url: URL, options: ) -&gt; Bool {
    return application(app, open: url, sourceApplication: nil, annotation: [:])
}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -&gt; Bool {
    let dynamicLink = FIRDynamicLinks.dynamicLinks()?.dynamicLink(fromCustomSchemeURL: url)
    if let dynamicLink = dynamicLink {
      let message = generateDynamicLinkMessage(dynamicLink)
      if #available(iOS 8.0, *) {
            showDeepLinkAlertView(withMessage: message)
      }
      return true
    }
    if #available(iOS 8.0, *) {
      showDeepLinkAlertView(withMessage: &#34;openURL:\n\(url)&#34;)
    } else {
    }
    return false
}
@available(iOS 8.0, *)
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping (?) -&gt; Void) -&gt; Bool {
    guard let dynamicLinks = FIRDynamicLinks.dynamicLinks() else {
      return false
    }
    let handled = dynamicLinks.handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
      let message = self.generateDynamicLinkMessage(dynamiclink!)
      self.showDeepLinkAlertView(withMessage: message)
    }
    return handled
}

func generateDynamicLinkMessage(_ dynamicLink: FIRDynamicLink) -&gt; String {
    let matchConfidence: String
    if dynamicLink.matchConfidence == .weak {
      matchConfidence = &#34;Weak&#34;
    } else {
      matchConfidence = &#34;Strong&#34;
    }
    let message = &#34;App URL: \(dynamicLink.url)\nMatch Confidence: \(matchConfidence)\n&#34;
    return message
}

@available(iOS 8.0, *)
func showDeepLinkAlertView(withMessage message: String) {
    let okAction = UIAlertAction.init(title: &#34;OK&#34;, style: .default) { (action) -&gt; Void in
      print(&#34;OK&#34;)
    }

    let alertController = UIAlertController.init(title: &#34;Deep-link Data&#34;, message: message, preferredStyle: .alert)
    alertController.addAction(okAction)
    self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
</code></pre>

<p>这是我的代码。我正在使用 firebase 动态链接,并像本教程一样实现它:</p>

<p> <a href="https://firebase.google.com/docs/dynamic-links/ios" rel="noreferrer noopener nofollow">https://firebase.google.com/docs/dynamic-links/ios</a> </p>

<p>还有这个示例:</p>

<p> <a href="https://github.com/firebase/quickstart-ios/blob/master/dynamiclinks/DynamicLinksExampleSwift/AppDelegate.swift" rel="noreferrer noopener nofollow">https://github.com/firebase/quickstart-ios/blob/master/dynamiclinks/DynamicLinksExampleSwift/AppDelegate.swift</a> </p>

<p>当我的应用程序处于后台时,它运行良好。当我点击动态链接时,打开应用程序并显示带有 url 的警报。</p>

<p>但如果我的应用程序处于非事件状态(未运行),它就无法工作。只需打开应用程序,什么都不做。 </p>

<p>还有我的问题:当应用处于非事件状态时如何处理动态链接?<br/>
对不起我的英语</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong> swift5</strong></p>
<p>你可以这样处理:</p>
<p>AppDelegate.swift</p>
<pre><code>import Firebase
import FirebaseDynamicLinks

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -&gt; Bool {

    // ... your other code here

    FirebaseApp.configure()

    let activityKey = NSString(string: &#34;UIApplicationLaunchOptionsUserActivityKey&#34;)
    if let userActivityDict = launchOptions?[.userActivityDictionary] as? , let userActivity = userActivityDict as? NSUserActivity, let webPageUrl = userActivity.webpageURL {
      
      DynamicLinks.dynamicLinks().handleUniversalLink(webPageUrl) { (dynamiclink, error) in
            
            // do some stuff with dynamiclink
            
      }
      
    }
   
    return true
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOS 在应用程序处于非事件状态时处理动态链接,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42081645/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42081645/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS 在应用程序处于非事件状态时处理动态链接