Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什么不需要处理didFailProvisionalNavigation代理方法 #52

Closed
dourgulf opened this issue Sep 19, 2016 · 14 comments
Closed

为什么不需要处理didFailProvisionalNavigation代理方法 #52

dourgulf opened this issue Sep 19, 2016 · 14 comments

Comments

@dourgulf
Copy link

public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {

    // Pocket OAuth
    if let errorString = (error as NSError).userInfo["NSErrorFailingURLStringKey"] as? String, errorString.hasSuffix(":authorizationFinished") {
        removeWebView(webView, tuples: (nil, nil, nil))
    }
}

这里为什么不需要做removeWebview的处理的?
对WKWebview的这个回调的机制还不是特别清楚. 请教作者?

@dourgulf
Copy link
Author

我遇到的问题是在微博Web方式授权的情况下, 授权成功之后会跳转到redirectURI, 这个地址经常是跳转失败就进入这个回调了.
我看你的在didReceiveServerRedirectForProvisionalNavigation的处理是调用了webview.stopLoading(). 但是, 看样子并不能阻止webview加载这个redirectURI?

@Limon-O-O
Copy link
Collaborator

Limon-O-O commented Sep 19, 2016

这里是 Pocket 的授权,不需要理会。
是我公司的项目有需求要分享到Pocket

@Limon-O-O
Copy link
Collaborator

你测试一下,stopLoading之后会不会调用其它的代理方法

@dourgulf
Copy link
Author

我的意思是这里的失败消息难道不应该处理关闭, 并返回错误吗? 主页面加载失败(各种网络原因),不是走这里报错的吗?

@dourgulf
Copy link
Author

并且在didReceiveServerRedirectForProvisionalNavigation里面调用stopLoading并不能阻止WebView跳转. 看了一下WKWebview的代理方法, 貌似应该在decidePolicyForNavigationAction处理才是.

@dourgulf
Copy link
Author

  • (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
    {
    // 微博在授权完毕之后会跳转到指定的redirectURI, 我们在这里扑捉这个跳转, 并禁止它
    // 然后, 单独发起一个获取access_token的请求来获得用户的uid信息
    NSString *address = [navigationAction.request.URL absoluteString];
    if (self.finishRedirectURI.length > 0 && [address hasPrefix:self.finishRedirectURI]) {

    NSDictionary *codeParams = [self extractParametersOfURI:navigationAction.request.URL];
    NSString *code = codeParams[@"code"];
    if (code.length > 0) {
        NSMutableString *userInfoAPI = [NSMutableString stringWithString:@"https://api.weibo.com/oauth2/access_token?"];
        [userInfoAPI appendFormat:@"code=%@", code];
        [userInfoAPI appendFormat:@"&client_id=%@", self.loadingParameters[@"client_id"]];
        [userInfoAPI appendFormat:@"&client_secret=%@", self.loadingParameters[@"client_secret"]];
        [userInfoAPI appendFormat:@"&redirect_uri=%@", self.loadingParameters[@"redirect_uri"]];
        [userInfoAPI appendString:@"&grant_type=authorization_code"];
        NSLog(@"redirect %@", userInfoAPI);
        [self postRequestURL:[NSURL URLWithString:userInfoAPI] completion:^(NSDictionary *info, NSError *err) {
            NSLog(@"completion info:%@", info);
            [self closeAuthviewWithResult:info error:err];
        }];
    }
    else {
        [self closeAuthviewWithResult:nil error:nil];
    }
    decisionHandler(WKNavigationActionPolicyCancel);
    

    }
    decisionHandler(WKNavigationActionPolicyAllow);
    }

我把授权完成的控制放在这个回调函数里处理了

@Limon-O-O
Copy link
Collaborator

Limon-O-O commented Sep 19, 2016

WKWebview加载失败这些情况,我没做处理,因为下方的关闭按钮会一直存在,加载失败了,用户会点击关闭,加载失败的机率毕竟太少了(逃...

@dourgulf
Copy link
Author

嗯, 但是, 关闭按钮也是依赖加载成功之后才添加上去的.(
加载成功才走的这个回调吧?

public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
)
我测试偶尔遇到过一次:(

@dourgulf
Copy link
Author

遇到就一直白屏幕, 转圈圈..., 实际上已经进了失败的回调了

@Limon-O-O
Copy link
Collaborator

对,应该我写的逻辑还不够完善

@Limon-O-O
Copy link
Collaborator

你可以先按你的逻辑写完,测试完,我们可以再一起探讨探讨

@dourgulf
Copy link
Author

好呢, 我调试完, 应该会提交给OpenShare, 到时候请你一起review一下.
BTW:你用什么客户端, 为啥总是如此快的反馈?

@Limon-O-O
Copy link
Collaborator

没用客户端,我是收到 Email,在网页操作

@dourgulf
Copy link
Author

O~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants