简体   繁体   English

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

[英]Open a users twitter profile page from iOS app

I am trying to deep link from my app to a user's twitter profile on the native twitter app. 我正试图从我的应用程序深入链接到本机Twitter应用程序上的用户的Twitter个人资料。 I have added schema rules for twitter and the following code: 我已经为twitter添加了架构规则和以下代码:

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

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. 我可以成功打开Twitter应用程序并看到控制台打印“成功”,但我自己的推文是我看到的,而不是用户的推特页面。 Is this url schema still valid? 此网址架构是否仍然有效?

Thanks 谢谢

OK, there are two easy steps to achieve this in Swift 4: 好的,在Swift 4中有两个简单的步骤来实现这一点:

First, you have to modify Info.plist to list instagram and facebook with LSApplicationQueriesSchemes. 首先,您必须修改Info.plist以使用LSApplicationQueriesSchemes列出instagram和facebook。 Simply open Info.plist as a Source Code, and paste this: 只需打开Info.plist作为源代码,然后粘贴:

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

After that, you can open twitter apps. 之后,您可以打开Twitter应用程序。 Here is a complete code for twitter you can link this code to any button you have as an Action: 这是一个完整的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)
   }
}

Use this for twitter profile share, Swift 4: 将此用于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)
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM