No such module 'UIKit' - xcode

All I want to do is make the most BASIC mac 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)
}}
https://developer.apple.com/documentation/webkit/wkwebview

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

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

Open utilities from the top right corner
Change the platform to IOS
Then it should be okay to use UIKit

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/

Related

Cannot change PlivoLoginAppDelegate to swift in iOS SDK

In your iOS SDK PlivoLogin, there are PlivoLoginAppDelegate.h and PlivoLoginAppDelegate.m.
I want to change it to swift file.
I changed it to the following.
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
class var shared: AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
var providerDelegate: ProviderDelegate?
var window: UIWindow?
#IBOutlet var viewController: ViewController?
var phone: Phone?
// MARK: UIApplicationDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
print("Finished launching with options: \(launchOptions)")
// // Phone
self.phone = Phone()
self.viewController = self.window?.rootViewController as? ViewController
self.viewController?.phone = self.phone
self.phone?.setDelegate(self.viewController)
return true
}
...
}
But the log only says
Creating endpoint
Generating random password
and stops, fails to get a delegate response to - (void)successWithResponse:.
How can we get a delegate response?
Plivo Sales Engineer here.
Plivo iOS SDK has Objective C interface and you can access the Objective-C library from the Swift 3.0. This is a sample app in the Swift 3.0 using the iOS SDK's Objective-C interface. This app makes outgoing calls and gets delegate callbacks in Swift 3.0.

Xcode Mac application (Swift) problems

I am working on a Mac application using Swift.
And I got a lot of problems there cause I thought it would be the same as I did it in the iOS application
I am really sorry about this long post :(
The first problem:
The error: "Method does not override any method from its superclass"
The code:
import Cocoa
class CreateAccountViewController: NSViewController {
#IBOutlet weak var emailTextField: NSTextField!
#IBOutlet weak var passwordTextField: NSSecureTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
The next one:
Error:Value of type 'CreateAccountViewController' has no member 'dismissViewControllerAnimated'
Code:
self.dismissViewControllerAnimated(true, completion: nil)
The last one:
The error: Use of unresolved identifier 'UIAlertController'
The code:
let alert = UIAlertController(title: "Error", message: "Enter Email and Password", preferredStyle: .Alert)
I am so sorry but this is my first time I wrote a Mac application.
UIAlertController is part of UIKit Library which is only available on iOS.
You'll need to use NSAlert to pop an alert in a window on Mac OS X. You also need to do an import AppKit at the top of your Swift file.

Detection of WebView load finish

I am creating a simple app with a WebView basing on this tutorial.
I would like to display a progress indicator while loading the page, but the methods didStartProvisionalLoadForFrame and didFinishLoadForFrame are never called. What am I doing wrong?
ViewController.swift
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet weak var webView: WebView!
#IBOutlet weak var webViewProgressIndicator: NSProgressIndicator!
let messengerUrl = "https://www.messenger.com/"
override func viewDidLoad() {
super.viewDidLoad()
self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: messengerUrl)!))
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
func webView(sender: WebView!, didStartProvisionalLoadForFrame frame: WebFrame!)
{
self.webViewProgressIndicator.startAnimation(self)
}
func webView(sender: WebView!, didFinishLoadForFrame frame: WebFrame!)
{
self.webViewProgressIndicator.stopAnimation(self)
}
}
I am using Xcode 7 Beta and OS X 10.11 Beta.
Rich's answer works on Xcode 7.0.1 ElCaptain Swift2
BUT I had to connect it in the connections inspector as Swift2 did not accept
self.view.frameLoadDelegate = self
as it gave the following error
Cannot assign a value of type 'ViewController' to a value of type
'WebFrameLoadDelegate!'
So as long as you connect it up as follows, all works great.
The tutorial missed out something, Need to add:
self.webView.frameLoadDelegate = self
(can be done in connections inspector)
then call the load..
self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: messengerUrl)!))
Also add the WebFrameLoadDelegate interface to your ViewController.. (thanks tjv)
class ViewController: NSViewController, WebFrameLoadDelegate{ .....
You need to assign the delegate of your UIWebview ie: webview.delegate = self

Minimal Cocoa/Swift menu bar app doesn't want to run

I'm trying to build a skeleton app in Swift where I basically only have a menu bar icon, and no window. Starting from a new Storyboard project in Xcode, it worked initially, but trying to get rid of the window, it doesn't seem to want to run anymore. I have the following:
import Cocoa
import AppKit
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var window = NSWindow()
var statusBar = NSStatusBar.systemStatusBar()
var statusBarItem : NSStatusItem = NSStatusItem()
override func awakeFromNib() {
statusBarItem = statusBar.statusItemWithLength(-1)
statusBarItem.title = "Test"
}
func applicationDidFinishLaunching(aNotification: NSNotification) {
sleep(10);
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
In AppDelegate.swift (based on this tutorial). When running this through Xcode, I get some warnings:
2015-06-23 22:20:28.444 PENCloud[19491:3303755] Failed to connect (colorGridView) outlet from (NSApplication) to (NSColorPickerGridView): missing setter or instance variable
2015-06-23 22:20:28.444 PENCloud[19491:3303755] Failed to connect (view) outlet from (NSApplication) to (NSColorPickerGridView): missing setter or instance variable
From some Googling, it seems like I should be able to ignore these, but my statusBarItem no longer shows up. What am I missing?
You need to have main.swift with the code like below.
import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
var statusBarItem : NSStatusItem!
func applicationDidFinishLaunching(aNotification: NSNotification) {
statusBarItem = statusBar.statusItemWithLength(-1)
statusBarItem.title = "Test"
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
autoreleasepool { () -> () in
let app = NSApplication.sharedApplication()
let delegate = AppDelegate()
app.delegate = delegate
app.run()
}
The file name MUST be main.swift. Otherwise you will get the error, Expressions are not allowed at the top level, on the line of autoreleasepool.
I found the answer here:
https://stackoverflow.com/a/26322464/338986
I have had the same problem before and I found out that when there is no storyboard or something else is missing, the app doesn't even start. I suggest setting up a new project, add your status bar code and then just do two things:
Add the "Application is UIElement (Agent)" key to your info.plist file and set it to true
Go to your storyboard, select the windowcontroller and unckeck "Initial view controller" on the right in the property inspector
If you have any other question regarding a status bar item I'd love to help you

Swift for OsX WebFrameLoadDelegate doesn't works

I am new in OSX developing and i am new in Swift. I want to write simple app with WebView.
I can load url, but I need to catch event when, WebView end loading content.
it's my app delegate.swift file:
import WebKit
class AppDelegate: NSObject, NSApplicationDelegate{
#IBOutlet weak var window: NSWindow!
#IBOutlet var webview: WebView!
func applicationDidFinishLaunching(aNotification: NSNotification?) {
let url = NSURL(string: "google.com")
let request = NSURLRequest(URL: url);
webview.mainFrame.loadRequest(request);
}
func applicationWillTerminate(aNotification: NSNotification?) {
}
}
and i try to make WebFrameLoad delegate class:
import WebKit
class WebViewControllerDelegate: WebFrameLoadDelegate{
#IBOutlet var webview: WebView!
override func didFinishLoadForFrame()
{
println("ok:");
}
}
It doesn't work. And also i don't know how to set this Delegate to my WebView.
TY for answers.
Generally speaking the WebViewControllerDelegate would be the class to have the webView as a property.
Move this code:
#IBOutlet var webview: WebView!
let url = NSURL(string: "google.com")
let request = NSURLRequest(URL: url)
self.webview.mainFrame.loadRequest(request)
to your WebViewControllerDelegate
You then also need to set the delegate for the WebView in your WebViewControllerDelegate
self.webView.delegate = self
If you don't set this, then the system will not call the delegate methods for you.
Finally you need to ensure that your class conforms to the WebFrameLoadDelegate protocol (which you have already done).
class WebViewControllerDelegate: WebFrameLoadDelegate{
The above means that you have a viewController which has a webView property, is conforming to the correct delegate protocol, and is itself the delegate for the webView.
Delegate methods defined inside the WebViewControllerDelegate will then be called for you.

Resources