了解 decidePolicyForNavigationResponse 与用户交互
Understanding decidePolicyForNavigationResponse with user interaction
我想在使用 WKWebView 打开 link 之前获得用户批准。 (Objective C) 我正在使用 decidePolicyForNaviagationResponse。
当它在 HTML 中遇到 link 时,它应该使用 UIAlertController 询问是否可以遵循 link(在最简单的实现中)。
但是它似乎是 运行 异步的,所以它首先打开 link 然后最终开始弹出警报。
我如何遇到 link,弹出警报然后打开 link 或不打开。我猜要么是关于块的一些我不理解的东西,比如完成处理程序,要么可能是使用信号量,尽管我对它们的适度尝试没有奏效。
我简化了代码以明确发生了什么。
谢谢!
static bool launchPermission = false;
@property (strong, nonatomic) WKWebViewConfiguration *theConfiguration;
@property (strong, nonatomic) WKWebView *webView;
.
.
.
_webView.navigationDelegate = self;
[_webView loadRequest:nsrequest];
[self.view addSubview:_webView];
.
.
.
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
[self askPermissionForExternalLink];
if (launchPermission)
{
decisionHandler(WKNavigationResponsePolicyAllow);
}
else
{
decisionHandler(WKNavigationResponsePolicyCancel);
}
}
- (void) askPermissionForExternalLink
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Open external Web Conten?" message:@"Link is embedded with other content" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
[self cancelMethod];
//return;
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
//[self launchURL];
[self OKMethod];
}];
[alert addAction:cancelAction];
[alert addAction:okAction];
[alert show];
}
- (bool) cancelMethod
{
launchPermission = false;
return false;
}
- (bool) OKMethod
{
launchPermission = true;
return true;
}
您可以先在 decidePolicyForNavigationResponse
中使用块以这种方式尝试在块内进行 decideAction
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
void (^ launchPermission)(BOOL) = ^(BOOL isAllow)
{
if(isAllow)
{
decisionHandler(WKNavigationActionPolicyAllow);
}
else
{
decisionHandler(WKNavigationActionPolicyCancel);
}
return;
};
[self askPermissionForExternalLink];
}
这里根据用户的选择发送 YES 或 NO 到 launchPermission 块
- (void) askPermissionForExternalLink
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Open external Web Conten?" message:@"Link is embedded with other content" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
launchPermission(NO);
return;
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
launchPermission(YES);
return ;
}];
[alert addAction:cancelAction];
[alert addAction:okAction];
[alert show];
}
我想在使用 WKWebView 打开 link 之前获得用户批准。 (Objective C) 我正在使用 decidePolicyForNaviagationResponse。
当它在 HTML 中遇到 link 时,它应该使用 UIAlertController 询问是否可以遵循 link(在最简单的实现中)。
但是它似乎是 运行 异步的,所以它首先打开 link 然后最终开始弹出警报。
我如何遇到 link,弹出警报然后打开 link 或不打开。我猜要么是关于块的一些我不理解的东西,比如完成处理程序,要么可能是使用信号量,尽管我对它们的适度尝试没有奏效。
我简化了代码以明确发生了什么。
谢谢!
static bool launchPermission = false;
@property (strong, nonatomic) WKWebViewConfiguration *theConfiguration;
@property (strong, nonatomic) WKWebView *webView;
.
.
.
_webView.navigationDelegate = self;
[_webView loadRequest:nsrequest];
[self.view addSubview:_webView];
.
.
.
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
[self askPermissionForExternalLink];
if (launchPermission)
{
decisionHandler(WKNavigationResponsePolicyAllow);
}
else
{
decisionHandler(WKNavigationResponsePolicyCancel);
}
}
- (void) askPermissionForExternalLink
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Open external Web Conten?" message:@"Link is embedded with other content" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
[self cancelMethod];
//return;
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
//[self launchURL];
[self OKMethod];
}];
[alert addAction:cancelAction];
[alert addAction:okAction];
[alert show];
}
- (bool) cancelMethod
{
launchPermission = false;
return false;
}
- (bool) OKMethod
{
launchPermission = true;
return true;
}
您可以先在 decidePolicyForNavigationResponse
中使用块以这种方式尝试在块内进行 decideAction
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
void (^ launchPermission)(BOOL) = ^(BOOL isAllow)
{
if(isAllow)
{
decisionHandler(WKNavigationActionPolicyAllow);
}
else
{
decisionHandler(WKNavigationActionPolicyCancel);
}
return;
};
[self askPermissionForExternalLink];
}
这里根据用户的选择发送 YES 或 NO 到 launchPermission 块
- (void) askPermissionForExternalLink
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Open external Web Conten?" message:@"Link is embedded with other content" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
launchPermission(NO);
return;
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
launchPermission(YES);
return ;
}];
[alert addAction:cancelAction];
[alert addAction:okAction];
[alert show];
}