簡體   English   中英

從 Xcode 啟動 iOS 模擬器並出現黑屏,隨后 Xcode 掛起且無法停止任務

[英]Launch iOS simulator from Xcode and getting a black screen, followed by Xcode hanging and unable to stop tasks

我在 iOS 模擬器中運行我的基本 iPhone 應用程序時遇到問題(在完成斯坦福 iTunes CS193p 講座時)。

我已經搜索了一段時間(Google 和 SO),但到目前為止找不到解決方案。 有很多類似的錯誤,但解決方案似乎並沒有解決這個問題。

在 Xcode 中,我單擊“運行”。 它編譯和構建成功,啟動 iOS 模擬器,但它永遠不會加載應用程序。 只有頂部的狀態欄。 黑屏。

我只編寫了非常基本的代碼(跟隨講座)並且無法解決這個問題。

為了讓事情更加混亂,我在這些講座之前寫了一個 web 包裝器(UIWebView) ,而且效果很好。 但是代碼幾乎沒有任何區別。 我從頭開始創建的所有新應用程序都因同樣的黑屏問題而失敗。

如果我按下模擬器上的主頁按鈕並啟動該應用程序,它就會顯示出來。 但是 Xcode 似乎並不知道發生了什么。

就好像 Xcode 已經失去了與 iOS 模擬器對話的能力,並假設它正在運行(即使我退出 iOS 模擬器)。 我嘗試退出 Xcode,它要求我停止任務。 然后它就掛了。 所以我必須強制重啟才能退出 Xcode。

我正在使用:OSX 10.8.2 Xcode 4.5.2 iOS 模擬器 6.0

CalculatorAppDelegate.h

#import <UIKit/UIKit.h>

@interface CalculatorAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

CalculatorAppDelegate.m

#import "CalculatorAppDelegate.h"

@implementation CalculatorAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

CalculatorViewController.h

#import <UIKit/UIKit.h>

@interface CalculatorViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *display;

@end

CalculatorViewController.m

#import "CalculatorViewController.h"

@implementation CalculatorViewController

@synthesize display = _display;

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = [sender currentTitle];
NSLog(@"digit pressed = %@", digit);
}

@end

令人驚訝的是,對我有用的是轉到 iOS 模擬器菜單,然后按“重置內容和設置”。 (在 iOS 13 中,它在硬件下)

在重置模擬器之前,首先轉到您的項目“項目導航器”屏幕並在常規->部署信息屏幕下檢查主界面屬性是否正確設置!

在此處輸入圖像描述

如果您使用的是 SwiftUI

如果您從以前版本的 Xcode 11 更新, SceneDelegate willConnectTo session: options connectionOptions初始化:

window現在使用UIWindow(windowScene: windowScene)初始化,它曾經是UIWindow(frame: UIScreen.main.bounds)

在以前的版本中:

    let window = UIWindow(frame: UIScreen.main.bounds)
    window.rootViewController = UIHostingController(rootView: ContentView())
    self.window = window
    window.makeKeyAndVisible()

在新版本中:

    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: ContentView())
        self.window = window
        window.makeKeyAndVisible()
    }

我是 iOS 應用程序開發的新手。 我從一開始就在練習開發 iOS 應用程序,在運行一個非常基本的 Hello World 應用程序時,我也遇到了同樣的問題,即在 iOS 模擬器中構建和運行應用程序后只出現一個黑色的空白屏幕。 不知何故,在努力尋找問題的解決方案時,我不小心在 iOS 模擬器中單擊了 Window--> Scale--> 50%,它確實解決了我的問題。 然后我可以看到我的應用程序的主頁並單擊應用程序圖標我能夠成功運行我的應用程序。 應用程序版本:Xcode 5.1 iOS 模擬器:7.1 OSX:10.9.3

這可能是由於未設置正確的部署信息造成的。 (即如果您的故事板未設置為主界面)

重新啟動計算機應該是您需要做的所有事情。

另一個解決方案是,如果您在 Objective C 項目中以編程方式構建 UI,那么您可能需要添加一些代碼來膨脹視圖,因此您需要添加這 3 行以使窗口的界面與代碼交互(外部 2實際上正在與代碼進行交互,而另一個會將屏幕更改為白色,因此您會知道它可以工作)。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

...

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];


return YES;
}

解決方案1:您可以Quit simulator試。

解決方案 2 (推薦):轉到iOS Simulator -> Reset Content and Settings...

步驟1

這將彈出一條警報,說明'Are you sure you want to reset the iOS Simulator content and settings?'

第2步

如果您願意,請選擇“ Reset ”,否則請選擇Don't Reset按鈕。

請注意,一旦您重置模擬器內容,安裝在模擬器上的所有應用程序都將被刪除,並將重置為初始設置。

如果您應該在 Storyboard 中放開您的入口點,或者只是想更改入口點,您可以在 Interface Builder 中指定它。 要設置新的入口點,您必須首先決定哪個 ViewController 將充當新的入口點,並在 Attribute Inspector 中選擇 Initial Scene 復選框。

你可以試試: http ://www.scott-sherwood.com/ios-5-specifying-the-entry-point-of-your-storyboard/

我為此掙扎了幾個小時。 最后為我解決的是:

sudo xcrun simctl erase all

xcrun simctl shutdown all

如果從另一個項目復制故事板后出現黑屏,請確保您已完成此操作在此處輸入圖像描述

我通過按清理項目解決了這個問題
cmd + shift + k
並退出我的模擬器並重新構建。

我在 iOS 13 中通過設置窗口背景顏色解決了這個問題:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let _ = (scene as? UIWindowScene) else { return }
    if let windowScene = scene as? UIWindowScene {
        window = UIWindow(windowScene: windowScene)
        window?.backgroundColor = .white
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        
        // workaround for svprogresshud
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.window = window
    }
}

我已經設法找到解決這個問題的方法。 它是由這篇博客文章提供的:

http://vandadnp.wordpress.com/2012/03/18/xcode-4-3-1-cannot-attach-to-ios-simulator/

解決方案是按 cmd+shift+, (command, shift 然后逗號 ",").. 加載一些用於發布或調試的選項。

將調試器從 LLDB 更改為 GDB。 這解決了這個問題。

為確保這是模擬器問題,請查看是否可以在不更改任何代碼的情況下使用全新項目連接到模擬器。 試試標簽欄模板。

如果您認為這是模擬器問題,請按 iOS Simulator 菜單。 選擇“重置內容和設置...”。 按“重置”。

我看不到您的 XIB 以及您在 Interface Builder 中連接的 @properties,但也可能是您沒有加載窗口,或者您的窗口沒有加載視圖控制器。

我在 Xcode 上遇到了同樣的問題......啟動應用程序時出現黑屏,沒有調試和點擊會鎖定 Xcode。

我終於找到了問題……在模擬器無法連接到 Xcode 的引導下,我查看了我的 etc/hosts 文件,發現幾個月前為了解決另一個問題,我編輯了主機文件以將 localhost 映射到我的固定IP而不是默認IP......我的價值:

10.0.1.17 本地主機

這應該可以工作,因為那是我的 IP,但是將其更改回默認 IP 固定 Xcode ...

127.0.0.1 本地主機

希望這可以幫助。

我正在按照 doug 的建議(“重置內容和設置”)進行操作,但需要花費大量時間,而且真的很煩人……直到我最近發現完全意外的另一種解決方案,它的速度要快得多,而且到目前為止似乎也有效! 只需在您的模擬器上按 cmd+L 或轉到模擬器菜單“硬件 - > 鎖定”,它會鎖定屏幕,當您解鎖屏幕時,應用程序就像什么都沒發生過一樣工作 :)

我發生的事情是類的類型不是 UIViewController 附加到我的視圖控制器的腳本。 它是針對 UITabController 的.....我錯誤地快速創建了錯誤類型的類....所以請確保該類是否是正確的類型..希望這對某人有所幫助。 我很着急,犯了這個錯誤。

只需單擊 Simulator-> Reset Contents and Settings 即可重置您的模擬器。

你也可以去硬件->重啟,然后硬件->主頁,然后點擊你的應用程序

確保設置了初始視圖控制器

我只有在刪除具有早期 iOS 版本的模擬器后才解決了這個問題。

我只有 iOS 11 sims 的黑色模擬器屏幕。 在嘗試重置、重啟、重新安裝並在我的機器上創建一個全新的用戶帳戶后,我找到了這個解決方案:

defaults write com.apple.CoreSimulator.IndigoFramebufferServices FramebufferRendererHint 3

在此答案中找到: 安裝 Xcode 10 beta 后 Xcode 9 iOS 模擬器變成黑屏

請檢查所有視圖的所有約束並查看完整的故事板。 通常這是因為這個而發生的。

問候, 阿羅拉

如果您最近更新到 Xcode 11 beta 3 並嘗試運行較舊的 SwiftUI 項目,則必須在加載視圖的 SceneDelegate 中進行一些更改,否則在運行 iOS 13 beta 3 的設備上屏幕將保持黑色,這當然包括所有模擬器。

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

    // Use a UIHostingController as window root view controller
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: ContentView())
        self.window = window
        window.makeKeyAndVisible()
    }
}

該行為並不嚴格限於模擬器,但由於大多數人將專門在模擬器上運行測試版軟件,因此它只會在這種情況下發生。 困擾了我好久,希望對你有幫助。

當我在 Xcode 11.1 上進行項目時,我遇到了這個問題。 該黑屏問題可能會出現在 ViewController 之間的任何演示文稿中。

這個答案對我有幫助。 因為模式顯示隨着 iOS 13 的變化而改變。如果你在 iOS 13 之前沒有遇到這個問題,請嘗試在你的 ViewController 顯示之前添加下面的行;

viewController.modalPresentationStyle = .fullScreen

在您的代碼可能如下所示之后;

let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen //or .overFullScreen for transparency
self.present(vc, animated: true, completion: nil)

我正在使用 android studio 開發顫振並運行模擬器,模擬器很暗,我根據接受的答案重置了內容。 但這並沒有解決我的問題。 我的 CPU 以某種方式運行得足夠高,所以我重新啟動了,但沒有解決問題。 我打開 Xcode 並運行我的原生 iOS 應用程序之一,模擬器像往常一樣重新打開。 我關閉了 Xcode 並打開了 Android Studio,然后再次開始開發 Flutter 應用程序。

在此處輸入圖像描述

這就是我遇到這個問題的原因。

出於某種原因,未選中“是初始視圖控制器”(對於我的主視圖控制器),這導致加載時出現黑屏。

希望這可以幫助那里的人!

我注意到我不小心刪除了var window:UIWindow? 來自 AppDelegate 的行。 再次介紹它為我解決了這個問題!

對我來說,只需要搖動設備( Device > Shake )。 奇怪但為我工作。 如果這不起作用,請嘗試重置( Device > Erase All Content and Settings ),然后再次搖動設備。

在 Macbook Air 進入睡眠狀態后,2021 年 12 月使用 iOS 13.7 模擬器遇到了這個問題。 模擬器屏幕是黑色的,沒有生命跡象。 重置模擬器沒有幫助。 關閉所有窗口並重新啟動筆記本電腦並沒有幫助。 最終, syspolicyd占用了 350% 的 CPU。 殺死這個進程解決了這個問題。

對我來說,崩潰的模擬器是帶有 iOS 15.2 的 iPhone 13。 沒有一個解決方案奏效。

每當出現此問題時,進程“syspolicyd”的 CPU 使用率也會上升,但終止該進程也無濟於事。

將設備更改為其他設備(文件 > 打開模擬器 > iOS 15.2 > iPhone 13 Pro Max)解決了該問題。 之后,我再次啟動了之前的設備(iPhone 13),它也可以正常工作。

我也遇到了同樣的問題,但令人驚訝的是對我有用的是

  • 打開 Xcode 並轉到 Preferences > Components
  • 然后,如果您尚未安裝 ios 版本,則從那里安裝(例如,在 iOS 15.2 版本的 iPhone 12 上運行所需的 iOS 15.2)
  • 完成安裝后,然后單擊“立即檢查並安裝”按鈕
  • 關閉窗口並現在運行模擬器(您已經安裝了版本)

在 Project - Build Sittings 檢查 info.plist 的正確路徑

將 info.plist 放入搜索欄,找到“info.plist 文件”行

如果您將信息文件移動到另一個文件夾中。

我有 Xcode 13.2.1

對我有用的是模擬器菜單。 轉到DeviceErase All Content and Settings...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM