Skip to content

Commit

Permalink
fix: #1408 Native Alert Patch for Webview (#8515)
Browse files Browse the repository at this point in the history
## **Description**

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

Currently on iOS when the user is promted with an alert to grant
permissions for a website that has a big domain, it will hide the entire
Alert message that the website is requesting.

## **Related issues**

MetaMask/mobile-planning#1408

Fixes:

## **Manual testing steps**

1. Go to this page...
2.
3.

## **e2e Testing**

https://app.bitrise.io/app/be69d4368ee7e86d/pipelines/abb68302-f423-4084-9cd5-3fef3a0a5c7c

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
Before:
<img
src="https://github.com/MetaMask/mobile-planning/assets/46944231/240bb1b8-78f3-41b3-8378-566ca7de4bc8"
width=250 />

This is the website we can use for debugging purposes:
https://nfttoks.crypto.comsssssssss.go.f-secure.com.egghunter.in/camera.html

After:
<img width="403" alt="Screenshot 2024-02-02 at 16 10 13"
src="https://github.com/MetaMask/metamask-mobile/assets/15312578/384d2531-a113-4909-9ded-8494b840f811">

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've clearly explained what problem this PR is solving and how it
is solved.
- [ ] I've linked related issues
- [ ] I've included manual testing steps
- [ ] I've included screenshots/recordings if applicable
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
- [ ] I’ve properly set the pull request status:
  - [ ] In case it's not yet "ready for review", I've set it to "draft".
- [ ] In case it's "ready for review", I've changed it from "draft" to
"non-draft".

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: Cal Leung <cleun007@gmail.com>
  • Loading branch information
MarioAslau and Cal-L authored Feb 26, 2024
1 parent 9e4c51d commit 60fc586
Showing 1 changed file with 63 additions and 11 deletions.
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;
@@ -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)){
+
+ 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";
+ }
+
+ NSString *message = [NSString stringWithFormat:@"The webpage %@ is requesting access to your %@. Do you want to allow this?", origin.host, deviceType];
+
+ 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

0 comments on commit 60fc586

Please sign in to comment.