Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSX Swift webview load local file

I know this has been asked many times and I have read quite a few questions and googled for days with no success so far.

I just want to load a local html file in a desktop app, truth is for this project I need a JS library and most of it is already done as a webpage (css, js and html, no server side processing needed). I dont want to force the app to load the webpage from an internet server so as to not force the users to have an internet connection. Needless to say I am completely inexperienced in Swift and apple development.

Right now this is the problem I am having: enter image description here the ide complaints about the params and I cant seem to get them right.

For reference here is a snippet of my latest code:

class AppDelegate: NSObject, NSApplicationDelegate
{
    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var webView: WebView!
    typealias NSSize = CGSize

    func applicationDidFinishLaunching(aNotification: NSNotification?)
    {
        self.window.title = "Chess Study Room"

        var try1 = "main.html";
        println(try1);

        var try2 = NSURL(string: try1)!;
        println(try2);

        var try3 =
        NSBundle.URLForResource("main", withExtension: "html", subdirectory: "web", inBundleWithURL: try2);
        println(try3);

        var try4 = NSBundle.pathForResource("main", ofType: "html", inDirectory: "web");
        println(try4);

        var try5 = NSString.stringByAppendingPathComponent("main.html");
        println(try5);
        // var myInternalUrl = NSURL(string: myInternalHtml)!;
        //NSLog("%s", myInternalHtml!);
        var request = NSURLRequest(try1,
            NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
            60
        );

        self.webView.frameLoadDelegate = self;
        self.webView.mainFrame.loadRequest(
            request!
        );
    }

But as you can see from my gif I've tried other things as well. The full errors are /path/TestWebView/AppDelegate.swift:32:35: Extra argument in call and /path/TestWebView/AppDelegate.swift:32:36: Missing argument for parameter 'cachePolicy' in call

At this point try1 and try2 output "main.html", try3 and try4 output nil and try5 outputs"(Function)"

The structure of folders is this: enter image description here

I added the folder "web" as a reference (as advised in another question) but I doubt this would work to ship just one package...

I dont know if there's any difference but I'm not targetting iOS, I want this to be a desktop app.

Any advise will be greatly appreciated

like image 801
Purefan Avatar asked Oct 19 '22 22:10

Purefan


1 Answers

After reading this post in the apple forums and putting it together with this other question I was able to come up with the following working code:

func applicationDidFinishLaunching(aNotification: NSNotification?)
    {
        self.window.title = "My App Title"

        var try6 = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("main", ofType:"html")!)

        var request = NSURLRequest(URL: try6!);

        self.webView.frameLoadDelegate = self;
        self.webView.mainFrame.loadRequest(request);
    }
like image 154
Purefan Avatar answered Dec 18 '22 03:12

Purefan