var fileString = "";
print(CACurrentMediaTime()); // time 1
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
do
{
let helloPath = documentsURL.appendingPathComponent("hello.txt"); // 20MB
fileString = try String(contentsOf:helloPath, encoding:.utf8);
}
catch let error as NSError
{
print("error Result : " + error.localizedDescription)
}
print(CACurrentMediaTime()); // time 2
let evaluateString = "iphoneMessageHandler('" + fileString + "');"; // ★★★ Very Slow!!!
print(CACurrentMediaTime()); // time 3
myWebView.evaluateJavaScript(evaluateString)
{
(result, error) in
print("result : ", result);
print("error : ", error);
}
I want to create an iPhone webview application using xcode on my MacBook. I'm going to read about 20 megabytes of files and send data to webview.
But there's a slow section. As a result of the test, the file read rate took only 0.1 seconds. It took as long as two seconds to create the code string you wanted to run in webview. Running the string in webview took only 0.1 seconds.
Is there a way to speed up the creation of strings? Or I wonder if there's a better way.