iOS Twitter Reverse Auth Example and Tutorial

Hi, today’s tutorial will be based on @theSeanCook project, that shows how to use Twitter Reverse Authorization.  You can find the original project here.

The project is moderately big, hovever it takes some time to realize what is happening. In today’s tutorial, we will create single, reusable class implementing Twitter Reverse Authorization. The purpose is to have access to Twitter Reverse Auth methods all across the application.

Normally you would create some singleton and work with it, but the point of this post is not to teach you how to create singletons, so for the simplicity sake, we will use `AppDelegate.h/m` which is easily accessible from all over the application.

All you have to do is the following:

1. Open yours and Sean Cook’s project (the one which URL is above)

2. Drag and copy Source->Vendor->ABOauthCore group into your project

3. Select `TWAPIManager.h/m`, `TWSignedRequest.h/m` and copy them into your project

4. Add the below code into your AppDelegate.h file

@property (nonatomic, strong) ACAccountStore* store;
@property (nonatomic, strong) TWAPIManager *apiManager;
@property (nonatomic, strong) NSArray *accounts;
-(void)storeAccountWithAccessToken:(NSString *)token secret:(NSString *)secret;
-(void)performReverseAuth:(id)sender inView:(UIView*)viewToDisplaySheet;
-(void)_refreshTwitterAccounts;

5. Now paste the following methods into your `AppDelegate.m` file

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
-(void)_refreshTwitterAccounts;
-(void)_obtainAccessToAccountsWithBlock:(void (^)(BOOL))block;
-(void)performReverseAuth:(id)sender inView:(UIView*)viewToDisplaySheet;

6. In some initialization method of your file, or as of this example in: `application: didFinishLaunchingWithOptions’ paste the following code:

_store = [[ACAccountStore alloc] init];
_apiManager = [[TWAPIManager alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_refreshTwitterAccounts) name:ACAccountStoreDidChangeNotification object:nil];

7. Remember to remove observer using the following code. Paste it in `AppDelegate.m`:

-(void)dealloc{

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

8. Open your `app-Info.plist` file and add 2 string keys. Take their values from your twitter applications

TWITTER_CONSUMER_KEY

TWITTER_CONSUMER_SECRET

9. In the View Controller that you want to use to implement twitter features, in the `viewDidLoad` method, add the following code:

AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate _refreshTwitterAccounts];

10. OK, finally you are ready to start the whole machine. In the View Controller that you want to use to implement twitter features, create `UIButton` called `_reverseAuthBtn` and create an `IBAction` to it. Then in your `IBAction` paste the following code:

AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate performReverseAuth:sender inView:self.view];

Whew, I guess that’s it! If I haven’t forgotten anything, you have got Twitter Reverse Oauth implementation, and if you want to use it in multiple view controllers, all you have to do is:

finish steps 1-8,

and then paste the code from the steps 9 and 10 into your view controller.

Best regards!