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

[Android] Using ShowHtmlFile with HTML files stored at runtime #518

Open
1 task done
omaRaad opened this issue Jun 25, 2024 · 0 comments
Open
1 task done

[Android] Using ShowHtmlFile with HTML files stored at runtime #518

omaRaad opened this issue Jun 25, 2024 · 0 comments
Assignees
Labels
question Issue type : Question WebView Service : WebView

Comments

@omaRaad
Copy link

omaRaad commented Jun 25, 2024

Service

  • WebView

Version

2.2.6

Summary

Context

I have an HTML file stearmingAssets/html/WidgetTemplate.html. This file includes some placeholder text that is set at runtime to cater to different users(e.g. {APP_TOKEN} and {USER_ID}

I set these values at runtime by taking all the content from the WidgetTemplate.html and replacing the placeholders with the respective value. I then create a new file widget.html with the new content.

The Problem

On iOS, the whole process works smoothly. I can Write/Read into/from the streamingAssets path.
But on Android, it seems that it's read only. So I cannot do that. I have to write the file into the persisitentData path. And unfortunately, this path is not accepted by Gpm.WebView.

Any way to fix that?

Screenshots/Code

The code I use to read the template file:

    private IEnumerator LoadTemplate()
    {
        var htmlString = string.Empty;
        var htmlFile = Path.Combine(Application.streamingAssetsPath, "html/WidgetTemplate.html");
#if UNITY_IOS
        htmlString = File.ReadAllText(htmlFile);
        yield return null;
#elif UNITY_ANDROID
        var uri = "jar:file://" + Application.dataPath + "!/assets/html/WidgetTemplate.html";
        using (var www = UnityWebRequest.Get(uri))
        {
            yield return www.SendWebRequest();
            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.LogError(www.error);
                yield break;
            }

            htmlString = www.downloadHandler.text;
        }
#endif

        ShowHtmlString(htmlString);
    }

The code to write into a file and use with Gpm.

    public void ShowHtmlString(string htmlString)
    {
        htmlString = htmlString.Replace("{{APP_TOKEN}}", token)
                           .Replace("{{USER_ID}}", userId)
                           .Replace("{{WIDGET_TYPE}}", GetStringFromWidgetType(widgetType));

        string newWidgetFile = Path.Combine(Application.persistentDataPath, "Widget.html");
        Directory.CreateDirectory(Path.GetDirectoryName(newWidgetFile));
        File.WriteAllText(newWidgetFile, htmlString);

        string htmlFilePath = "file://" + newWidgetFile;

        GpmWebView.ShowHtmlFile(
            htmlFilePath,
            new GpmWebViewRequest.Configuration()
            {
                orientation = GpmOrientation.UNSPECIFIED,
                isClearCookie = true,
                isClearCache = true,
#if UNITY_IOS
                contentMode = GpmWebViewContentMode.MOBILE
#endif
            },
            OnCallback,
            new List<string>()
        );
    }

The error thrown by Gpm.WebView due to not using streamingAssets path for Android.

- [GPM][WebView][Android][WebViewManager] The custom scheme should be started by 'file:///android_asset/'.  (file:///storage/emulated/0/Android/data/com.bitburst.BitLabs/files/Widget.html)
- OnAsyncEvent : {"callback":0,"callbackType":0,"error":"{\"domain\":\"WebViewManager\",\"code\":\"INVALID_URL\",\"message\":\"Invalid file path.\"}","scheme":"gpmwebview://webViewCallback"}
- JsonException: Can't assign value 'INVALID_URL' (type System.String) to type System.Int32

Additional context

I can't use GpmWebView.ShowHtmlString either, because the HTML I am trying to render uses some CSS and Javascript resources with relative URLs, and thus I it needs a to launch with a Base URL. And as far as I know, GpmWebView doesn't support adding a base URL. (That is actually a good feature request as well).

@omaRaad omaRaad added the question Issue type : Question label Jun 25, 2024
@SangYun-nhn SangYun-nhn added the WebView Service : WebView label Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issue type : Question WebView Service : WebView
Projects
None yet
Development

No branches or pull requests

4 participants