Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(iOS) Avoid repetitive permission prompts #1245

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_GrantIfSameHost_ElsePrompt;

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

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="grantIfSameHostElsePrompt" />
<preference name="MediaTypesRequiringUserActionForPlayback" value="none" />
<preference name="SuppressesIncrementalRendering" value="false" />
<preference name="SuppressesLongPressGesture" value="false" />
Expand Down
Loading