SFSafariViewController不会重新打开webview内联网址链接

时间:2016-06-04 17:30:18

标签: ios objective-c webview sfsafariviewcontroller

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

-(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重新加载,但没有成功。

你可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分: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!!!");
}