diff --git a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m index 8e1094e7c9..8aedcb3c3d 100644 --- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m +++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m @@ -188,6 +188,7 @@ - (void)pluginInitialize } CDVWebViewUIDelegate* uiDelegate = [[CDVWebViewUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]]; + uiDelegate.mediaPermissionGrantType = [self parsePermissionGrantType:[settings cordovaSettingForKey:@"MediaPermissionGrantType"]]; uiDelegate.allowNewWindows = [settings cordovaBoolSettingForKey:@"AllowNewWindows" defaultValue:NO]; self.uiDelegate = uiDelegate; @@ -458,6 +459,29 @@ - (UIView*)webView return self.engineWebView; } +- (CDVWebViewPermissionGrantType)parsePermissionGrantType:(NSString*)optionString +{ + CDVWebViewPermissionGrantType result = CDVWebViewPermissionGrantType_Prompt; + + if (optionString != nil){ + if ([optionString isEqualToString:@"prompt"]) { + result = CDVWebViewPermissionGrantType_Prompt; + } else if ([optionString isEqualToString:@"deny"]) { + result = CDVWebViewPermissionGrantType_Deny; + } else if ([optionString isEqualToString:@"grant"]) { + result = CDVWebViewPermissionGrantType_Grant; + } else if ([optionString isEqualToString:@"grantIfSameHostElsePrompt"]) { + result = CDVWebViewPermissionGrantType_GrantIfSameHost_ElsePrompt; + } else if ([optionString isEqualToString:@"grantIfSameHostElseDeny"]) { + result = CDVWebViewPermissionGrantType_GrantIfSameHost_ElseDeny; + } else { + NSLog(@"Invalid \"MediaPermissionGrantType\" was detected. Fallback to default value of \"prompt\""); + } + } + + return result; +} + #pragma mark WKScriptMessageHandler implementation - (void)userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(WKScriptMessage*)message diff --git a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.h b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.h index c5f85fd735..0c00cc4680 100644 --- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.h +++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.h @@ -24,8 +24,17 @@ NSMutableArray* windows; } +typedef NS_ENUM(NSInteger, CDVWebViewPermissionGrantType) { + CDVWebViewPermissionGrantType_Prompt, + CDVWebViewPermissionGrantType_Deny, + CDVWebViewPermissionGrantType_Grant, + CDVWebViewPermissionGrantType_GrantIfSameHost_ElsePrompt, + CDVWebViewPermissionGrantType_GrantIfSameHost_ElseDeny +}; + @property (nonatomic, copy) NSString* title; @property (nonatomic, assign) BOOL allowNewWindows; +@property (nonatomic, assign) CDVWebViewPermissionGrantType mediaPermissionGrantType; - (instancetype)initWithTitle:(NSString*)title; diff --git a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.m b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.m index 784af8dfae..d3ec070d43 100644 --- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.m +++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.m @@ -159,5 +159,31 @@ - (void)webViewDidClose:(WKWebView*)webView // We do not allow closing the primary WebView } +- (void)webView:(WKWebView *)webView requestMediaCapturePermissionForOrigin:(nonnull WKSecurityOrigin *)origin initiatedByFrame:(nonnull WKFrameInfo *)frame type:(WKMediaCaptureType)type decisionHandler:(nonnull void (^)(WKPermissionDecision))decisionHandler + API_AVAILABLE(ios(15.0)) +{ + WKPermissionDecision decision; + + if (_mediaPermissionGrantType == CDVWebViewPermissionGrantType_Prompt) { + decision = WKPermissionDecisionPrompt; + } + else if (_mediaPermissionGrantType == CDVWebViewPermissionGrantType_Deny) { + decision = WKPermissionDecisionDeny; + } + else if (_mediaPermissionGrantType == CDVWebViewPermissionGrantType_Grant) { + decision = WKPermissionDecisionGrant; + } + else { + if ([origin.host isEqualToString:webView.URL.host]) { + decision = WKPermissionDecisionGrant; + } + else { + decision =_mediaPermissionGrantType == CDVWebViewPermissionGrantType_GrantIfSameHost_ElsePrompt ? WKPermissionDecisionPrompt : WKPermissionDecisionDeny; + } + } + + decisionHandler(decision); +} + @end diff --git a/templates/cordova/defaults.xml b/templates/cordova/defaults.xml index ebbe6f66f9..6508598553 100644 --- a/templates/cordova/defaults.xml +++ b/templates/cordova/defaults.xml @@ -26,6 +26,7 @@ +