深度链接URL方案不调用任何函数(快速)

时间:2019-10-02 11:01:43

标签: ios swift deep-linking

实现深层链接的

im是iOS。我已经在Project-Setting-> Info-> Url类型中配置了URL Scheme URL方案:洗车角色:查看者

当我键入carwash://时,浏览器要求打开应用程序,但是在应用程序中没有任何调用,我无法处理应该采取的行动。

apple文档说您应该在AppDelegate中覆盖应用程序(打开url),但是深层链接会调用它,并且应用程序会在最后一个状态下打开

这是我的代码,不起作用

 func application(_ app: UIApplication, open url: URL,
                     options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        fatalError()
    }

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        if let url = launchOptions?[UIApplication.LaunchOptionsKey.url] as? URL {
           /// some
            fatalError()
        }
    GMSServices.provideAPIKey("")

    return true
} 

迅速5 模拟器:iOS 13

5 个答案:

答案 0 :(得分:3)

经过几天努力解决方案网址后,我终于找到了答案。 我的模拟器的iOS版本是13。在iOS 13 UIApplication中,打开的网址不会被调用(至少就我而言)

您应该覆盖iOS 13中可用的SceneDelegate中的以下功能:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    if let url = URLContexts.first?.url{
        print(url)

    }
}

编辑: 如果用户在您的应用未运行时点击该链接,则您将不会对其进行响应

您必须实现scene(_:willConnectTo:options:)来检测选项中的URLContext:并对其进行处理。

答案 1 :(得分:0)

您应在Info.plist文件中配置深层链接

示例:

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>CFBundleURLName</key>
            <string>carwash</string> // your identifier
            <key>CFBundleURLSchemes</key>
            <array>
                <string>carwash</string> // schema
            </array>
        </dict>
    </array>

答案 2 :(得分:0)

您应该使用application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool

请注意,您的方法中没有 return 语句

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    print("Got from URL: \(url)"
    return true
}

模拟器中的链接最好通过终端进行

xcrun simctl openurl booted “carwasher://deeplink”

答案 3 :(得分:0)

对于我在 iOS13+ 中的以下作品(在我的 SceneDelegate 中):

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Set up everything you need
    // ...
    
    // Handle deep link on cold start
    if let url = connectionOptions.userActivities.first?.webpageURL {
        handle(url: url)
    }
}

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    if let url = userActivity.webpageURL {
        // Handle deep link when the app is running
        handle(url: url)
    }
}

答案 4 :(得分:0)

在 iOS 13+ 或场景委托中,会调用两个方法,并且 willPerformHTTPRedirection 方法肯定会调用。

  func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        if userActivity.activityType == NSUserActivityTypeBrowsingWeb, let url =      userActivity.webpageURL {
          
        }
      }
    }

 func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Swift.Void) {
            if let url = request.url {
               guard let detailDictionary = String.queryParameters(from: url) else { return }

            }
        }
    }

通过这个函数解析url:

static func queryParameters(from url: URL) -> [String: String]? {
    let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
    var queryParams: [String : String]? = nil
    if let components = urlComponents?.queryItems {
      queryParams = [String: String]()
      for queryItem in components {
        if queryItem.value == nil {
          continue
        }
        queryParams?[queryItem.name] = queryItem.value
      }
    }
    return queryParams
  }