No such module 'UIKit'
Asked Answered
G

5

23

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'

Gardening answered 24/10, 2017 at 3:48 Comment(0)
D
53

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

Danicadanice answered 24/10, 2017 at 3:53 Comment(5)
Okay. So how do I create a simple WKWebView for my mac app... not for iOS. The code above is clearly for iOS I guess.Gardening
Delete the import UIKit and import AppKit & Cocoa.Danicadanice
Now I have 6 errors... including "Use of undeclared type 'UIViewController'"Gardening
UIViewController is part of UIKit. You can't use any UIKit class. In this case UIViewController's counterpart in AppKit is NSViewControllerContemptuous
I have a mobile app. I'm getting this for a pod.Neritic
B
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

Bitterroot answered 22/2, 2022 at 12:25 Comment(1)
The question is about building a macOS app, not an iOS app. The error is caused by trying to import UIKit instead of AppKit.Hebraism
H
2
  • Open utilities from the top right corner
  • Change the platform to IOS

Then it should be okay to use UIKit

Hollow answered 25/7, 2018 at 23:43 Comment(2)
A helpful screenshot in https://mcmap.net/q/141901/-xcode-playground-gets-stuck-on-39-running-playground-39-or-39-launching-simulator-39-and-won-39-t-run-the-code-what-to-do illustrates how to change the platform. Make sure Inspectors (the right-hand sidebar) is open.Neomineomycin
The question is about building a macOS app, not an iOS app. The error is caused by trying to import UIKit instead of AppKit.Hebraism
K
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/
Kangaroo answered 30/11, 2018 at 4:0 Comment(0)
S
-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.

Suzetta answered 29/8, 2023 at 20:9 Comment(2)
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.Todo
My software does not show the dialog screen like the one in the screenshot. So I posted to how I made it work with my version. Or it is available but I could not find it. Sorry this year one for me.Suzetta

© 2022 - 2024 — McMap. All rights reserved.