使用UIWebView在iOS应用中登录Instagram时出错 [英] Error logging into Instagram in iOS app using UIWebView
问题描述
我在iOS应用上使用UIWebView跟踪
还有另一个问题这里并没有帮助。
编辑:
它好像是Cookies问题。虽然,我还没能解决它。
我设法在一小时前解决了这个问题。
首先,不要使用UIWebView或WebView;请改用 WKWebView 。
你看,你需要实现方法
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)导航withError:(NSError *)错误
当instagram的登录页面转发你的redirect_url时,它无法导航。可能是因为我们没有进行服务器端身份验证,只是客户端身份验证,而redirect_url不是有效的URL。
我们期望页面重定向到无效的URL,但是当发生这种情况时,WKWebView不会调用方法
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
或方法
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
所以。你需要做的是专注于第一种方法,检查错误
变量,并提取 error.userInfo
。
就我而言,这就是我解决问题的方法:
- (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];
//打开下一个视图控制器
} else {
// TODO:没有互联网?出了别的问题..
NSLog(@错误:%@,错误);
}
}
希望有所帮助。
好运! ;)
I'm following Instagram authentication [recommended] steps using UIWebView on an iOS app. After entering credentials, hitting login loads a page with following error.
This page could not be loaded. If you have cookies disabled in your browser, or you are browsing in private mode, please try enabling cookies or turning off private mode, and then retrying your action.
And, this only happens on first run through the steps of authentication; on the next attempt, everything works smooth as silk. I get the code suffixed to redirect url and I request access token using it.
Screenshot:
There's already another question here and it doesn't help.
EDIT: It seems like Cookies issue. Though, I haven't been able to fix it yet.
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! ;)
这篇关于使用UIWebView在iOS应用中登录Instagram时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!