I do not see the simulator

When I Run a project I see nothing, the window does not appear. I see no error. I see the "Build succeed" and the general menu of the bar, but no window.


Even when I create a new project, with no code, sometimes I see the window, sometimes I do not. When I see the window, if I add some code, just a few lines, then run, and I cannot see the window anymore. I tried many things and I do not see a pattern, it seems random. I tried to give a different name, use the shortcut, use the button, I am sure that is not hidden behind or something, I compare a project that works and one that does not and I see no difference ...


I can do no work and I have no idea what's going on. Any suggestion?


macOS Catalina 10.15.5

Xcode 11.5

Swift

Replies

Is it a MacOS app ?


If so, there is no simulator, you run code directly as an app on the Mac.


Is it storyboard based or xib based ?


I add some code, just a few lines, then run, and I cannot see the window anymore.

Plesase explain:

- where do you add this code ?

- and show the complete file where you added this code.


if xib:

What are the files you see in the file browser on the left ?

There should be at least:

AppDelegate.swift

MainMenu.xib

info.plist

Assets.xcassets folder

If so, select the window below the menubar in IB.

Is it defined as visible at launch in Attributes inspector ?

In size inspector, check its position on screen and size


if storyboard:

AppDelegate.swift

ViewController.swift

info.plist

Main.storyboard

Assets.xcassets folder

If so, in the storyboard, you should see:

- a window controller scene

- a viewController scene attached to this window

Is the window controller declared as "is initial controller" in Attributes inspector ?


In size inspector, check the position of the window inside windowcontroller on screen and size.




Please answer to all questions so that we can focus better on the root cause

- It is a macOS app. Any macOS app

- Storyboard

- Many times it fails from the beginning without any code. Now I am testing adding small code in class ViewController: NSViewController

- In Storyboard I see a Window Controller and a View Controller Scene

- The Window Controller Attributes > Is initial Controller is checked and I see the initial arrow

- Size Inspector, I have checked the position of the window

Now I am testing adding small code in class ViewController: NSViewController

Could you show the code for the class ?

I have other macOS and iOS projects with the exact same code and still now they work well. I do not know why I cannot do any new one.


Signing & Capabilities > Outgoing Connections (Client)


ViewController.swift:

import Cocoa
import WebKit

class ViewController: NSViewController {

    let webView = WKWebView()
   
    override func loadView() {
        self.view = webView
       
        if let url = URL(string: "https://www.apple.com") {
            let request = URLRequest(url: url)
            webView.load(request)
        }
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()

    }

}

A few points that could explain the problem.


You create webView with no frame.

I do not see where you call loadView

Try



class WebViewController: NSViewController, WKUIDelegate {     // ADD delegate
   
   // REMOVE  let webView = WKWebView()

    var webView: MyWKWebView! 
   
// REPLACE
    override func loadView() {
        super.loadView()

        let webConfiguration = WKWebViewConfiguration()
        webView = MyWKWebView(frame: CGRect(x: 0, y: 0, width: 300, height: 300), configuration: webConfiguration)
        webView.uiDelegate = self

        view = webView
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()
       
        let myURL = URL(string: "https://developer.apple.com/")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }
   
}

This code gives me the same problem:


import Cocoa
import WebKit


class ViewController: NSViewController, WKNavigationDelegate {
   
    var webView: WKWebView!
   
    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "https://www.apple.com")!
        webView.load(URLRequest(url: url))
        webView.allowsBackForwardNavigationGestures = true
    }

}

If I do not make subclass I think I should:

var webView: WKWebView!


If I do that the code works well to make a webview with code if I begin a new project.


To repair the projects with the old code just write that new code does not seem to work. Even then I make Product > Clean Built Folder.

Yes, if you do not subclass, you must declare

var webView: WKWebView!


Here I used the same code as for scroll hiding, but you are right, you can simply have:


class WebViewController: NSViewController, WKUIDelegate {     // ADD delegate 
    
    var webView: WKWebView!  
    
    override func loadView() { 
        super.loadView() 
 
        let webConfiguration = WKWebViewConfiguration() 
        webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 300, height: 300), configuration: webConfiguration) 
        webView.uiDelegate = self 
 
        view = webView 
    } 
    
    override func viewDidLoad() { 
        super.viewDidLoad() 
        
        let myURL = URL(string: "https://developer.apple.com/") 
        let myRequest = URLRequest(url: myURL!) 
        webView.load(myRequest) 
    } 
    
}