Cannot load a local html file into a WKWebView on device only (works on simulator)
OK I have found "why" it was not working. I changed this:
[self.webView loadFileURL:URL allowingReadAccessToURL:URL];
to this:
[self.webView loadFileURL:URL.absoluteURL allowingReadAccessToURL:rootAppURL];
with rootAppURL
being:
NSString *rootappPath = [NSFileUtility pathRelativeToContentDirectoryForSubpath:@"/www"];
NSURL *rootAppURL = [NSURL fileURLWithPath:rootappPath isDirectory:YES];
And it now works on ios < 13
iOS: WKWebView does not load local JS files immediately
WKWebView initialization and first load are slow. It is better you initialize and load WKWebView before the view controller containing your WKWebView is loaded. You can use below project to check how they have achieved this for your reference and also they have done comparison of time taken to load.
https://github.com/bernikovich/WebViewWarmUper
Also hope that loading an animation is not taking much time.
Swift WKWebView Loading local file not working on a device
Thank you for any one who tried to answer my question. I have released that this is an error with the WebKit lib that Apple are trying to fix. However I have found a good workaround that required little work.
I open the local file and read its content and then send that string into a webView.loadHTMLString method that compiles the hmtl that was in the file. That way you avoid the issues with iOS not being able to find the path to the local file.
Here is an example of reading a file and then opening it for any one who has the same issues:
let path2 = NSBundle.mainBundle().pathForResource("index", ofType: "html")
var text = String(contentsOfFile: path2!, encoding: NSUTF8StringEncoding, error: nil)!
webView!.loadHTMLString(text, baseURL: url)
Kind regards,
Dimitar
WKWebView cannot load local resource in HTML
Add all the HTML directories & files in your project.
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
}
WKWebView not loading slider images
Add following key into info.plist if not added
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Related Topics
How to Record Audio on Iphone With Avaudiorecorder
How to Install iOS 6 Sdk on Xcode 5
How to Add Nsapptransportsecurity to My Info.Plist File
Change User Agent in Uiwebview
What Are Different Types of Notifications in iOS and How to Configure Them Correctly
Iphone 6 Plus Resolution Confusion: Xcode or Apple'S Website? For Development
Can You Build Dynamic Libraries For iOS and Load Them At Runtime
Objective-C and Swift Url Encoding
Objective-C Arc: Strong VS Retain and Weak VS Assign
Programmatically Set the Initial View Controller Using Storyboards
How to Apply Gradient to Background View of iOS Swift App
How to Create a Uicolor from a Hex String
Uploading File With Parameters Using Alamofire
How to Convert String Date to Nsdate
Prevent Mobile Safari from Presenting Toolbar When Bottom of Viewport Is Tapped
How to Get Height of Uitableview When Cells Are Dynamically Sized
Xcode Command /Usr/Bin/Codesign Failed With Exit Code 1: Errsecinternalcomponent