从iOS应用程序打开用户twitter个人资料页面

时间:2017-06-23 21:11:29

标签: ios swift twitter

我正在尝试从我的应用程序深层链接到本机Twitter应用程序上的用户的Twitter个人资料。我已经为twitter添加了架构规则和以下代码:

    application.open(  URL(string:"twitter://user?screen_name=BarackObama", options[:],  completionHandler:{(success) in 
        print("Success")
    })

我可以成功打开Twitter应用程序并看到控制台打印“成功”但我自己的推文是我看到的,而不是用户的推特页面。此网址架构是否仍然有效?

由于

2 个答案:

答案 0 :(得分:8)

好的,在Swift 4中有两个简单的步骤来实现这一点:

首先,您必须修改Info.plist以使用LSApplicationQueriesSchemes列出instagram和facebook。只需将Info.plist打开为源代码,然后粘贴:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>twitter</string>
</array>

之后,您可以打开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)
   }
}

答案 1 :(得分:3)

将此用于Twitter个人资料共享,Swift 4:

    let screenName =  "NJMINISTRIESINC"
    let appURL = NSURL(string: "twitter://user?screen_name=\(screenName)")!
    let webURL = NSURL(string: "https://twitter.com/\(screenName)")!

    if UIApplication.shared.canOpenURL(appURL as URL) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(appURL as URL, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(appURL as URL)
        }
    } else {
        //redirect to safari because the user doesn't have Instagram
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(webURL as URL, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(webURL as URL)
        }
    }