From 5f94413c9fc6f0d0c6172315bc9c3d8df3819c66 Mon Sep 17 00:00:00 2001 From: Koji Nakamaru Date: Mon, 7 Mar 2022 00:32:01 +0900 Subject: [PATCH 1/2] modified iOS implementation to enable allowFileAccessFromFileURLs/allowUniversalAccessFromFileURLs if UNITYWEBVIEW_IOS_ALLOW_FILE_URLS is defined. --- plugins/Editor/UnityWebViewPostprocessBuild.cs | 7 +++++++ plugins/iOS/WebView.mm | 13 +++++++++++++ plugins/iOS/WebViewWithUIWebView.mm | 13 +++++++++++++ 3 files changed, 33 insertions(+) diff --git a/plugins/Editor/UnityWebViewPostprocessBuild.cs b/plugins/Editor/UnityWebViewPostprocessBuild.cs index 386c3187..ea584d6a 100644 --- a/plugins/Editor/UnityWebViewPostprocessBuild.cs +++ b/plugins/Editor/UnityWebViewPostprocessBuild.cs @@ -215,6 +215,13 @@ public static void OnPostprocessBuild(BuildTarget buildTarget, string path) { var method = type.GetMethod("AddFrameworkToProject"); method.Invoke(proj, new object[]{target, "WebKit.framework", false}); } +#if UNITYWEBVIEW_IOS_ALLOW_FILE_URLS + // proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-DUNITYWEBVIEW_IOS_ALLOW_FILE_URLS"); + { + var method = type.GetMethod("AddBuildProperty", new Type[]{typeof(string), typeof(string), typeof(string)}); + method.Invoke(proj, new object[]{target, "OTHER_CFLAGS", "-DUNITYWEBVIEW_IOS_ALLOW_FILE_URLS"}); + } +#endif var dst = ""; //dst = proj.WriteToString(); { diff --git a/plugins/iOS/WebView.mm b/plugins/iOS/WebView.mm index 63b922fc..c426fdc1 100644 --- a/plugins/iOS/WebView.mm +++ b/plugins/iOS/WebView.mm @@ -171,6 +171,19 @@ - (id)initWithGameObjectName:(const char *)gameObjectName_ transparent:(BOOL)tra if (@available(iOS 13.0, *)) { configuration.defaultWebpagePreferences.preferredContentMode = contentMode; } +#if UNITYWEBVIEW_IOS_ALLOW_FILE_URLS + // cf. https://stackoverflow.com/questions/35554814/wkwebview-xmlhttprequest-with-file-url/44365081#44365081 + try { + [configuration.preferences setValue:@TRUE forKey:@"allowFileAccessFromFileURLs"]; + } + catch (NSException *ex) { + } + try { + [configuration setValue:@TRUE forKey:@"allowUniversalAccessFromFileURLs"]; + } + catch (NSException *ex) { + } +#endif WKWebView *wkwebView = [[WKWebView alloc] initWithFrame:view.frame configuration:configuration]; wkwebView.allowsLinkPreview = allowsLinkPreview; webView = wkwebView; diff --git a/plugins/iOS/WebViewWithUIWebView.mm b/plugins/iOS/WebViewWithUIWebView.mm index f18a04b7..ec1b91da 100644 --- a/plugins/iOS/WebViewWithUIWebView.mm +++ b/plugins/iOS/WebViewWithUIWebView.mm @@ -223,6 +223,19 @@ - (id)initWithGameObjectName:(const char *)gameObjectName_ transparent:(BOOL)tra if (@available(iOS 13.0, *)) { configuration.defaultWebpagePreferences.preferredContentMode = contentMode; } +#if UNITYWEBVIEW_IOS_ALLOW_FILE_URLS + // cf. https://stackoverflow.com/questions/35554814/wkwebview-xmlhttprequest-with-file-url/44365081#44365081 + try { + [configuration.preferences setValue:@TRUE forKey:@"allowFileAccessFromFileURLs"]; + } + catch (NSException *ex) { + } + try { + [configuration setValue:@TRUE forKey:@"allowUniversalAccessFromFileURLs"]; + } + catch (NSException *ex) { + } +#endif WKWebView *wkwebView = [[WKWebView alloc] initWithFrame:view.frame configuration:configuration]; wkwebView.allowsLinkPreview = allowsLinkPreview; webView = wkwebView; From 50ff2d35688e69725760e573e8c91d2e04edb05f Mon Sep 17 00:00:00 2001 From: Koji Nakamaru Date: Mon, 7 Mar 2022 00:44:10 +0900 Subject: [PATCH 2/2] updated README.md about UNITYWEBVIEW_IOS_ALLOW_FILE_URLS. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 37aa6cda..31a73193 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,13 @@ which new one (Assets/Plugins/iOS/WebView.mm) utilizes only WKWebView if iOS dep *NOTE: WKWebView is available since iOS8 but was largely changed in iOS9, so we use `___IPHONE_9_0` instead of `__IPHONE_8_0`* *NOTE: Several versions of Unity themselves also have the ITMS-90809 issue (cf. https://issuetracker.unity3d.com/issues/ios-apple-throws-deprecated-api-usage-warning-for-using-uiwebview-when-submitting-builds-to-the-app-store-connect ).*/ +#### XMLHttpRequest for file URLs + +WKWebView doesn't allow to access file URLs with XMLHttpRequest. This limitation can be relaxed by `allowFileAccessFromFileURLs`/`allowUniversalAccessFromFileURLs` settings. Those are however private APIs so currently disabled by default. For enabling them, please define `UNITYWEBVIEW_IOS_ALLOW_FILE_URLS`. + +cf. https://github.com/gree/unity-webview/issues/785 +cf. https://github.com/gree/unity-webview/issues/224#issuecomment-640642516 + ### Android #### File Input Field