0

我有一个使用 SFSafariViewController 打开的带有内联 URL 链接的 Webview,如下所示:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
    if ( inType == UIWebViewNavigationTypeLinkClicked ) {
        if ([SFSafariViewController class] != nil) {
            NSString *inR = [[inRequest URL] absoluteString];
            NSURL *inReq = [NSURL URLWithString:inR];
            SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:inReq entersReaderIfAvailable:YES];
            safariVC.delegate = self;
            [self presentViewController:safariVC animated:YES completion:nil];
        } else {
            [[UIApplication sharedApplication] openURL:[inRequest URL]];
            return NO;
        }
    }
    return YES;
}

#pragma mark - SFSafariViewController delegate methods
-(void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
    // Load finished

}

-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
    // Done button pressed
    NSLog(@"DONE PRESSED!!!");
}

当我按 DONE 按钮正确返回到我的 Webview。问题是,如果我再次按下同一个内联链接,它不会使用 SFSafariViewController 打开,而是在 Webview 中打开,这不是我想要的。我试图在 safariViewControllerDidFinish 中强制 Webview 重新加载,但没有成功。

能否请你帮忙?谢谢!

4

1 回答 1

0

代码更正如下(遵循beyowulf的建议),现在工作正常:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
    if ( inType == UIWebViewNavigationTypeLinkClicked ) {
        if ([SFSafariViewController class] != nil) {
            NSString *inR = [[inRequest URL] absoluteString];
            NSURL *inReq = [NSURL URLWithString:inR];
            SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:inReq entersReaderIfAvailable:YES];
            safariVC.delegate = self;
            [self presentViewController:safariVC animated:YES completion:nil];
            return NO;
        } else {
            [[UIApplication sharedApplication] openURL:[inRequest URL]];
            return NO;
        }
    } else {
        return YES;
    }
}

#pragma mark - SFSafariViewController delegate methods
-(void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
    // Load finished
}

-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
    // Done button pressed
    NSLog(@"DONE PRESSED!!!");
}
于 2016-06-04T19:59:30.787 回答