简体   繁体   English

如何为 MacOS 创建 WKWebView 应用程序

[英]How do I create a WKWebView App for MacOS

I have an HTML5 App developed in XCode in the IOS App Store and I wish to create a copy of that app for the Mac App Store.我在 IOS App Store 中有一个用 XCode 开发的 HTML5 应用程序,我希望为 Mac App Store 创建该应用程序的副本。 I thought this would be easy but I cannot find any easy tutorials to follow, certainly not using XCode 11 and Swift 5.1.我认为这很容易,但我找不到任何简单的教程来遵循,当然不是使用 XCode 11 和 Swift 5.1。 I do not want to go down the automated route provided in Mac OS 10.15我不想走 Mac OS 10.15 中提供的自动路线

When I built the default Hello World App that comes with XCode 11, it will not run on 10.14!当我构建 XCode 11 附带的默认 Hello World 应用程序时,它不会在 10.14 上运行! I do not want to upgrade to 10.15 yet, primarily because I want the app to work on earlier versions of MacOS我还不想升级到 10.15,主要是因为我希望该应用程序可以在早期版本的 MacOS 上运行

Anyone who can provide me with a reference to a working example of a WKWebView App loading local files that runs in XCode 11 on Max OS 10.14 will be my hero.任何可以向我提供 WKWebView 应用程序加载本地文件的工作示例的参考的人都将成为我的英雄。

Step 1 - Install XCode 11 and open it.步骤 1 - 安装 XCode 11 并打开它。

Step 2 - Select "File" and "New" and "Project" from the main menu.步骤 2 - 从主菜单中选择“文件”、“新建”和“项目”。

Step 3 - Select "App" (top left) and Select "Next" (bottom right).第 3 步 - 选择“应用程序”(左上角)并选择“下一步”(右下角)。

Step 4 - Give your app a name, for User Interface select "Storyboard" and then select "Next".第 4 步 - 为您的应用命名,为用户界面选择“故事板”,然后选择“下一步”。

Step 5 - Select a folder for where to store your new app on your computer and select "Create".步骤 5 - 选择一个文件夹,用于在您的计算机上存储您的新应用程序,然后选择“创建”。

Step 6 - In XCode Navigator (left hand pane) select "Add Files" from the context menu.第 6 步 - 在 XCode Navigator(左侧窗格)中,从上下文菜单中选择“添加文件”。

Step 7 - Select the folder containing your html/javascript/css/image files - in this example I assume the folder will have the name "www" but it can be anything - just remember to change "www" in the code shown below to what you want.第 7 步 - 选择包含 html/javascript/css/image 文件的文件夹 - 在本例中,我假设该文件夹的名称为“www”,但它可以是任何名称 - 只需记住将下面显示的代码中的“www”更改为你想要什么。

Step 8 - Select "Create folder references for any added folders" and select "Add"步骤 8 - 选择“为任何添加的文件夹创建文件夹引用”并选择“添加”

Step 9 - Select "ViewController.swift" from the Navigator pane and replace everything with with code shown below, changing "www" to the folder name containing your html etc and changing "AppName" to the name of your html file.第 9 步 - 从导航器窗格中选择“ViewController.swift”并将所有内容替换为如下所示的代码,将“www”更改为包含您的 html 等的文件夹名称,并将“AppName”更改为您的 html 文件的名称。

Step 10 - Press "Run" and use your new app.第 10 步 - 按“运行”并使用您的新应用。

For how to publish it and add other functionality (such as in app purchases) refer to Apple Developer and other internet resources/stack overflow questions.有关如何发布它和添加其他功能(例如在应用程序购买中),请参阅 Apple Developer 和其他互联网资源/堆栈溢出问题。

import Cocoa
import WebKit

class ViewController: NSViewController, WKUIDelegate
    {
    var webView: WKWebView!

    override func loadView()
        {
        let webConfiguration = WKWebViewConfiguration ();
        webConfiguration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs");
        webView = WKWebView (frame: CGRect(x:0, y:0, width:800, height:600), configuration:webConfiguration);
        webView.uiDelegate = self ;
        view = webView;
        }

    override func viewDidLoad() {
    super.viewDidLoad()

    if let url = Bundle.main.url ( forResource: "AppName"
                                 , withExtension: "html"
                                 , subdirectory: "www")
        {
        let path = url.deletingLastPathComponent();
        self.webView.loadFileURL ( url
                                 , allowingReadAccessTo: path);
        self.view = webView ;
        }
    }
}

使用 Xcode 12 和 Big Sur,我必须转到项目属性,选择签名和功能选项卡,然后在App Sandbox下选中Outgoing Connections (Client)才能从 Steve Brooker 那里获得出色的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当通过 macOS 本机应用程序内的 SwiftUI WKWebView 调用时,如何让文件打开对话框工作? - How do I get the file open dialog to work, when called through a SwiftUI WKWebView inside a macOS native app? 如何修改 iOS WKWebView Swift 代码以在 macOS 上执行相同操作? - How to modify a iOS WKWebView Swift code to do the same on macOS? 如何在 MacOS WKWebView 中支持 WebAuthN? - How to support WebAuthN in MacOS WKWebView? 如何在 MacOS 的 SwiftUI 中创建多行文本字段? - How do I create a Multi Line Text Field in SwiftUI for MacOS? 如何在没有 IDE 的情况下编写 macOS 应用程序? - How do I write a macOS app without an IDE? 如何授予我的 MacOS 应用程序权限以创建“WAL”文件以使用 journal-mode=WAL 打开 SQLite 数据库? - How do I give my MacOS app permission to create "WAL" file to open an SQLite DB with journal-mode=WAL? 如何从我的 Python 程序中制作 macOS 应用程序? - How do I make a macOS app out of my Python program? 如何使用 SwiftUI 向 macOS 应用程序添加工具栏? - How do I add a toolbar to a macOS app using SwiftUI? 如何在 SwiftUI macOS App 中限制 window 实例? - How do I limit window instance in a SwiftUI macOS App? 如何让 Fastlane Match 在证书存储库中创建和存储 MacOS 应用程序的 mac_installer_distribution 证书? - How do I get Fastlane Match to create & store the mac_installer_distribution certificate for a MacOS app in the certs repo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM