Hello, I've been searching all over for this for days and I can't find the solution as I'm kind of illiterate with code so I have been reading and watching tutorials. Anyway, I have a webapp I created that is in HTML, CSS, and javascript. I have all the local files as I want it built-in to my xcode project. I am having trouble getting the code to work and this is what I have in the ViewController.swift All I am trying to do it make a simple app that can interact with my javascript using WKWebview tool.
This is what i have came up with and tried several things but no matter what I get a "SIGABRT" error. I think I am not telling it correctly where my files are located? Can someone please help? I have searched all over the web and apple documentation but since I don't know the code I don't really understand the language of what I am doing wrong. I tried looking at all the sub link apple documention and it shows you what to use but not really how to use it. So I feel like I am using it wrong...
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var loadhtml: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let webView = WKWebView()
let htmlPath = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "www")
let htmlUrl = URL(fileURLWithPath: htmlPath!)
let request = URLRequest(url: htmlUrl)
loadhtml.load(request)
webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlUrl)
view = webView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// Do any additional setup after loading the view.
}
I have a "www" folder I added to the main file that has the index.html file in it along with a javascript file and also subolders with the images, CSS, etc..
I am getting frustrated trying to fix it.
Thank you in advance...
You’re definitely approaching this from the right direction. Some suggestions:
Make sure that your
directory gets copied to your app correctly. To start, when you add it to the target, make sure to select “Create folder references” and not “Create groups”. Also, use the Finder to inspect the built app to confirm that everything is where you expect it to be.www
Get your URL directly, rather than bouncing through a path, using:
Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "www")
.
Remove the
and everything that references it. You must useURLRequest
.loadFileURL(_:allowingReadAccessTo:)
Apropos that, pass the parent directory to the
parameter:allowingReadAccessTo
webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlUrl.deletingLastPathComponent())
.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"