Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
91 lines (73 sloc) 2.633 kb
//
// FoursquareAuthViewController.m
// CoreDataTalk
//
// Created by Anoop Ranganath on 2/19/11.
// Copyright 2011 foursquare. All rights reserved.
//
#import "FoursquareAuthViewController.h"
#import "Constants.h"
@interface FoursquareAuthViewController()
@property (nonatomic, readwrite, retain) UIWebView *webView;
@end
@implementation FoursquareAuthViewController
@synthesize webView = _webView;
- (id)init
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView = [[[UIWebView alloc] initWithFrame:self.view.bounds] autorelease];
self.webView.delegate = self;
NSString *authenticateURLString = [NSString stringWithFormat:@"https://foursquare.com/oauth2/authenticate?client_id=%@&response_type=token&redirect_uri=%@", CLIENT_ID, CALLBACK_URL];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:authenticateURLString]];
[self.webView loadRequest:request];
[self.view addSubview:self.webView];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.webView = nil;
}
#pragma mark - Web view delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([request.URL.scheme isEqualToString:@"itms-apps"]) {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *URLString = [[self.webView.request URL] absoluteString];
NSLog(@"--> %@", URLString);
if ([URLString rangeOfString:@"access_token="].location != NSNotFound) {
NSString *accessToken = [[URLString componentsSeparatedByString:@"="] lastObject];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:accessToken forKey:@"access_token"];
[defaults synchronize];
[self dismissModalViewControllerAnimated:YES];
}
}
@end
Jump to Line
Something went wrong with that request. Please try again.