Integrating Instagram SDK in your iOS apps

Click here to find out how to schedule an event in less than 2minutes and save time !!!!
Lets get straight to the point. Unlike facebook, Instagram doesn’t have an SDK specifically tailored for iOS. We need to use https requests to authorize the user. This is pretty easy if your doing it on the web. But if you are going to do it using iOS, its not straight forward. First things first ,
a. Goto Instagram and register yourself and your client. http://instagram.com/developer/clients/manage/
b. You would need the following:
CLIENT ID df2esdfasdfdsfasdfadsfadsfadsf34c27
CLIENT SECRET 357dsasdfadsfasdfadsfasdfb96b0313
WEBSITE URL http://localhost:8888/MAMP/
REDIRECT URI http://localhost:8888/MAMP/
c. I don’t want to elaborate much on this topic. Its all specified in the Instagram website.
d. Lets get to the iOS part now. You would need to UIWebview to get the job done.
[sourcecode language=”objc”]
@interface viewController : UIViewController<UIWebViewDelegate> // need this delegate here
[/sourcecode]
[sourcecode language=”objc”]
– (void)viewDidLoad
{
[super viewDidLoad];
// create a webview
UIWebView* mywebview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSString *fullURL = @"https://instagram.com/oauth/authorize/?client_id=df2esdfasdfdsfasdfadsfadsfadsf34c27&redirect_uri=http://localhost:8888/MAMP/&response_type=token";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[mywebview loadRequest:requestObj];
mywebview.delegate = self;
[self.view addSubview:mywebview];
}
[/sourcecode]
If you run the above code, you will get the authorization page as shown below:
Next lets get to the crux of this post. Getting the access code, which what we want for getting the instagram photos.
[sourcecode language=”objc”]
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString* urlString = [[request URL] absoluteString];
NSURL *Url = [request URL];
NSArray *UrlParts = [Url pathComponents];
// do any of the following here
if ([[UrlParts objectAtIndex:(1)] isEqualToString:@"MAMP"]) {
//if ([urlString hasPrefix: @"localhost"]) {
NSRange tokenParam = [urlString rangeOfString: @"access_token="];
if (tokenParam.location != NSNotFound) {
NSString* token = [urlString substringFromIndex: NSMaxRange(tokenParam)];
// If there are more args, don’t include them in the token:
NSRange endRange = [token rangeOfString: @"&"];
if (endRange.location != NSNotFound)
token = [token substringToIndex: endRange.location];
NSLog(@"access token %@", token);
if ([token length] > 0 ) {
// display the photos here
instagramTableViewController *iController = [[instagramPhotosTableViewController alloc] initWithStyle:UITableViewStylePlain];
NSString* redirectUrl = [[NSString alloc] initWithFormat:@"https://api.instagram.com/v1/users/self/feed?access_token=%@", token];
}
// use delegate if you want
//[self.delegate instagramLoginSucceededWithToken: token];
}
else {
// Handle the access rejected case here.
NSLog(@"rejected case, user denied request");
}
return NO;
}
return YES;
}
[/sourcecode]
The above code should be able to get you started.

In

6 responses

  1. Working with the Instagram API in iOS is much more simpler now. Check out this new SDK http://github.com/shyambhat/InstagramKit

  2. All Followers get EmaiAddress ? it is possible ?
    yes
    HOw it
    Thanks andvance

    1. kmdarshan Avatar
      kmdarshan

      can you explain more in detail??

  3. Sorry, it’s been late but i need your help. I have register my app for using secure api to get the feature like,comment, relationship etc. But can’t understand how much time it needs to review app. Please help.

    1. kmdarshan Avatar
      kmdarshan

      for getting instagram access, you get it as soon as possible.

  4. But my profile says, i have the access.

Leave a Reply

Your email address will not be published. Required fields are marked *