2017-09-12 17 views
0

構造化されたディレクトリを持つローカルアプリケーションの "Documents"フォルダからHTMLページを読み込みました。 "メイン"フォルダのホームページとリソース、 "インタラクティブ"フォルダに別のモジュールを保存してリダイレクトするためのホームページを開きます。WKWebViewはHTMLでローカルリソースを読み込めません

enter image description here

私は「メイン」フォルダ内の「ipad.html」をロードし、ページが正しいスタイルを表示するためのリソース(すなわち、CSS/JSファイル)をロードすることはできません。このようなコード。

let indexHTML = "ipad.html" 
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true) 
let documentDirectoryPath: String = path[0] 
let folderPath = documentDirectoryPath.appending("/main") 
let destinationURLForFile = URL(fileURLWithPath: folderPath + "/\(indexHTML)") 
let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true) 

do{ 
    let fileName = try String(contentsOf: destinationURLForFile, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) 
    webView.loadHTMLString(fileName, baseURL: baseUrl) 

}catch{ 
    print("Loading HTML failed.") 
} 

"ipad.html"のようにこのように。

<!doctype html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
    <title>demo</title> 
    <meta name="description" content="" /> 
    <meta name="author" content="" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> 

    <link rel="stylesheet" href="./css/animate.min.css"> 
    <link rel="stylesheet" href="./css/style.css"> 

    <script src="./bower_components/modernizr/modernizr-2.5.3.min.js"></script> 
</head> 
<body class="page-ipad-landing animated" lang=""> 
...... 
...... 
</body> 
</html> 

しかし、私はBundle.mainに移動し、「ipad.html」は正しく表示されロードするための「メイン」フォルダを削除。なぜ異なる動作やiOSが「Documents」フォルダの下で複雑なフォルダ構造をサポートしていないのですか、どのように修正する必要がありますか?ありがとう。

この単純なものはBundle.mainフォルダで正常に動作します。

if let url = Bundle.main.url(forResource: "ipad", withExtension: "html") { 
    do { 
     let contents = try String(contentsOfFile: url.path) 
     webView.loadHTMLString(contents, baseURL: url.deletingLastPathComponent()) 
    } catch { 
    print("Could not load the HTML string.") 
    } 
} 

答えて

0

すべてのHTMLディレクトリ&をプロジェクトに追加します。

let webView = WKWebView() 
if let htmlPath = Bundle.main.path(forResource: "directory/index", ofType: "html") { 
    let htmlUrl = URL(fileURLWithPath: htmlPath, isDirectory: false) 
    webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlUrl) 
    view = webView 
} 

enter image description here

+0

どのように、なぜ失敗し、 "ドキュメント" ディレクトリ内の作業について?私は "Documents"ディレクトリに保存し、インターネットアップデートファイルを許可します。 –

関連する問題