Last active
January 1, 2016 23:08
-
-
Save aug2uag/8214251 to your computer and use it in GitHub Desktop.
Meetup API Login iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "LoginViewController.h" | |
| #import "Global.h" | |
| #import "MeetupTopic.h" | |
| #import "UserAuthenticated.h" | |
| @interface LoginViewController () <UIWebViewDelegate> | |
| @property (strong, nonatomic) UIWebView* webView; | |
| @property (strong, nonatomic) NSNumber* userId; | |
| @end | |
| @implementation LoginViewController | |
| - (void)loadView | |
| { | |
| [super loadView]; | |
| CGRect meetupButton = CGRectMake(10, 110, 300, 44); | |
| UIButton* meetup = [UIButton buttonWithType:UIButtonTypeRoundedRect]; | |
| meetup.backgroundColor = [UIColor clearColor]; | |
| [meetup setTitle:@"MEETUP" forState:UIControlStateNormal]; | |
| [meetup addTarget:self action:@selector(goMeetup) forControlEvents:UIControlEventTouchUpInside]; | |
| meetup.frame = meetupButton; | |
| [self.view addSubview:meetup]; | |
| UIButton* exit = [UIButton buttonWithType:UIButtonTypeRoundedRect]; | |
| exit.backgroundColor = [UIColor clearColor]; | |
| [exit setTitle:@"X" forState:UIControlStateNormal]; | |
| [exit addTarget:self action:@selector(exit) forControlEvents:UIControlEventTouchUpInside]; | |
| exit.frame = CGRectMake(20, 20, 44, 44); | |
| [self.view addSubview:exit]; | |
| _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height)]; | |
| _webView.delegate = self; | |
| [self.view addSubview:_webView]; | |
| } | |
| - (void)goMeetup | |
| { | |
| NSString* scope = [NSString stringWithFormat:@"https://secure.meetup.com/oauth2/authorize?client_id=%@&response_type=code&redirect_uri=%@", MEETUP_CLIENTID, @"http://www.filmproj.com"]; | |
| CGRect newRect = self.view.bounds; | |
| [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:scope]]]; | |
| [UIView animateWithDuration:0.3f animations:^{ | |
| _webView.frame = newRect; | |
| }]; | |
| } | |
| - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType | |
| { | |
| NSLog(@"request url lastpathComponent = %@", request.URL.lastPathComponent); | |
| NSString *html = [webView stringByEvaluatingJavaScriptFromString: | |
| @"document.body.innerHTML"]; | |
| NSString* part = [self scanString:html startTag:@"MM.Member={" endTag:@"</script>"]; | |
| NSString* anId = [self scanString:part startTag:@"id:" endTag:@","]; | |
| NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; | |
| [f setNumberStyle:NSNumberFormatterDecimalStyle]; | |
| self.userId = [f numberFromString:anId]; | |
| if (self.userId) { | |
| [self userApi]; | |
| } else { | |
| [self alertError]; | |
| } | |
| return YES; | |
| } | |
| - (NSString *)scanString:(NSString *)string startTag:(NSString *)startTag endTag:(NSString *)endTag | |
| { | |
| NSString* scanString = @""; | |
| if (string.length > 0) { | |
| NSScanner* scanner = [[NSScanner alloc] initWithString:string]; | |
| [scanner scanUpToString:startTag intoString:nil]; | |
| scanner.scanLocation += [startTag length]; | |
| [scanner scanUpToString:endTag intoString:&scanString]; | |
| } | |
| return scanString; | |
| } | |
| - (void)userApi | |
| { | |
| NSString* urlString = [NSString stringWithFormat:@"https://api.meetup.com/members?member_id=%@&key=%@", self.userId, MEETUP_KEY]; | |
| [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { | |
| if (data) { | |
| NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; | |
| //NSLog(@"dictionary = %@", dictionary); | |
| if (dictionary) { | |
| NSDictionary* userResponse = [dictionary valueForKey:@"results"][0]; | |
| //NSLog(@"userResponse = %@", userResponse); | |
| if (userResponse) { | |
| NSString* city = [userResponse valueForKey:@"city"]; | |
| NSString* country = [userResponse valueForKey:@"country"]; | |
| NSString* name = [userResponse valueForKey:@"name"]; | |
| NSString* state = [userResponse valueForKey:@"state"]; | |
| NSString* avatarUrl = [userResponse valueForKey:@"photo_url"]; | |
| NSArray* topicsArray = [userResponse valueForKey:@"topics"]; | |
| NSMutableArray* topicsArrayHolder = [NSMutableArray array]; | |
| for (NSDictionary* json in topicsArray) { | |
| NSLog(@"topicsArray = %@", topicsArray); | |
| NSNumber* jsonId = [json valueForKey:@"id"]; | |
| NSString* jsonName = [json valueForKey:@"name"]; | |
| NSString* jsonUrlKey = [json valueForKey:@"urlKey"]; | |
| MeetupTopic* topic = [[MeetupTopic alloc] initWithTopicId:jsonId name:jsonName urlKey:jsonUrlKey]; | |
| [topicsArrayHolder addObject:topic]; | |
| } | |
| [[UserAuthenticated sharedInstance] makeUserWithId:self.userId username:name city:city state:state country:country imageUrl:avatarUrl andTopics:topicsArrayHolder]; | |
| [topicsArrayHolder removeAllObjects]; | |
| // tests for user authenticated object | |
| NSNumber* user0 = [[UserAuthenticated sharedInstance] userId]; | |
| NSString* user1 = [[UserAuthenticated sharedInstance] userName]; | |
| NSString* user2 = [[UserAuthenticated sharedInstance] city]; | |
| NSString* user3 = [[UserAuthenticated sharedInstance] state]; | |
| NSString* user4 = [[UserAuthenticated sharedInstance] country]; | |
| NSArray* user5 = [[UserAuthenticated sharedInstance] topicsArray]; | |
| UIImage* user6 = [[UserAuthenticated sharedInstance] avatar]; | |
| NSLog(@"%@\n%@\n%@\n%@\n%@\n%@\n%@", user0, user1, user2, user3, user4, user5, user6); | |
| // dismiss controller | |
| // update ui on other pages, and enable features | |
| } else { | |
| dispatch_async(dispatch_get_main_queue(), ^{ | |
| [self alertError]; | |
| }); | |
| } | |
| } else { | |
| dispatch_async(dispatch_get_main_queue(), ^{ | |
| [self alertError]; | |
| }); | |
| } | |
| } else { | |
| dispatch_async(dispatch_get_main_queue(), ^{ | |
| [self alertError]; | |
| }); | |
| } | |
| }]; | |
| } | |
| - (void)alertError | |
| { | |
| [[[UIAlertView alloc] initWithTitle:nil message:@"err, if it's not you it's me" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment