Search code examples
iphoneiosobjective-cfoursquare

Foursquare API UIWebView


As suggested here "https://developer.foursquare.com/overview/auth" I get the access token in a webview and then I close the browser by after getting the accesstoken:

- (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.webView removeFromSuperview];

    }

}

a similar example "https://github.com/anoopr/core-data-talk/blob/master/example/Classes/FoursquareAuthViewController.m"

But once I remove the webview, I get a dark screen. Which makes sense because I dont have a view anymore. But i probably should do something like [self.view addSubview:self.Anotherview], I am not sure what that Anotherview should be. Can I just segue to another ViewController?

I am pretty new to IOS so any help would be much appreciated.


Solution

  • You can put whatever you want after removing the UIWebView from the view. For example, if you just want to display a map, you can add a map view, or even easier, you can have a map view and a web view on top if you are using the interface builder, and removing the web view will leave you with the map view. But if what you have on the web view's view controller (the view controller that you've put the web view) is irrelevant of what you are trying to achieve after you get the access token, the cleanest way is just storing the access token somewhere else (e.g. App delegate class) performing a segue to another view controller, and using the token from there for whatever you need. You don't even need to deal with removing the web view from superview, you are already leaving that view controller and segueing to another one. Especially, when you are a beginner, as long as you can use storyboards, segues, and NIBs, go with them, try to do stuff in Interface Builder visually rather than trying to remove/add views/viewcontrollers (yes, it is also possible) within the code.