我在很多网站和 StackOverflow 上进行了搜索,但我找不到任何输出
我使用此功能在 Swift 项目中打开 Instagram,它工作正常。
guard var screenName = self.instagramLabel.text, screenName.count > 0 else { return }
if screenName.hasPrefix("@") {
screenName.removeFirst()
}
let appURL = URL(string: "instagram://user?username=\(screenName)")!
let application = UIApplication.shared
if application.canOpenURL(appURL) {
application.open(appURL)
} else {
guard let url = URL(string: "https://www.instagram.com/\(screenName)/") else { return }
self.openWeb(url)
}
现在我这样做是为了打开 Instagram 的 Threads 应用程序,但它不起作用
guard var screenName = self.threadsLabel.text, screenName.count > 0 else { return }
if screenName.hasPrefix("@") {
screenName.removeFirst()
}
let appURL = URL(string: "threads://user?username=\(screenName)")!
let application = UIApplication.shared
if application.canOpenURL(appURL) {
application.open(appURL)
} else {
guard let url = URL(string: "https://www.threads.net/\(screenName)/") else { return }
self.openWeb(url)
}
为此,只需删除
URL(string: "instagram://user?username=\(screenName)")!
并添加
URL(string: "threads://user?username=\(screenName)")!
但是它不起作用并执行了其他部分
希望我能得到答案
“URL 类型”(CFBundleURLTypes) 是控制哪些方案将打开您的应用程序的属性。
要允许打开另一个应用程序,请使用 LSApplicationQueriesSchemes:
本质上,您的定义是说 barcelona:// 链接应该打开您的应用程序,但您不允许在应用程序内使用它们。
如何使用示例:-
let appURL = URL(string: "barcelona://user?username=\(Yourusername)")!
let application = UIApplication.shared
if application.canOpenURL(appURL) {
application.open(appURL)
return
}
else {
guard let url = URL(string: "https://www.threads.net/\(Yourusername)/") else { return }
self.openWeb(url)
return
}