UIWebView 加载失败,自定义 URL 方案

Jam*_*est 5 iphone oauth objective-c uiwebview ios

我有一个 UIWebView 正在我的应用程序中处理 OAuth 身份验证流。OAuth 服务使用的回调 URL 指向我在我的应用程序中设置的自定义 URL 方案,我们称之为mycustomurlscheme.

因此,一旦用户身份验证成功,OAuth 服务会将他们重定向到 URL mycustomurlscheme://oauth/success?oauth_token=xxxxxxxxxxxxxxxxxxxx&oauth_verifier=xxxxxxxxxxxxxxxxxxxx。当 UIWebView 请求这个 URL 时,应用程序的行为是正确的;即,application:openURL:sourceApplication:annotation:我的应用程序委托中的方法被调用,然后我关闭 UIWebView 并继续使用该应用程序。

但是,我在 UIWebView 中记录加载失败(使用webView:didFailLoadWithError:),并且当上述过程发生时,我在控制台中看到以下内容:

Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted" UserInfo=0xa1a4810 {NSErrorFailingURLKey=mycustomurlscheme://oauth/success?oauth_token= xxxxxxxxxxxxxxxxxxxx&oauth_verifier=xxxxxxxxxxxxxxxxxxxx, NSErrorFailingURLStringKey=mycustomurlscheme://oauth/success?oauth_token= xxxxxxxxxxxxxxxxxxxx&oauth_verifier=xxxxxxxxxxxxxxxxxxxx, NSLocalizedDescription=Frame load interrupted}
Run Code Online (Sandbox Code Playgroud)

我尝试实现了下面的 UIWebViewDelegate 方法,认为拦截请求可以避免错误,但是错误仍然发生:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request
 navigationType:(UIWebViewNavigationType)navigationType
{
    if ([request.URL.scheme isEqualToString:@"mycustomurlscheme"]) {
        [[UIApplication sharedApplication] openURL:request.URL];
        return NO;
    } else {
        return YES;
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码然后导致我的应用程序委托中的以下代码运行:

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    NSNotification *notification = [NSNotification notificationWithName:kDSApplicationLaunchedWithURLNotification object:nil userInfo:@{kApplicationLaunchOptionsURLKey: url}];
    [[NSNotificationCenter defaultCenter] postNotification:notification];

    return YES;
} 
Run Code Online (Sandbox Code Playgroud)

此通知由以下块接收,该块继续加载和启动应用程序,授权后:

self.applicationLaunchNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kDSApplicationLaunchedWithURLNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
                    NSURL *url = [notification.userInfo valueForKey:kApplicationLaunchOptionsURLKey];
                    currentRequestToken.verifier = [DSAPIParametersFromQueryString([url query]) valueForKey:@"oauth_verifier"];

                    [[DSAPIManager sharedManager] acquireAccessTokenWithRequestToken:currentRequestToken success:^(DSAPIOAuth1Token * accessToken) {
                        [self finishLogin];
                    } failure:^(NSError *error) {
                        [self removeAsObserver];
                        [self showAlert:NSLocalizedString(@"Could not authorize the app. Please try again later.", nil)];
                    }];
                }];
Run Code Online (Sandbox Code Playgroud)

finishLoginremoveAsObserver方法是这样的:

- (void)finishLogin
{
    [self removeAsObserver];
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        [self performSegueWithIdentifier:@"PostLoginSegue" sender:self];
    }];
}

- (void)removeAsObserver
{
    if (self.applicationLaunchNotificationObserver) {
        [[NSNotificationCenter defaultCenter] removeObserver:self.applicationLaunchNotificationObserver];
        self.applicationLaunchNotificationObserver = nil;
    }
}
Run Code Online (Sandbox Code Playgroud)

忽略此错误消息是否安全?有谁知道为什么会这样?我的预感是 UIWebView 认为 URL 有一个无效的方案,但手机知道它没问题。如果是这种情况,有没有办法告诉 UIWebView(或 WebKit 引擎)这个方案是有效的?

Pop*_*eye 1

这里的错误实际上Frame load interrupted与 url 方案无关。使用自定义 url 方案是完全可能的,并且不会产生任何错误。我的许多应用程序中都有它们,并且从未遇到过问题。

我相信您的问题是因为从技术上讲,当您这样[webView stoploading];做时它会停止加载,因此只会导致过程中断。删除这一行并让它继续,看看会发生什么我 99.9% 确定这就是原因。return NO[webView stopLoading];return NO

所以你的代码看起来像

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([request.URL.scheme isEqualToString:@"mycustomurlscheme"]) {
        // Dismiss the view controller, continue loading the app, etc.
        return NO;
    } 

    return YES; // No need for the else statement if this is the only thing to happen if it fails the if statement.
}
Run Code Online (Sandbox Code Playgroud)

更新

基于唐的评论删除[webView stopLoading];并没有解决您的问题。如果是这种情况,并且您将其放入 if 语句中,我怀疑正在发生其他事情。你分享了你所有的代码吗?在您的代码中,// Dismiss the view controller, continue loading the app, etc.我怀疑您尚未共享所有内容,在这种情况下我们无法提供帮助。