2016-09-28 55 views
3

我想从本地自定义文件夹加载HTML文件到WebView - ProjectNameFolder/test/index.html加载HTML文件到WebView从本地自定义文件夹在SWIFT 3

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

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)) 

P.S.我是Swift新手。

+0

为什么你认为它应该是一个 '测试' 子文件夹内? –

+0

@PhillipMills,我有这样的文件夹结构。 /category/subcategory1/file.html,/category/subcategory2/file.html等。 – Abdukhafiz

+0

是否将文件作为“复制软件包资源”构建阶段的一部分复制到该结构中? –

回答

4

我发现我的问题的解决方案。

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

enter image description here

enter image description here

这里是代码

override func viewDidLoad() { 
    super.viewDidLoad()  
    mWebView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "test/index", ofType: "html")!))) 
} 
相关问题