3

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. I thought this would be easy but I cannot find any easy tutorials to follow, certainly not using XCode 11 and Swift 5.1. I do not want to go down the automated route provided in Mac OS 10.15

When I built the default Hello World App that comes with XCode 11, it will not run on 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

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.

Steve Brooker
  • 1,043
  • 11
  • 28
  • You have to change the deployment target for the project to make it run on earlier macOS versions. Select the project from the left side of the project window to open the project editor. Click the General button to access the section where you can set the deployment target, which is the earliest macOS version that can run the app. – Swift Dev Journal Feb 05 '20 at 22:39
  • I had tried that and found it makes no difference, setting it to 10.12,10.13,10.14. In each case the build fails because of 6 errors in the file Hello World ContentView.swift (e.g. View not available in10.15 etc) and 1 error in the Hello World AppDelegate.swuft (NSHostingView not available in 10.15). The Hello World example has no backwards compatibility with earlier versions of macOS. Shame on Apple! – Steve Brooker Feb 06 '20 at 06:53
  • Xcode defaults to using SwiftUI for new projects. SwiftUI requires 10.15. When creating your project, choose Storyboard from the User Interface menu. Then you'll be able to set the deployment target to something earlier than 10.15. You can't use the Catalyst framework to convert an iOS app to a Mac app if you want to support earlier macOS versions. – Swift Dev Journal Feb 06 '20 at 19:00
  • Thanks Mark that was very helpful. At least I have something that I can add some WKWebView creation code to. – Steve Brooker Feb 06 '20 at 21:46

2 Answers2

5

Step 1 - Install XCode 11 and open it.

Step 2 - Select "File" and "New" and "Project" from the main menu.

Step 3 - Select "App" (top left) and Select "Next" (bottom right).

Step 4 - Give your app a name, for User Interface select "Storyboard" and then select "Next".

Step 5 - Select a folder for where to store your new app on your computer and select "Create".

Step 6 - In XCode Navigator (left hand pane) select "Add Files" from the context menu.

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.

Step 8 - Select "Create folder references for any added folders" and select "Add"

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.

Step 10 - Press "Run" and use your new app.

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.

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 ;
        }
    }
}
Steve Brooker
  • 1,043
  • 11
  • 28
3

Using Xcode 12 and Big Sur I had to go to the project properties, select the Signing & Capabilities tab, and check Outgoing Connections (Client) under the App Sandbox to get the excellent answer from Steve Brooker to work.

droidix
  • 73
  • 5