I\'m following Instagram authentication [recommended] steps using UIWebView on an iOS app. After entering credentials, hitting login loads a page with following error.
I had a similar problem when I was deleting cookies to make sure the log in screen appeared and not just using the currently logged in user. Try (swift):
let storage = HTTPCookieStorage.shared
storage.cookieAcceptPolicy = .always
I managed to solve this problem about an hour ago.
First, DON'T use UIWebView or WebView; use a WKWebView instead.
You see, you need to implement the method
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
when instagram's login page forwards you to your redirect_url , it FAILS to navigate. Probably because we're not doing the server-side auth, but only the client-side auth, and the redirect_url is not a valid url. We're expecting the page to REDIRECT to an invalid url, but when that happens, the WKWebView doesn't call the method
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
or the method
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
So. What you need to do is focus on that first method, check the error
variable, and extract error.userInfo
.
In my case, this is how I solved the problem:
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
NSURL *err = [error.userInfo objectForKey:@"NSErrorFailingURLKey"];
if([err.absoluteString hasPrefix:INSTA_REDIRECT_URL]){
NSString *token = [[err.absoluteString componentsSeparatedByString:@"#access_token="] objectAtIndex:1];
[Utils saveToken:token];
[self.webView setHidden:YES];
//open next view controller
}else{
//TODO: No internet? something else went wrong..
NSLog(@"Error: %@", error);
}
}
Hope it helps. Best of luck! ;)