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

fix: #1408 Native Alert Patch for Webview #8515

Merged
merged 10 commits into from
Feb 26, 2024
74 changes: 63 additions & 11 deletions patches/react-native-webview+11.13.0.patch
Original file line number Diff line number Diff line change
Expand Up @@ -1171,42 +1171,42 @@ index 0000000..b9581ac
+}
\ No newline at end of file
diff --git a/node_modules/react-native-webview/apple/RNCWebView.m b/node_modules/react-native-webview/apple/RNCWebView.m
index 28c078a..6f7d0b7 100644
index 28c078a..9bb5368 100644
--- a/node_modules/react-native-webview/apple/RNCWebView.m
+++ b/node_modules/react-native-webview/apple/RNCWebView.m
@@ -105,6 +105,7 @@ static NSDictionary* customCertificatesForHost;
@@ -105,6 +105,7 @@ @implementation RNCWebView
UIStatusBarStyle _savedStatusBarStyle;
#endif // !TARGET_OS_OSX
BOOL _savedStatusBarHidden;
+ BOOL _disablePromptDuringLoading; //Disables the display of prompts during site navigation/loading

#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
UIScrollViewContentInsetAdjustmentBehavior _savedContentInsetAdjustmentBehavior;
@@ -139,6 +140,7 @@ static NSDictionary* customCertificatesForHost;
@@ -139,6 +140,7 @@ - (instancetype)initWithFrame:(CGRect)frame
_injectedJavaScriptForMainFrameOnly = YES;
_injectedJavaScriptBeforeContentLoaded = nil;
_injectedJavaScriptBeforeContentLoadedForMainFrameOnly = YES;
+ _disablePromptDuringLoading = YES;

#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
_savedContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
@@ -417,6 +419,7 @@ static NSDictionary* customCertificatesForHost;
@@ -417,6 +419,7 @@ -(void)keyboardDisplacementFix
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqual:@"estimatedProgress"] && object == self.webView) {
if(_onLoadingProgress){
+ _disablePromptDuringLoading = YES;
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
[event addEntriesFromDictionary:@{@"progress":[NSNumber numberWithDouble:self.webView.estimatedProgress]}];
_onLoadingProgress(event);
@@ -492,6 +495,7 @@ static NSDictionary* customCertificatesForHost;
MarioAslau marked this conversation as resolved.
Show resolved Hide resolved
@@ -492,6 +495,7 @@ - (void)userContentController:(WKUserContentController *)userContentController
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
[event addEntriesFromDictionary: @{@"navigationType": message.body}];
_onLoadingFinish(event);
+ _disablePromptDuringLoading = NO;
}
} else if ([message.name isEqualToString:MessageHandlerName]) {
if (_onMessage) {
@@ -851,11 +855,13 @@ static NSDictionary* customCertificatesForHost;
@@ -851,11 +855,13 @@ - (void) webView:(WKWebView *)webView
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
{
#if !TARGET_OS_OSX
Expand All @@ -1225,7 +1225,59 @@ index 28c078a..6f7d0b7 100644
#else
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:message];
@@ -894,44 +900,49 @@ static NSDictionary* customCertificatesForHost;
@@ -868,6 +874,51 @@ - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSStrin
/**
* confirm
*/
+// This patch made to overridde the restrictions that webView is imposing to the native Alert, by restricting its size.
+- (void)webView:(WKWebView *)webView requestMediaCapturePermissionForOrigin:(WKSecurityOrigin *)origin initiatedByFrame:(WKFrameInfo *)frame type:(WKMediaCaptureType)type decisionHandler:(void (^)(WKPermissionDecision decision))decisionHandler API_AVAILABLE(ios(15.0)){
MarioAslau marked this conversation as resolved.
Show resolved Hide resolved
MarioAslau marked this conversation as resolved.
Show resolved Hide resolved
+
+ NSString *deviceType;
+
+ switch (type) {
+ case WKMediaCaptureTypeCamera:
+ deviceType = @"camera";
+ break;
+ case WKMediaCaptureTypeMicrophone:
+ deviceType = @"microphone";
+ break;
+ case WKMediaCaptureTypeCameraAndMicrophone:
+ deviceType = @"camera and microphone";
+ break;
+ default:
+ deviceType = @"unknown device";
MarioAslau marked this conversation as resolved.
Show resolved Hide resolved
+ }
+
+ NSString *message = [NSString stringWithFormat:@"The webpage %@ is requesting access to your %@. Do you want to allow this?", origin.host, deviceType];
MarioAslau marked this conversation as resolved.
Show resolved Hide resolved
+
+ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Permission Request"
+ message:message
+ preferredStyle:UIAlertControllerStyleAlert];
+
+ UIAlertAction *allowAction = [UIAlertAction actionWithTitle:@"Allow"
+ style:UIAlertActionStyleDefault
+ handler:^(UIAlertAction * _Nonnull action) {
+ decisionHandler(WKPermissionDecisionGrant);
+ }
+ ];
+
+ UIAlertAction *denyAction = [UIAlertAction actionWithTitle:@"Deny"
+ style:UIAlertActionStyleCancel
+ handler:^(UIAlertAction * _Nonnull action) {
+ decisionHandler(WKPermissionDecisionDeny);
+ }
+ ];
+
+ [alertController addAction:allowAction];
+ [alertController addAction:denyAction];
+
+ [[self topViewController] presentViewController:alertController animated:YES completion:NULL];
+}
+
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
#if !TARGET_OS_OSX
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
@@ -894,44 +945,49 @@ - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSStr
* prompt
*/
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *))completionHandler{
Expand All @@ -1247,7 +1299,7 @@ index 28c078a..6f7d0b7 100644
-#else
- NSAlert *alert = [[NSAlert alloc] init];
- [alert setMessageText:prompt];
-
- const NSRect RCTSingleTextFieldFrame = NSMakeRect(0.0, 0.0, 275.0, 22.0);
- NSTextField *textField = [[NSTextField alloc] initWithFrame:RCTSingleTextFieldFrame];
- textField.cell.scrollable = YES;
Expand All @@ -1256,7 +1308,7 @@ index 28c078a..6f7d0b7 100644
- }
- textField.stringValue = defaultText;
- [alert setAccessoryView:textField];
-
- [alert addButtonWithTitle:NSLocalizedString(@"OK", @"OK button")];
- [alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"Cancel button")];
- [alert beginSheetModalForWindow:[NSApp keyWindow] completionHandler:^(NSModalResponse response) {
Expand Down Expand Up @@ -1310,15 +1362,15 @@ index 28c078a..6f7d0b7 100644
}

#if !TARGET_OS_OSX
@@ -1157,6 +1168,7 @@ static NSDictionary* customCertificatesForHost;
@@ -1157,6 +1213,7 @@ - (void)webView:(WKWebView *)webView
}

if (_onLoadingFinish) {
+ _disablePromptDuringLoading = NO;
_onLoadingFinish([self baseEvent]);
}
}
@@ -1446,3 +1458,4 @@ static NSDictionary* customCertificatesForHost;
@@ -1446,3 +1503,4 @@ - (void)userContentController:(WKUserContentController *)userContentController d
}

@end
Expand Down
Loading