How do I create a WKWebView App for MacOS
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 ;
}
}
}
WKWebView popup is not working in MacOS app
Note: You won't be able to tap few links on the WebSite, because they would be HTTP instead of HTTPS. WKWebView by default blocks all HTTP requests which aren't secure.
To bypass, just add NSExceptionAllowsInsecureHTTPLoads as true in info.plist file.
override func loadView() {
webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
webView?.uiDelegate = self
webView?.navigationDelegate = self
view = webView
}
func webView(_: WKWebView, createWebViewWith _: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures _: WKWindowFeatures) -> WKWebView? {
self.webView?.load(navigationAction.request)
return nil
}
macOS app crash on WKWebView() with EXC_BREAKPOINT on M1
I have the same problem. The only workaround that works for me is to run XCode under Rosetta (in /Applications open settings of XCode and set Rosetta checkbox) and then build and run app on target My mac (Rosetta)
In this case all code will compile for x86_64 which doesn't have the problem with WKWebView
.
Related Topics
Convert Single File to Swift 3 in Xcode 8
Swift 2 Mkmapviewdelegate Rendererforoverlay Compiler Warning
Geocodeaddressstring Not Working for Swift
Localizing The Reading of Emoji on iOS 10.0 or Higher
How to Restore In-App Purchases Correctly
Uitextview Contentoffset Is Set
Key Data Not Storing to Icloud with Nsubiquitouskeyvaluestore.Defaultstore
How to Create a Single Page Vertical Scrolling Pdfview in Swift
How to Invoke Method with Cvalistpointer Parameters in Swift
Is There Any Difference at All Between Suffix(From:) and Dropfirst(_:)
Passing in Variable Number of Args from One Function to Another in Swift
Using Multiple Hosting Controllers in Swiftui on Watchos
What's The Difference Between [String] VS. [(String)]
Instantiating a Nested Class Using Nsclassfromstring in Swift