OSX Swift webview加载本地文件

Pur*_*fan 5 macos local webview swift

我知道这已经被问了很多次,我已阅读相当 一个 问题 ,并用Google搜索,但没有成功,到目前为止天。

我只想在桌面应用程序中加载一个本地 html 文件,事实是这个项目我需要一个 JS 库,其中大部分已经作为网页完成(css、js 和 html,不需要服务器端处理)。我不想强制应用程序从互联网服务器加载网页,以免强迫用户连接互联网。不用说,我在 Swift 和苹果开发方面完全没有经验。

现在这是我遇到的问题: 在此处输入图片说明 关于参数的 ide 抱怨,我似乎无法理解它们。

作为参考,这里是我最新代码的片段:

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!
        );
    }
Run Code Online (Sandbox Code Playgroud)

但正如你从我的 gif 中看到的,我也尝试了其他的东西。完整的错误是/path/TestWebView/AppDelegate.swift:32:35: Extra argument in call/path/TestWebView/AppDelegate.swift:32:36: Missing argument for parameter 'cachePolicy' in call

此时try1和try2输出“main.html”,try3和try4输出nil,try5输出“(Function)”

文件夹的结构是这样的: 在此处输入图片说明

我添加了文件夹“web”作为参考(如另一个问题中所建议的),但我怀疑这是否可以只运送一个包裹......

我不知道是否有任何区别,但我不是针对 iOS,我希望这是一个桌面应用程序。

任何建议将不胜感激

Pur*_*fan 3

在阅读了苹果论坛中的这篇文章并将其与其他问题放在一起之后,我能够得出以下工作代码:

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);
    }
Run Code Online (Sandbox Code Playgroud)