Here's what I currently have, which works on iOS.
It is a simple test trial, of getting a WebView to load up on iOS. This is not supposed to be put on the AppStore or anything like that - so please don't offer help by linking me to articles on stackoverflow about "how Apple will not allow it". I just want to run this app locally.
I started on this, because I was unable to figure out how to get a similar thing going on macOS.
The code that I have now is the following - basically pasted and modified from multiple iOS WKWebView Swift code tutorials.
ViewController.swift
import UIKit
import WebKit
let webView = WKWebView()
class ViewController: UIViewController {
override func loadView() {
self.view = webView
if let url = URL(string: "https://daringfireball.net") {
let request = URLRequest(url: url)
webView.load(request)
}
}
override func viewDidLoad()
{
super.viewDidLoad()
let request = URLRequest(url: URL(string: "https://daringfireball.net")!)
webView.load(request)
}
}
Now, here's my question: How do I change this so that it would work on macOS Catalina, so I could save this as an .app and run it. Again, not trying to get it the AppStore - just for local use.
I take it that macOS does not have "UIKit". Also, is it redundant to have two places in the code where the same URL is specified, should there just be one override?
You're almost there :)
Here's the code you need to add to a macOS project to have it load a webpage
import Cocoa
import WebKit
class ViewController: NSViewController {
let webView = WKWebView()
override func loadView() {
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let request = URLRequest(url: URL(string: "https://daringfireball.net")!)
webView.load(request)
}
}
You are absolutely right that macOS does not have UIKit
, instead you use Cocoa
as you can see here.
I've taken the liberty of removing one of your "load webpage" code snippets as you question yourself. Now we just load it in viewDidLoad
.
If you create a new macOS project in Xcode and paste the above and then run your project.....nothing happens :(
That is because we need to do one final thing.
So go to your project overview and select the "Signing & Capabilities" pane.
You need to check "Outgoing Connections (Client)" under App Sandbox as shown here
Hopefully you'll see your webpage loaded now.
Hope that helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.