Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 未声明类型UIUserActivity的使用_Ios_Swift - Fatal编程技术网

Ios 未声明类型UIUserActivity的使用

Ios 未声明类型UIUserActivity的使用,ios,swift,Ios,Swift,我正在尝试按照以下说明在iOS应用程序中启用深度链接: 当我将示例代码添加到我的AppDelegate.swift文件中时: private func application(_ application: UIApplication, continue userActivity: UIUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?)

我正在尝试按照以下说明在iOS应用程序中启用深度链接:

当我将示例代码添加到我的
AppDelegate.swift
文件中时:

private func application(_ application: UIApplication,
                 continue userActivity: UIUserActivity,
                 restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool  {
   ...
}
每当我尝试构建应用程序时,就会出现错误
使用未声明的UIUserActivity类型。我在文件顶部有
import UIKit
。我尝试过清理和重建,但得到了相同的错误。我的应用程序的目标是iOS 10.1

我还尝试用“MacOS”版本的处理程序替换它,它编译正常,但当我通过深层链接打开应用程序时,该函数不会触发


如何让xcode识别“UIUserActivity”并使深度链接处理程序工作?

弄明白了这一点,似乎苹果的深度链接文档对于iOS 13已经过时或不正确

下面是一篇关于正确方法的好文章:

如果您使用的是SceneDelegate,则深度链接文档页面上提到的任何功能都不会被触发,相反,您应该在SceneDelegate中实现这两个功能:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
  for context in URLContexts {
    print("url: \(context.url.absoluteURL)")
    print("scheme: \(context.url.scheme)")
    print("host: \(context.url.host)")
    print("path: \(context.url.path)")
    print("components: \(context.url.pathComponents)")
  }
}

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
  guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
    let urlToOpen = userActivity.webpageURL else {
      return
  }

  print("[SceneDelegate] \(urlToOpen)")
}

请注意,这是使用
nsseractivity
而不是
UIUserActivity

您想要的
nsseractivity
我尝试过使用它,但在通过深度链接打开时未触发处理程序是的,正如您所了解的,深度链接不是用户活动。深度链接是URL
internal func application(_ app: UIApplication, open url: URL,
                         options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    if let scheme = url.scheme,
        scheme.localizedCaseInsensitiveCompare("com.appName") == .orderedSame,
        let view = url.host {

        var parameters: [String: String] = [:]
        URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems?.forEach {
            parameters[$0.name] = $0.value
        }

        print("[AppDelegate] parameters: \(parameters)")
        // redirect(to: view, with: parameters)
    }
    return true
}

internal func application(_ application: UIApplication,
                 continue userActivity: NSUserActivity,
                 restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    // Get URL components from the incoming user activity
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
        let incomingURL = userActivity.webpageURL,
        let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else {
        return false
    }

    // Check for specific URL components that you need
    guard let path = components.path,
    let params = components.queryItems else {
        return false
    }
    print("[AppDelegate] path = \(path)")

    if let email = params.first(where: { $0.name == "email" } )?.value,
        let token = params.first(where: { $0.name == "token" })?.value {

        print("[AppDelegate] email = \(email)")
        print("[AppDelegate] token = \(token)")
        return true

    } else {
        print("[AppDelegate] Either email or token missing")
        return false
    }
}