Skip to content

Commit

Permalink
feat: avoid repetitive permission prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
jancabadaj committed Aug 10, 2022
1 parent 480a3c2 commit 78f907e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@
NSMutableArray<UIViewController*>* 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions templates/cordova/defaults.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<preference name="BackupWebStorage" value="cloud" />
<preference name="DisallowOverscroll" value="false" />
<preference name="EnableViewportScale" value="false" />
<preference name="MediaPermissionGrantType" value="prompt" />
<preference name="MediaTypesRequiringUserActionForPlayback" value="none" />
<preference name="SuppressesIncrementalRendering" value="false" />
<preference name="SuppressesLongPressGesture" value="false" />
Expand Down

0 comments on commit 78f907e

Please sign in to comment.