2017-01-18 6 views
-1

ためFileManagerのに働いていない私のコードです:はWKWebViewがここdocumentDirectory

var fileContent = "" 
let baseURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! 
let fileURL = baseURL.appendingPathComponent("index").appendingPathExtension("html") 
do { 
    fileContent = try String(contentsOf: fileURL, encoding: String.Encoding.utf8) 
} catch let error as NSError { 
    print("Failed getting content of the file: \(fileURL), Error: " + error.localizedDescription) 
    fileContent = "" 
} 

editorWebView!.loadHTMLString(fileContent, baseURL: baseURL) 

問題は、次のとおりです。

<img src='resources/check-box-uncheck.png'> 

WKWebView負荷HTML文字列が、画像が表示されません。 FileManagerを使用して、index.htmlファイルと関連ファイルをdocumentDirectoryにコピーしました。私はmainBundleを使いたくない。 助けてください

ベースURLは次のとおりです。 ファイル:ファイルやフォルダをチェックする/// VAR /モバイル/コンテナ/データ/アプリケーション/ C89D5B66-30FD-4B88-949F-F591E0EA1BE7 /ドキュメント/

機能: FUNC allFilesList( atPath:URL) - > [文字列]? {

var allFileNames = [String]() 
    do { 
     let fullPaths = try self.contentsOfDirectory(at: atPath, includingPropertiesForKeys:[], options: FileManager.DirectoryEnumerationOptions.skipsHiddenFiles) 
     for fileName in fullPaths { 
      let theFileName = fileName.lastPathComponent 
      allFileNames.append(theFileName) 
     } 
    } catch let error { 
     print("Error for list folders OR notes: \(error.localizedDescription)") 
    } 
    return allFileNames 
} 
+0

あなたはデリゲートプロトコルを実装しましたか?そこにあるメソッドの1つが、どこに何がロードできるかを制御します。 – Mgetz

+0

@Mgetzはい、しました。 editorWebView!.uiDelegate = selfそしてクラスWKUIDelegateに含まれています – mriaz0011

+0

@mattファイルをチェックする方法があります。 – mriaz0011

答えて

0

私のコードにバグが見つかりました。私はコピーするファイルを追加しませんでした。

let fileURL = urlPath.appendingPathComponent("index").appendingPathExtension("html") 

上記の行を追加すると問題が解決しました。私が実装

実際の方法は次のとおりです。

//To copy file in userDocuments 
    func copyFileFromThisAppMainToUserDocument(fName: String, fExtension:String) -> Bool { 
     let fileManager = FileManager.default 
     let urlPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! 
     let fileURL = urlPath.appendingPathComponent(fName).appendingPathExtension(fExtension) 
     if let bundleURL = Bundle.main.url(forResource: fName, withExtension: fExtension) { 
      // if file exist, copy it 
      do { 
       try fileManager.copyItem(at: bundleURL, to: fileURL) 
       return true 
      } catch let error as NSError { // Handle the error 
       print("\(fName) copy failed! Error:\(error.localizedDescription)") 
       return false 
      } 
     } else { 
      print("\(fName) not exist in bundle folder") 
      return false 
     } 
    } 
関連する問題