WKWebView doesn't load local files

SUDALVSUDALV Member ✭✭

My app have a WKWebView control. The webview is loading pages from local HTML files. The pages contains some files references (like pictures, css, js) and this files also stored in local filesystem (but not in the project resources folder). How to set proper Uri for this files?
I have tried several different places for the files:

Environment.SpecialFolder.ApplicationData

NSBundle.MainBundle.BundlePath

./

I have tried to use it with and without "file://" prefix, but nothing works.
If i try to read this files directly from C# code (File.ReadAllText()), then i can see this files are exists and have correct content, but webview doesn't display the images and doesn't load css/js files.

example file writing:
File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "Static/", fileName), content);

example HTML with local file reference:
<link href="file:///var/mobile/Containers/Data/Application/4675A5AF-A3DD-457E-8B8B-BFABD4EF4AB8/Documents/.config/Static/0.css" rel="stylesheet">

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited December 2018

    I think you can use File.ReadAllText() to get the content of that css file , then use EvaluateJavaScript to load css string.

    Refer this solution :https://stackoverflow.com/a/33126467/8187800

  • SUDALVSUDALV Member ✭✭

    @ColeX said:
    I think you can use File.ReadAllText() to get the content of that css file , then use EvaluateJavaScript to load css string.

    I need to load fonts (.ttf) from local files (not from bundle), so i can't use EvaluateJavaScript and i can't put content of this files into html (in <script> tag for js or <style> tag for css or base64 format for images)

  • BerryvanElkBerryvanElk NLMember

    Hi Sudalv, did you found a solution for this. Is it possible to just open a WKWebview and open local html pages with absolute paths to assets?

  • SUDALVSUDALV Member ✭✭

    @BerryvanElk said:
    Hi Sudalv, did you found a solution for this. Is it possible to just open a WKWebview and open local html pages with absolute paths to assets?

    I use local http server

  • itriceitrice Member

    1、动态申请权限
    protected override void OnResume()
    {
    base.OnResume();

        #region 权限请求
    
        if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != Permission.Granted)
        {
            ActivityCompat.RequestPermissions(this, new[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage },0);
        }
    
        #endregion
    }
    

    2、路径地址:/storage/emulated/0/
    3、viewer显示
    var rs = System.IO.File.Exists(filepath);
    if (rs)
    {
    var context = Android.App.Application.Context;
    var file = new Java.IO.File(filepath);

            //var uri = FileProvider.GetUriForFile(context, "com.jzxy.duoke.provider", new Java.IO.File(filepath));
            //var uri = FileProvider.GetUriForFile(MainActivity.AppContext, MainActivity.AppContext.PackageName+ ".provider", new Java.IO.File(filepath));
            var uri = Android.Net.Uri.FromFile(file);
            try
            {
                var intent = new Intent(Intent.ActionView);
                intent.AddCategory(Intent.CategoryDefault);
                intent.SetFlags(ActivityFlags.GrantReadUriPermission);
                intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
                intent.SetDataAndType(uri, mimeType);
                Forms.Context.StartActivity(Intent.CreateChooser(intent, "选择打开的程序"));
            }
            catch (Exception e)
            {
                Toast.MakeText(context, e.Message, ToastLength.Long).Show();
            }
    

    }

    作者:iTrice 来源:CSDN

Sign In or Register to comment.