Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 通过用户交互理解decidePolicyForNavigationResponse_Objective C_Wkwebview_Uialertcontroller - Fatal编程技术网

Objective c 通过用户交互理解decidePolicyForNavigationResponse

Objective c 通过用户交互理解decidePolicyForNavigationResponse,objective-c,wkwebview,uialertcontroller,Objective C,Wkwebview,Uialertcontroller,我希望在使用WKWebView打开链接之前获得用户批准。(目标C)我正在使用DecidepolicyFornavigationResponse 当它遇到HTML中的链接时,应该使用UIAlertController询问是否可以跟随该链接(在最简单的实现中) 但是,它似乎是异步运行的,所以它首先打开链接,然后最终会弹出警报 我如何遇到链接,弹出警报,然后打开或不打开链接。我猜测关于块的一些我不理解的东西,比如完成处理程序,或者可能使用信号量,尽管我对它们的适度尝试不起作用 我简化了代码,以清楚地了

我希望在使用WKWebView打开链接之前获得用户批准。(目标C)我正在使用DecidepolicyFornavigationResponse

当它遇到HTML中的链接时,应该使用UIAlertController询问是否可以跟随该链接(在最简单的实现中)

但是,它似乎是异步运行的,所以它首先打开链接,然后最终会弹出警报

我如何遇到链接,弹出警报,然后打开或不打开链接。我猜测关于块的一些我不理解的东西,比如完成处理程序,或者可能使用信号量,尽管我对它们的适度尝试不起作用

我简化了代码,以清楚地了解发生了什么

谢谢大家!

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
make 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];

    }

此处根据用户选择发送是或否以启动权限块

- (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];
}

您可以在
decidePolicyForNavigationResponse
make 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];

    }

此处根据用户选择发送是或否以启动权限块

- (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];
}

我会给你一个机会,让你知道,谢谢你。谢谢你这个工作!不过我有一个语法问题,因为有一件事我不明白。如何将launchpermission()放入要在-(void)AskPermissionForExternalink中使用的正确范围?我必须将来自AskPermMisionForExternalink的代码嵌入到webView代码中(抱歉,我不知道如何将整个代码发布到这个简短的注释字段中),请参见fragment.void(^launchPermission)(BOOL)=^(BOOL isAllow){if(isAllow){decisionHandler(wkNavigationResponsePolicyLow);}的下一条注释{decisionHandler(WKNavigationResponsePolicyCancel);}return;};/[self-AskPermissionForExternalink];UIAlertController*alert=[UIAlertController alertControllerWithTitle:@“打开外部Web内容?”消息:@“链接嵌入了其他内容”preferredStyle:UIAlertControllerStyleAlert];UIAlertAction*cancelAction=[UIAlertAction action WithTitle:NSLocalizedString(@“Cancel”、@“Cancel action”)等。我会尝试一下并让您知道,谢谢。谢谢您,这很有效!但我有一个语法问题,因为有一件事我不明白。如何将launchpermission()放入正确的范围中,以便在-(void)中使用AskPermissionForExternalink?我必须将AskPermisionForExternalink中的代码嵌入到webView代码中(对不起,我不知道如何将整个代码发布到这个简短的注释字段中),请参见fragment.void(^launchPermission)(BOOL)=^(BOOL isAllow){if(isAllow){decisionHandler的下一条注释(wkNavigationResponsePolicyLow);}其他{decisionHandler(WKNavigationResponsePolicyCancel)}返回;};/[self-AskPermissionForExternalink];UIAlertController*警报=[UIAlertController alertControllerWithTitle:@“打开外部Web内容?”消息:@“链接嵌入了其他内容”preferredStyle:UIAlertControllerStyleAlert];UIAlertAction*cancelAction=[UIAlertAction actionWithTitle:NSLocalizedString(@“Cancel”、“Cancel action”)等。