1

I get an unknown error with WKWebView on my Iphone.When try to Change the navigation link inside the view the destination link request camera and microphone permission. when try to view the alert between WKWebView and the device its crashed .

Cannot be called with asCopy = NO on non-main thread.

+[UIView setAnimationsEnabled:] being called from a background thread. Performing any operation from a background thread on UIView or a subclass is not supported and may result in unexpected and insidious behavior. trace=(....)

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler {
    dispatch_async(dispatch_get_main_queue(), ^{

            if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
                if (navigationAction.request.URL) {
                    if (![navigationAction.request.URL.resourceSpecifier containsString:@"ex path"]) {
                        if ([[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
                            //------>
                            UIApplication *application = [UIApplication sharedApplication];
                            [application openURL:navigationAction.request.URL options:@{} completionHandler:nil];
                            decisionHandler(WKNavigationActionPolicyCancel);
                        }else{
                        }
                    } else {
                        decisionHandler(WKNavigationActionPolicyAllow);
                    }
                }
            }
        
            else if  (navigationAction.request.URL && [[NSString stringWithFormat:@"%@",navigationAction.request.URL] rangeOfString:@"disconnectChat"].location != NSNotFound ){
                decisionHandler(WKNavigationActionPolicyAllow);
             
                [UIView animateWithDuration:0.5 animations:^{
                   self->_thankYouView.hidden =  NO ;
                } completion:^(BOOL finished) {
                }];
            }
            else {
                //------>
                    decisionHandler(WKNavigationActionPolicyAllow);
            }
    });
    
}
cat
  • 25
  • 1
  • 2
  • 10
  • You need to perform UI code in main thread. What's your code for the delegate where you show the alert? – Larme Apr 18 '22 at 08:18
  • the alert is is shown from safari wkWebView asking about permission for the camera and microphone. – cat Apr 18 '22 at 08:38
  • edit the question add the code. – cat Apr 18 '22 at 08:47
  • Which "path" of your logic is called before showing that pop up? Unrelated, but you can do `navigationAction.request.URL.asboluteString`instead of `[NSString stringWithFormat:@"%@",navigationAction.request.URL]`. There is also an unhandled case `}else{}` where you need the call the `decisionHandler`. – Larme Apr 18 '22 at 12:46

1 Answers1

1

Try use this

- (void) webView:(WKWebView *)webView
     requestMediaCapturePermissionForOrigin:(WKSecurityOrigin *)origin
     initiatedByFrame:(WKFrameInfo *)frame type:(WKMediaCaptureType)type
     decisionHandler:(void (^)(WKPermissionDecision decision))decisionHandlerii
     API_AVAILABLE(ios(15.0))
{
    decisionHandler(WKPermissionDecisionGrant);
 
}