crino / instagram-ios-sdk

Instagram SDK for iOS
http://www.followgram.me
228 stars 63 forks source link

Login / Logout using UIWebView Modal View Controller #34

Closed RyanTLX closed 10 years ago

RyanTLX commented 10 years ago

Hi, can I have an example on how to login and logout using a UIWebView in a ModalViewController please?

I also wish to know how can I create an "instagram" instance in another class, which I use as my AppDirector class, rather than having to do it in the app delegate. (The AppDirector class is my initial view controller, which is also the rootViewController of my side menu system).

I have tried my best to edit the codes myself but I kept failing. Can I get some help? Thanks so much in advance for this library and your help!

RyanTLX commented 10 years ago

I managed to figure out a way to achieve the results I wanted.

I have a LoginViewController. Inside that controller, I have a UIWebView that will load the Instagram login page.

Here's the the login button's method:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self authorizeWithSafariWithScopes:@[@"comments", @"likes"]];
}

That button calls the following method which I extracted and edited from the Instagram.m class.

- (void)authorizeWithSafariWithScopes:(NSArray *)scopes
{
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   *CLIENT_ID*, @"client_id",
                                   @"token", @"response_type",
                                   [NSString stringWithFormat:@"ig%@://authorize", *CLIENT_ID*], @"redirect_uri",
                                   nil];

    NSString *loginDialogURL    = [@"https://instagram.com/" stringByAppendingString:@"oauth/authorize"];

    if (scopes != nil) {
        NSString* scope = [scopes componentsJoinedByString:@"+"];
        [params setValue:scope forKey:@"scope"];
    }

    NSString *igAppUrl          = [IGRequest serializeURL:loginDialogURL params:params];
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:igAppUrl]];
    [self.webView loadRequest:urlRequest];
}

Remember to set your UIWebView's delegate to self and implement the following method:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"Url: %@", request.URL.absoluteString);
    if ([request.URL.absoluteString rangeOfString:@"#"].location != NSNotFound)
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }

    return YES;
}

And you're done!

The RootViewController is the controller that I will be showing the feed. So that controller has the IGSessionDelegate and IGRequestDelegate protocol and methods implemented. Examples of these methods can be found in the SDK's example app but here's how I did it.

-(void)igDidLogin
{
    NSLog(@"Instagram did login");

    // Here i can store accessToken
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [[NSUserDefaults standardUserDefaults] setObject:appDelegate.instagram.accessToken forKey:@"accessToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    [self requestForData];
}
- (void)requestForData
{
    NSLog(@"PhotoFeed requestForData");

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[AppDirector Director] feedType], @"method", nil];
    [appDelegate.instagram requestWithParams:params delegate:self];

    [self.collectionView reloadData];
}
- (void)request:(IGRequest *)request didLoad:(id)result
{
    // Manipulate the retrieved data here.
}

Logout works as expected too with the following code as seen from the example app.

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.instagram logout];

Hope this helps anybody that were in my shoes! Cheers!

crino commented 10 years ago

Thanks @RyanTLX for share your code.

I'm thinking to add a new authorize method with the follow sign: -(void)authorize:(NSArray)scopes inWebView:(UIWebView webView);