微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

macos – OSX Swift webview加载本地文件

我知道这已被多次询问,我已阅读 quite a few questions
并且google了好几天,到目前为止没有成功.

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

在这是我遇到的问题:

关于params和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!
        );
    }

但是从你的gif我可以看到我也尝试了其他的东西.完整的错误是/path/TestWebView/AppDelegate.swift:32:35:调用中的额外参数和/path/TestWebView/AppDelegate.swift:32:36:调用中缺少参数’cachePolicy’的参数

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

文件夹的结构是这样的:

添加文件夹“web”作为参考(在另一个问题中建议),但我怀疑这只能运送一个包…

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

任何建议将不胜感激

解决方法

阅读 this post in the apple forums并将其与 this other question放在一起后,我能够得到以下工作代码

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);
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐