Skip to content

Commit

Permalink
(iOS) Avoid repetitive permission prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
jancabadaj committed Nov 7, 2024
1 parent 18a80d2 commit 3a46e22
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 @@ -189,6 +189,7 @@ - (void)pluginInitialize

CDVWebViewUIDelegate* uiDelegate = [[CDVWebViewUIDelegate alloc] initWithViewController:self.viewController];
uiDelegate.title = [[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 @@ -453,6 +454,29 @@ - (UIView*)webView
return self.engineWebView;
}

- (CDVWebViewPermissionGrantType)parsePermissionGrantType:(NSString*)optionString
{
CDVWebViewPermissionGrantType result = CDVWebViewPermissionGrantType_Grant;

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 \"grant\"");
}
}

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 @@ -32,8 +32,17 @@ NS_ASSUME_NONNULL_BEGIN
CDV_SWIFT_UI_ACTOR
@interface CDVWebViewUIDelegate : NSObject <WKUIDelegate>

typedef NS_ENUM(NSInteger, CDVWebViewPermissionGrantType) {
CDVWebViewPermissionGrantType_Prompt,
CDVWebViewPermissionGrantType_Deny,
CDVWebViewPermissionGrantType_Grant,
CDVWebViewPermissionGrantType_GrantIfSameHost_ElsePrompt,
CDVWebViewPermissionGrantType_GrantIfSameHost_ElseDeny
};

@property (nonatomic, nullable, copy) NSString* title;
@property (nonatomic, assign) BOOL allowNewWindows;
@property (nonatomic, assign) CDVWebViewPermissionGrantType mediaPermissionGrantType;

- (instancetype)initWithViewController:(CDVViewController*)vc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,32 @@ - (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);
}

#pragma mark - Utility Methods

- (nullable UIViewController *)topViewController
Expand Down
1 change: 1 addition & 0 deletions templates/cordova/defaults.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<preference name="AllowInlineMediaPlayback" value="false" />
<preference name="DisallowOverscroll" value="false" />
<preference name="EnableViewportScale" value="false" />
<preference name="MediaPermissionGrantType" value="grant" />
<preference name="MediaTypesRequiringUserActionForPlayback" value="none" />
<preference name="SuppressesIncrementalRendering" value="false" />
<preference name="SuppressesLongPressGesture" value="false" />
Expand Down

0 comments on commit 3a46e22

Please sign in to comment.