Tutorial – Following a Twitter page.
In todays connected world it is important to have a Twitter page so your user’s can connect and interact with you. In this tutorial we will look at adding a button that will open up a Twitter page within the Twitter app. If the user does not have the Facebook installed it will open up in Safari instead.
You can also view the tutorial on youtube:
To start of we create a blank project in Xcode. Select the storyboard and add a button to it.
From here in the top right select show the assistant editor , this shows our view controller. Select the button and control drag it into the code for the view controller. This will allow you to create an IBAction for the button. Create one as “followusTwitter”.
Now the button is connected our code will go in this IBOutlet to open up the Twitter page.
We need two variables, one to store the Twitter page URL, and also the URL to open it within the twitter app.
Now that we have these we make our two variables as follows:
var twUrl: NSURL = NSURL(string: "twitter://user?screen_name=SeemuApps")! var twUrlWeb: NSURL = NSURL(string: "https://twitter.com/SeemuApps")!
Replace the “SeemuApps” with whatever twitter page you want to open.
From here we now need a way to check if the Twitter app is installed. If so we open up the page in the app, otherwise we open it up in safari. To do this we use the following:
if(UIApplication.sharedApplication().canOpenURL(twUrl)) { // If user twitter installed UIApplication.sharedApplication().openURL(twUrl) } else { // If user does not have twitter installed UIApplication.sharedApplication().openURL(twUrlWeb) }
Now you can run your code and test it. In the simulator since Facebook isn’t installed it will open the page up in safari. Otherwise if it is run on a iOS device with Twitter installed it will open up in the Twitter app.