SVShareViewController is a simple compose UI for posting textual content to 3rd party services like Facebook and Twitter. It doesn’t include the network classes that will make the actual API calls. For that, you’ll have to work with the Facebook iOS SDK and MGTwitterEngine.
- Drag the
SVShareViewController/SVShareViewController
folder into your project. - Add the QuartzCore framework to your project.
SVShareViewController currently supports 2 services: Facebook (SVShareTypeFacebook
) and Twitter (SVShareTypeTwitter
). You specify the used service via its initWithShareType:
method. You also need to set its delegate (that handles the sending of messages and user signout). Finally, it is recommended that you set the userString
(usually retrieved from initial sign in and then stored locally — more on that later) and defaultMessage
properties. The following code snippet is the typical way to setup and present an SVShareViewController instance:
SVShareViewController *fbViewController = [[SVShareViewController alloc] initWithShareType:SVShareTypeFacebook]; fbViewController.delegate = self; fbViewController.userString = @"John Smith"; fbViewController.defaultMessage = @"Some stock text about the shared item."; [self presentModalViewController:fbViewController animated:YES]; [fbViewController release];
SVShareViewController provides UI controls for 2 actions: sending the message and logging out. You are required to adopt both delegate methods (presumably inside the view controller that presents the SVShareViewController instance):
- (void)shareViewController:(SVShareViewController*)controller sendMessage:(NSString*)string forService:(SVShareType)shareType;
This is where you make the API calls using either the Facebook iOS SDK or MGTwitterEngine (or any other API wrapper of your chosing), depending on the current shareType
. Other typical tasks to be executed from within this delegate method:
- Show a progress HUD to let the user know what’s going on (cough SVProgressHUD cough)
- Append a link to the tweet or the facebook post
- Validate the user input (for instance make sure the string doesn’t exceed 140 characters for tweets)
- Dismiss the SVShareViewController instance (on request success)
- (void)shareViewController:(SVShareViewController*)controller logoutFromService:(SVShareType)shareType;
Although SVShareViewController doesn’t provide a sign in mechanism, it provides a “Logout” button along with a corresponding delegate method. The latter typically involves 2 required actions:
- Make the appropriate API calls that make the session tokens invalid
- Clear locally stored session variables (in
NSUserDefaults
for instance)
To make SVShareViewController more flexible, I decided not to include Facebook and Twitter API wrappers into this project. That being said, here are some rough walkthroughs of how they should be interacting with SVShareViewController:
The official Facebook iOS SDK is the only way to integrate Facebook into your app. The class at its core is Facebook
, and it has a isSessionValid
method to check whether the user is already signed in. When that method returns NO
, user is presented with a modal webview that prompts him for credentials. On successful login, the fbDidLogin
delegate method gets called. This is where you should:
- make an API call to Facebook to retrieve the user’s fullname and store it locally;
- grab the
accessToken
and theexpirationDate
properties from yourFacebook
instance and store them locally, that way the user stays signed in for as long as the expirationDate isn’t reached (make sure you assign them back everytime you create a newFacebook
instance though) - present your
SVShareViewController
instance, after setting itsuserString
property using the previously stored name value.
Twitter is much more flexible in terms of sign in and API wrappers. For signing in, there are many web-based solutions out there, which ask for Twitter credentials and then requires the user to copy/paste an access pin. This is terrible UX. To allow native sign in and most importantly not ask the user to copy paste any bit of information, you’ll need to enable XAuth access for your app. Once that’s done, you can then use MGTwitterEngine to make both the authentification and other API calls. On successful sign in, the accessTokenReceived:forRequest:
will be called. This is where you should:
- make an API call to Twitter to retrieve the username (using MGTwitterEngine’s
checkUserCredentials
method) and store it locally; - grab the (OAuth)
key
andsecret
from the returnedOAToken
and store them locally, that way the user stays signed in for as long they’re valid (make sure you assign them through theaccessToken
property everytime you create a newMGTwitterEngine
instance though) - present your
SVShareViewController
instance, after setting itsuserString
property using the previously stored name value.