swift 读取本地html,从swift 3中的本地自定义文件夹将html文件加载到WebView(Load html file to WebView from local custom folde...

从swift 3中的本地自定义文件夹将html文件加载到WebView(Load html file to WebView from local custom folder in swift 3)

我想从本地自定义文件夹 - ProjectNameFolder/test/index.html将html文件加载到WebView。

尝试此代码,但不起作用。

mWebView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "test/index", ofType: "html")!) as URL))

EDITED

当我没有文件夹名称,工作完美。

mWebView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "index", ofType: "html")!) as URL))

PS我是斯威夫特的新手。

I want to load html file to WebView from local custom folder - ProjectNameFolder/test/index.html.

Trying this code, but doesn't work.

mWebView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "test/index", ofType: "html")!) as URL))

EDITED

When I do without folder name, works perfectly.

mWebView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "index", ofType: "html")!) as URL))

P.S. I'm new in Swift.

原文:https://stackoverflow.com/questions/39753699

更新时间:2020-02-20 19:15

最满意答案

我找到了解决问题的方法。

将测试文件夹拖放到项目文件夹中。

Pzp8q.png

9Jiws.png

这是代码

override func viewDidLoad() {

super.viewDidLoad()

mWebView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "test/index", ofType: "html")!)))

}

I found solution for my problem.

Drag and drop test folder into project folder.

Pzp8q.png

9Jiws.png

Here is code

override func viewDidLoad() {

super.viewDidLoad()

mWebView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "test/index", ofType: "html")!)))

}

相关问答

对此的解决方案是下面的一段代码: final File filesDir = getContext().getFilesDir();

final File htmlStuffDir = new File(filesDir, "htmlStuff");

final String baseUrlPath = htmlStuffDir.getAbsolutePath() + "/";

webView.loadDataWithBaseURL("file:///" + baseUrlPath, htmlC

...

MSDN写道: 要从应用程序的LocalFolder或TemporaryFolder数据存储中加载未压缩和未加密的内容,请使用Navigate方法和使用ms-appdata方案的Uri。 对此方案的WebView支持要求您将内容放在本地或临时文件夹下的子文件夹中。 所以看起来如果你想加载存储在不同文件夹中的html,它就不受支持了。 但是,您可以将文件的内容读取为字符串,而不是使用web.NavigateToString(yourHtmlInString) 。 (NavigateToString用

...

将html复制到您的项目目录并使用以下代码: @IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {

super.viewDidLoad()

var htmlFile = NSBundle.mainBundle().pathForResource("MyHtmlFile", ofType: "html")

var htmlString = try? String(conten

...

要检索应用程序资源的URL,您应该使用NSBundle类的URLForResource方法。 斯威夫特2 let url = NSBundle.mainBundle().URLForResource("privacy", withExtension:"html")

Swift 3 let url = Bundle.main.url(forResource: "privacy", withExtension: "html")

To retrieve URLs for application r

...

就像在一个普通的网页中一样。 让测试1中的链接指向test2。 Like in a regular webpage. Let the link in test 1 point to test2.

不知道为什么要使用这样的两个webView。 但是, 一种解决方案是将一个Javascript接口桥接器添加到navWebView,并让它将URL传递回Java,然后Java可以通过loadUrl将其传递给第二个webView 。 有关JSInterface的信息: http : //developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20jav

...

我找到了解决问题的方法。 将测试文件夹拖放到项目文件夹中。 这是代码 override func viewDidLoad() {

super.viewDidLoad()

mWebView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "test/index", ofType: "html")!)))

}

I found solution for my prob

...

在苹果论坛上阅读这篇文章并将其与其他问题放在一起后,我能够得出以下工作代码: func applicationDidFinishLaunching(aNotification: NSNotification?)

{

self.window.title = "My App Title"

var try6 = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("main", ofType:"

...

我已经解决了。 解: 将图像文件转换为base64字符串 StorageFolder appFolder = ApplicationData.Current.LocalFolder;

StorageFile file = await appFolder.GetFileAsync("SplashScreen.png");

using (var stream = await file.OpenAsync(FileAccessMode.Read))

{

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值