gpt4 book ai didi

ios - 自定义 URL 方案

转载 作者:行者123 更新时间:2023-11-30 12:00:36 25 4
gpt4 key购买 nike

如何处理自定义 URL 方案以允许一个应用程序定向到另一个应用程序?例如instagram://user?username=someusername 通过用户名将用户直接引导至用户个人资料。我需要创建类似的东西。

我已经检查了 application(_ application: UIApplication, open url: URL, sourceApplication: String?, comment: Any) -> Boolapplication(_ application: UIApplication, handleOpen url: URL) -> Bool 但在尝试实现时它们似乎有一条红线穿过,所以我认为它们已被弃用。此外,当我通过浏览器中的 url 打开我的应用程序时,它们似乎没有被调用。

最佳答案

以此网址为例:appName://?id=12345

一旦启动,您就可以使用 URL 在应用程序中执行某些操作,如下所示

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
if let items = urlComponents?.queryItems as [NSURLQueryItem]?,
(url.scheme == "appName") {
if items.first?.name == "id",
let id = items.first?.value {
print(id) // prints 12345 to console
// do something with the id, possibly present/push a controller for the user
}
}
return false
}

关于ios - 自定义 URL 方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47291172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com