Saturday, March 29, 2014

Add functionality to UIAlertView buttons and share to Twitter and Facebook [Code Tutorial]

PhoneDog Media Exclusive Download iM5, now available in the App Store and Google Play. iM5 is a PhoneDog Media backed Social Platform to inspire real-life action through the crowdsourcing of ideas. See the video

hello_tip_topLast week I got started with a new type of article, dubbed Code Tutorials. Well, today I'm back with another, and they are most likely going to be every Monday and Thursday. Today I am going to be adding on to the previous tutorial, so if you have yet to check that out, you can do so over here: Build your first iPhone app [Code Tutorial]. If you would rather use my code to add on to for this, you can download the project from GitHub here: HelloTiP (make sure you are in the master branch). This is going to be taking the UIAlertView that we implemented and adding buttons to it, as well as sharing text to Twitter and Facebook. Let's get started…

Screen_Shot_2014-03-24_at_6_12_54_PMBecause we are going to be using the social sharing services Twitter and Facebook, we will need to add a framework that will be used later on. Adding a framework is simple, just head on over to the main project page (if you are using my code from last tutorial, that'd be in HelloTiP.xcodeproj), and go to the bottom where it says "Linked Frameworks and Libraries." Click the plus button (circled in blue above), and you'll get a pop up.

Screen Shot 2014-03-24 at 6.13.02 PMType "Social" into the search field at the top of the slide down menu and then you can select the only framework with the word Social in its name, the aptly named Social.framework. After doing that, press Add and it will be added to your project and ready for you to implement. Now to the fun part, code!

header/******** 1 ********/

Just as in the previous tutorial, we are going to start in our .h file. Open up (if you're following my naming scheme still) HTViewController.m, and where we added "UITextFieldDelegate" last time, we need to add UIAlertViewDelegate. To do that, simply put a comma after UITextFieldDelegate and add UIAlertViewDelegate, staying inside the angle brackets. That's all you have to do in the header file, no outlets or actions for this one.

import Social

/******** 1 ********/

Next, at the very top of your HTViewController.m file, you are going to import the framework that we added earlier, Social.framework. In order to do this, though, you just have to import the actual file that we want to use, which is Social.h. User defined files are imported using quotes, however when you use a framework, you import using angle brackets, so you are going to add <Social/Social.h>. This will allow us to use the Social.framework to call Twitter and Facebook posting capabilities, explained later on.

pressButton/******** 1 ********/

For those of you following along from last time, you will recognize the code above from when we implemented the IBAction that was triggered when our user presses our "Press This!" button. Will, if you'll notice, there are a few changes that need to be made in order for this tutorial to work. What you are going to change is the delegate, which you will set to "self" (that's why we set our UIAlertViewDelegate in the .h), as well as adding two more buttons. In order to do this, after "otherButtonTitles" add @"Share to Twitter" and @"Share to Facebook". You need to have the @ and the quotes in order for this to work, and you also put "nil" (without quotes) after a comma following the @"Share to Facebook" part. Now we can get on to adding the functionality behind those buttons.

buttonAtIndex/******** 1 ********/

Now we need to add our method for getting what button was pressed and deciding what we want to do. For this, we are going to add a method called "-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex". Inside of this is where we will put the code for what we want to post on Twitter and Facebook.

/******** 2 ********/

Here we are going to just copy and paste the code from above where we have our NSStrings for textFieldText and alertMessage. However, inside of alertMessage we are going to make it say "Hello from %@'s iPhone app!" so that when you post it the users name will replace the %@. Simple enough, so now on to the actual posting part.

/******** 3 ********/

Here we are going to write our Twitter posting part of the application. When you press buttons on a UIAlertView, they are registered with a number, in this case called buttonIndex. So, in order to write the code for when a button is pressed, you write an if statement to find the buttonIndex and if it matches, you run what is inside; if it doesn't match, you move on to the next part. Our Twitter button is at the buttonIndex of 1, so write "if (buttonIndex ==1)" and inside of that put the Twitter code. We need to create a new instance of SLCompostViewController, and we will call it tweetSheet (to keep straight what is what). You need to create it and pass SLServiceTypeTwitter for the SLCompostViewController's composeViewControllerForServiceType method. After this, we type "[tweetSheet setInitialText:alertMessage];" so that our text is set as our alertMessage when the view pops up, then we present the controller with presentViewController:animated:completion.

/******* 4 ********/

Because this is almost the same as before, what I did is just copy and paste the code from our if for buttonIndex 1 and put it in our "else if (buttonIndex == 2)" statement. Then all you have to do is change the service type to SLServiceTypeFacebook and it will bring up the Facebook share sheet instead of Twitter, and it's that easy.

That is all you have to do, run the app and when you press the button after entering your name, you should see "Share to Twitter" and "Share to Facebook" buttons above the "Nice to meet you." button. Tap on one, and you'll be presented with a Facebook or Twitter share sheet that will post to the account you have set up inside of the iOS Settings app. If you haven't set up an account in Settings, it should prompt you that an account needs to be set up and you can either cancel or go to Settings to set up an account. This is all a part of the SLComposeViewController, with no extra work for you. If you want to download the source code for this tutorial, you can do so by grabbing it from GitHub here: HelloTiP. Make sure you are in the Tutorial-2 branch, not master.

Tell me what you think of the tutorial in the comments below, or tweet me suggestions for tutorial on Twitter @TiP_Kyle.

Add functionality to UIAlertView buttons and share to Twitter and Facebook [Code Tutorial] Rating: 4.5 Posted by: Unknown


Post a Comment