从iOS应用程序打开用户的Twitter个人资料页面
我正在尝试从我的应用程序到本机Twitter应用程序上用户的Twitter个人资料的深层链接.我已经为twitter和以下代码添加了架构规则:
I am trying to deep link from my app to a user's twitter profile on the native twitter app. I have added schema rules for twitter and the following code:
application.open( URL(string:"twitter://user?screen_name=BarackObama", options[:], completionHandler:{(success) in
print("Success")
})
我可以成功打开twitter应用程序并看到控制台打印成功",但是我看到的是我自己的twitter feed,而不是用户的twitter页面.这个网址架构仍然有效吗?
I can successfully open the twitter app and see the console print "Success" but my own twitter feed is what I see, not the user's twitter page. Is this url schema still valid?
谢谢
好的,有两个简单的步骤可以在Swift 4中实现:
OK, there are two easy steps to achieve this in Swift 4:
首先,您必须修改Info.plist以使用LSApplicationQueriesSchemes列出instagram和facebook.只需打开Info.plist作为源代码,然后将其粘贴:
First, you have to modify Info.plist to list instagram and facebook with LSApplicationQueriesSchemes. Simply open Info.plist as a Source Code, and paste this:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>twitter</string>
</array>
之后,您可以打开Twitter应用程序.这是Twitter的完整代码,您可以将此代码链接到操作中具有的任何按钮:
After that, you can open twitter apps. Here is a complete code for twitter you can link this code to any button you have as an Action:
@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)
}
}