Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a users twitter profile page from iOS app

Tags:

ios

swift

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")
    })

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?

Thanks

like image 610
Mehdi Avatar asked Jun 23 '17 21:06

Mehdi


People also ask

How do I open Twitter on iOS Twitter app?

Sign into your Apple ID. In Messages, tap a link for a tweet. It should open in the Twitter app.

How do you get your Twitter URL on iOS?

In the Twitter for iOS or Twitter for Android app: Tap the share icon ( on iOS, on Android) then tap Tweet this Moment to see the URL in the Tweet compose view. From this menu pop-up you also have the option to copy the URL link.


2 Answers

OK, there are two easy steps to achieve this in Swift 4:

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>

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)
   }
}
like image 161
kiril kiroski Avatar answered Oct 20 '22 01:10

kiril kiroski


Use this for twitter profile share, 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)
        }
    }
like image 45
Md Imran Choudhury Avatar answered Oct 20 '22 00:10

Md Imran Choudhury