20

All I want to do is make the most basic macOS / OS X app that just shows a WKWebView...

import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {

var webView: WKWebView!

override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    view = webView
}
override func viewDidLoad() {
    super.viewDidLoad()
    
    let myURL = URL(string: "https://www.apple.com")
    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)
}}

But this shows the error:

No such module 'UIKit'

pkamb
  • 33,281
  • 23
  • 160
  • 191
Taylor A. Leach
  • 2,115
  • 4
  • 25
  • 43

5 Answers5

45

UIKit is for mobile apps, not MacOS apps. You want AppKit.

cbiggin
  • 1,942
  • 1
  • 17
  • 18
4
  • First of all your detail shows that you are working on MacOS app, but your Tittle is about UIKit.
  • APPKit MacOS development and UIKit is for iOS/TabOS.
  1. UIKit cannot be import for MacOS playground environment. Use AppKit
  2. If you are developing for iOS mobile environment and still face issue importing UIKit then you can follow following steps:

If you already selected iOS while creating Playground file but still getting error No such module 'UIKit', then follow following steps. Xcode 13 enter image description here

enter image description here

enter image description here

Step By Step Action

gsk_fs
  • 94
  • 1
  • 10
  • The question is about building a macOS app, not an iOS app. The error is caused by trying to import UIKit instead of AppKit. – HangarRash Aug 30 '23 at 02:38
2
  • Open utilities from the top right corner
  • Change the platform to IOS

Then it should be okay to use UIKit

Bhavik Modi
  • 1,517
  • 14
  • 29
Orhan Özkerçin
  • 342
  • 1
  • 4
  • 14
  • A helpful screenshot in https://stackoverflow.com/a/49971034/446106 illustrates how to change the platform. Make sure Inspectors (the right-hand sidebar) is open. – mwfearnley Jan 26 '20 at 21:37
  • The question is about building a macOS app, not an iOS app. The error is caused by trying to import UIKit instead of AppKit. – HangarRash Aug 30 '23 at 02:38
1

Check the 'show utilities' window. If the platform is set to MacOS, you'll get this error if you're calling UIKit. Same thing goes if your wanting IoS and your calling AppKit.

http://iosbrain.com/blog/2018/08/16/xcode-9-playground-error-no-such-module-uikit-or-appkit/
Gsuit
  • 11
  • 1
-1

Check what device you are using. If you are using macOS instead of an iPhone 14, you will get this fault when you import UIKit. You do not have to reinstall Xcode.

M--
  • 25,431
  • 8
  • 61
  • 93
  • 1
    This duplicates essentially every other answer and adds no new content. Please don't post an answer unless you actually have something new to contribute. You can show your support for an answer by upvoting. – Ben A. Sep 01 '23 at 23:03