我正试图从我的应用程序深入链接到本机Twitter应用程序上的用户的Twitter个人资料.我已经为twitter添加了架构规则和以下代码:
application.open( URL(string:"twitter://user?screen_name=BarackObama", options[:], completionHandler:{(success) in
print("Success")
})
Run Code Online (Sandbox Code Playgroud)
我可以成功打开Twitter应用程序并看到控制台打印"成功",但我自己的推文是我看到的,而不是用户的推特页面.此网址架构是否仍然有效?
谢谢
kir*_*ski 12
好的,在Swift 4中有两个简单的步骤来实现这一点:
首先,您必须修改Info.plist以使用LSApplicationQueriesSchemes列出instagram和facebook.只需打开Info.plist作为源代码,然后粘贴:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>twitter</string>
</array>
Run Code Online (Sandbox Code Playgroud)
之后,您可以打开Twitter应用程序.这是一个完整的twitter代码,您可以将此代码链接到您作为操作的任何按钮:
@IBAction func followOnTwitter(sender: AnyObject) {
let screenName = "AffordIt_App"
let appURL = NSURL(string: "twitter://user?screen_name=\(screenName)")!
let webURL = NSURL(string: "https://twitter.com/\(screenName)")!
let application = UIApplication.shared
if application.canOpenURL(appURL as URL) {
application.open(appURL as URL)
} else {
application.open(webURL as URL)
}
}
Run Code Online (Sandbox Code Playgroud)
将此用于 Twitter 个人资料共享,Swift 4:
let screenName = "NJMINISTRIESINC"
let appURL = URL(string: "twitter://user?screen_name=\(screenName)")!
let webURL = URL(string: "https://twitter.com/\(screenName)")!
if UIApplication.shared.canOpenURL(appURL as URL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(appURL)
} else {
UIApplication.shared.openURL(appURL)
}
} else {
//redirect to safari because the user doesn't have Instagram
if #available(iOS 10.0, *) {
UIApplication.shared.open(webURL)
} else {
UIApplication.shared.openURL(webURL)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3334 次 |
| 最近记录: |