iwashi1t 4 年之前
父節點
當前提交
15110382eb
No account linked to committer's email address

+ 42
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java 查看文件

25
 import android.webkit.CookieManager;
25
 import android.webkit.CookieManager;
26
 import android.webkit.DownloadListener;
26
 import android.webkit.DownloadListener;
27
 import android.webkit.GeolocationPermissions;
27
 import android.webkit.GeolocationPermissions;
28
+import android.webkit.HttpAuthHandler;
28
 import android.webkit.JavascriptInterface;
29
 import android.webkit.JavascriptInterface;
29
 import android.webkit.RenderProcessGoneDetail;
30
 import android.webkit.RenderProcessGoneDetail;
30
 import android.webkit.SslErrorHandler;
31
 import android.webkit.SslErrorHandler;
533
     view.loadUrl(BLANK_URL);
534
     view.loadUrl(BLANK_URL);
534
   }
535
   }
535
 
536
 
537
+  @ReactProp(name = "basicAuthCredential")
538
+  public void setBasicAuthCredential(WebView view, @Nullable ReadableMap credential) {
539
+    @Nullable BasicAuthCredential basicAuthCredential = null;
540
+    if (credential != null) {
541
+      if (credential.hasKey("username") && credential.hasKey("password")) {
542
+        String username = credential.getString("username");
543
+        String password = credential.getString("password");
544
+        basicAuthCredential = new BasicAuthCredential(username, password);
545
+      }
546
+    }
547
+    ((RNCWebView) view).setBasicAuthCredential(basicAuthCredential);
548
+  }
549
+
536
   @ReactProp(name = "onContentSizeChange")
550
   @ReactProp(name = "onContentSizeChange")
537
   public void setOnContentSizeChange(WebView view, boolean sendContentSizeChangeEvents) {
551
   public void setOnContentSizeChange(WebView view, boolean sendContentSizeChangeEvents) {
538
     ((RNCWebView) view).setSendContentSizeChangeEvents(sendContentSizeChangeEvents);
552
     ((RNCWebView) view).setSendContentSizeChangeEvents(sendContentSizeChangeEvents);
777
     ReadableArray mUrlPrefixesForDefaultIntent;
791
     ReadableArray mUrlPrefixesForDefaultIntent;
778
     protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
792
     protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
779
     protected @Nullable String ignoreErrFailedForThisURL = null;
793
     protected @Nullable String ignoreErrFailedForThisURL = null;
794
+    protected @Nullable BasicAuthCredential basicAuthCredential = null;
780
 
795
 
781
     public void setIgnoreErrFailedForThisURL(@Nullable String url) {
796
     public void setIgnoreErrFailedForThisURL(@Nullable String url) {
782
       ignoreErrFailedForThisURL = url;
797
       ignoreErrFailedForThisURL = url;
783
     }
798
     }
784
 
799
 
800
+    public void setBasicAuthCredential(@Nullable BasicAuthCredential credential) {
801
+      basicAuthCredential = credential;
802
+    }
803
+
785
     @Override
804
     @Override
786
     public void onPageFinished(WebView webView, String url) {
805
     public void onPageFinished(WebView webView, String url) {
787
       super.onPageFinished(webView, url);
806
       super.onPageFinished(webView, url);
866
       return this.shouldOverrideUrlLoading(view, url);
885
       return this.shouldOverrideUrlLoading(view, url);
867
     }
886
     }
868
 
887
 
888
+    @Override
889
+    public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
890
+      if (basicAuthCredential != null) {
891
+        handler.proceed(basicAuthCredential.username, basicAuthCredential.password);
892
+        return;
893
+      }
894
+      super.onReceivedHttpAuthRequest(view, handler, host, realm);
895
+    }
896
+
869
     @Override
897
     @Override
870
     public void onReceivedSslError(final WebView webView, final SslErrorHandler handler, final SslError error) {
898
     public void onReceivedSslError(final WebView webView, final SslErrorHandler handler, final SslError error) {
871
         handler.cancel();
899
         handler.cancel();
1215
       mRNCWebViewClient.setIgnoreErrFailedForThisURL(url);
1243
       mRNCWebViewClient.setIgnoreErrFailedForThisURL(url);
1216
     }
1244
     }
1217
 
1245
 
1246
+    public void setBasicAuthCredential(BasicAuthCredential credential) {
1247
+      mRNCWebViewClient.setBasicAuthCredential(credential);
1248
+    }
1249
+
1218
     public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
1250
     public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
1219
       this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
1251
       this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
1220
     }
1252
     }
1470
     }
1502
     }
1471
   }
1503
   }
1472
 }
1504
 }
1505
+
1506
+class BasicAuthCredential {
1507
+  String username;
1508
+  String password;
1509
+
1510
+  BasicAuthCredential(String username, String password) {
1511
+    this.username = username;
1512
+    this.password = password;
1513
+  }
1514
+}

+ 1
- 0
apple/RNCWebView.h 查看文件

64
 @property (nonatomic, copy) NSString * _Nullable allowingReadAccessToURL;
64
 @property (nonatomic, copy) NSString * _Nullable allowingReadAccessToURL;
65
 @property (nonatomic, assign) BOOL pullToRefreshEnabled;
65
 @property (nonatomic, assign) BOOL pullToRefreshEnabled;
66
 @property (nonatomic, weak) UIRefreshControl * refreshControl;
66
 @property (nonatomic, weak) UIRefreshControl * refreshControl;
67
+@property (nonatomic, copy) NSDictionary * _Nullable basicAuthCredential;
67
 
68
 
68
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
69
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
69
 @property (nonatomic, assign) WKContentMode contentMode;
70
 @property (nonatomic, assign) WKContentMode contentMode;

+ 9
- 0
apple/RNCWebView.m 查看文件

781
             }
781
             }
782
         }
782
         }
783
     }
783
     }
784
+    if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodHTTPBasic) {
785
+        NSString *username = [_basicAuthCredential valueForKey:@"username"];
786
+        NSString *password = [_basicAuthCredential valueForKey:@"password"];
787
+        if (username && password) {
788
+            NSURLCredential *credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];
789
+            completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
790
+            return;
791
+        }
792
+    }
784
     completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
793
     completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
785
 }
794
 }
786
 
795
 

+ 1
- 0
apple/RNCWebViewManager.m 查看文件

75
 RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
75
 RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
76
 RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
76
 RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
77
 RCT_EXPORT_VIEW_PROPERTY(allowingReadAccessToURL, NSString)
77
 RCT_EXPORT_VIEW_PROPERTY(allowingReadAccessToURL, NSString)
78
+RCT_EXPORT_VIEW_PROPERTY(basicAuthCredential, NSDictionary)
78
 
79
 
79
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
80
 #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
80
 RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)
81
 RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)

+ 12
- 0
docs/Reference.md 查看文件

71
 - [`pullToRefreshEnabled`](Reference.md#pullToRefreshEnabled)
71
 - [`pullToRefreshEnabled`](Reference.md#pullToRefreshEnabled)
72
 - [`ignoreSilentHardwareSwitch`](Reference.md#ignoreSilentHardwareSwitch)
72
 - [`ignoreSilentHardwareSwitch`](Reference.md#ignoreSilentHardwareSwitch)
73
 - [`onFileDownload`](Reference.md#onFileDownload)
73
 - [`onFileDownload`](Reference.md#onFileDownload)
74
+- [`basicAuthCredential`](Reference.md#basicAuthCredential)
74
 
75
 
75
 ## Methods Index
76
 ## Methods Index
76
 
77
 
1263
 | ------- | -------- | -------- |
1264
 | ------- | -------- | -------- |
1264
 | function | No       | iOS      |
1265
 | function | No       | iOS      |
1265
 
1266
 
1267
+### `basicAuthCredential`[⬆](#props-index)<!-- Link generated with jump2header -->
1268
+
1269
+An object that specifies the credentials of a user to be used for basic authentication.
1270
+
1271
+- `username` (string) - A username used for basic authentication.
1272
+- `password` (string) - A password used for basic authentication.
1273
+
1274
+| Type   | Required |
1275
+| ------ | -------- |
1276
+| object | No       |
1277
+
1266
 ## Methods
1278
 ## Methods
1267
 
1279
 
1268
 ### `extraNativeComponentConfig()`[⬆](#methods-index)<!-- Link generated with jump2header -->
1280
 ### `extraNativeComponentConfig()`[⬆](#methods-index)<!-- Link generated with jump2header -->

+ 18
- 0
src/WebViewTypes.ts 查看文件

249
   event: ShouldStartLoadRequest,
249
   event: ShouldStartLoadRequest,
250
 ) => boolean;
250
 ) => boolean;
251
 
251
 
252
+export interface BasicAuthCredential {
253
+  /**
254
+   * A username used for basic authentication.
255
+   */
256
+  username: string;
257
+
258
+  /**
259
+   * A password used for basic authentication.
260
+   */
261
+  password: string;
262
+}
263
+
252
 export interface CommonNativeWebViewProps extends ViewProps {
264
 export interface CommonNativeWebViewProps extends ViewProps {
253
   cacheEnabled?: boolean;
265
   cacheEnabled?: boolean;
254
   incognito?: boolean;
266
   incognito?: boolean;
277
    * Append to the existing user-agent. Overridden if `userAgent` is set.
289
    * Append to the existing user-agent. Overridden if `userAgent` is set.
278
    */
290
    */
279
   applicationNameForUserAgent?: string;
291
   applicationNameForUserAgent?: string;
292
+  basicAuthCredential?: BasicAuthCredential;
280
 }
293
 }
281
 
294
 
282
 export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
295
 export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
1031
    * Append to the existing user-agent. Overridden if `userAgent` is set.
1044
    * Append to the existing user-agent. Overridden if `userAgent` is set.
1032
    */
1045
    */
1033
   applicationNameForUserAgent?: string;
1046
   applicationNameForUserAgent?: string;
1047
+
1048
+  /**
1049
+   * An object that specifies the credentials of a user to be used for basic authentication.
1050
+   */
1051
+  basicAuthCredential?: BasicAuthCredential;
1034
 }
1052
 }