从Xcode启动iOS模拟器并获得黑屏,然后Xcode挂起,无法停止任务

Mat*_*att 73 xcode ios ios-simulator

我在iOS模拟器中运行我的基本iPhone应用程序(在浏览斯坦福iTunes CS193p讲座时遇到问题).

我一直在搜索(谷歌和SO),但到目前为止还无法找到解决方案.有许多类似的错误,但解决方案似乎并没有解决这个问题.

在Xcode中,我点击"运行".它成功编译和构建,启动iOS模拟器,但它永远不会加载应用程序.只有顶部的状态栏.黑屏.

我只编写了非常基本的代码(跟随讲座)并且无法解决这个问题.

为了更多地混淆问题,我(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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

CalculatorViewController.h

#import <UIKit/UIKit.h>

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

@end
Run Code Online (Sandbox Code Playgroud)

CalculatorViewController.m

#import "CalculatorViewController.h"

@implementation CalculatorViewController

@synthesize display = _display;

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

@end
Run Code Online (Sandbox Code Playgroud)

dou*_*oug 78

令人惊讶的是,对我有用的是iOS模拟器菜单,然后按"重置内容和设置".

  • Jep,这对我有用!对于那些找不到该选项的人:启动模拟器,然后在菜单栏(屏幕顶部)中选择`iOS-Simulator - >重置内容和设置`. (11认同)
  • 没有帮助我:(( (6认同)
  • @mimic 尝试运行 `xcrun simctl shutdown all` (2认同)

Chr*_*ris 41

在重置模拟器之前,首先进入项目" 项目导航器 "屏幕,然后在常规 - > depoyment info屏幕下检查主界面 属性是否正确设置!

在此输入图像描述


Yon*_*aor 11

我为此苦苦挣扎了几个小时。最后为我解决的是:

sudo xcrun simctl erase all

xcrun simctl shutdown all


小智 9

建议的方法都没有解决我面临的问题,我能够自己理解并解决问题。我先把我的做法说出来,然后解释我的问题。

首先,我通过终端启用了开发人员模式和开发安全工具:

defaults write com.apple.dt.Xcode DVTEnableCoreDevice enabled

sudo DevToolsSecurity -enable
Run Code Online (Sandbox Code Playgroud)

其次,我删除了模拟器缓存:

sudo rm -rf ~/Library/*/CoreSimulator
Run Code Online (Sandbox Code Playgroud)

第三,我强制退出(杀死)我的模拟器应用程序服务:

sudo killall -9 com.apple.CoreSimulator.CoreSimulatorService      
Run Code Online (Sandbox Code Playgroud)

第四,我从 Xcode 顶部菜单栏附加核心模拟器服务:

在此输入图像描述

最后,为了确保一切正常,我重新启动了系统。然后我运行 Xcode 并且能够毫无问题地运行模拟器。

所以我的问题是模拟器应用程序无法连接到 Xcode,并且在尝试时挂起。


小智 8

我是iOS应用程序开发的新手.我从一开始就练习开发iOS应用程序,在运行一个非常基本的Hello World应用程序时,我也面临同样的问题,即在iOS模拟器中构建和运行应用程序后只出现黑屏.不知怎的,在努力寻找问题的解决方案时,我不小心点击了iOS模拟器中的Window - > Scale - > 50%,它确实解决了我的问题.然后,我可以通过我的应用程序看到主页,然后单击应用程序图标,我就能够成功运行我的应用程序.应用程序版本:Xcode 5.1 iOS模拟器:7.1 OSX:10.9.3


Lol*_*olo 5

您只需要重新启动计算机即可。


小智 5

原因可能是未设置正确的部署信息。(即,如果您的故事板未设置为主界面)


siv*_*ivi 5

另一个解决方案是,如果您在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;
}
Run Code Online (Sandbox Code Playgroud)


Jay*_*bey 5

解决方案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按钮。

请注意,一旦重置模拟器内容,模拟器上安装的所有应用程序都将被删除,并将重置为初始设置。


bau*_*sic 5

如果您使用的是SwiftUI

如果您要从Xcode 11的早期版本进行更新,则对SceneDelegate willConnectTo session: options connectionOptions初始化有一些更改:

window现在,主要UIWindow(windowScene: windowScene)使用来初始化main。UIWindow(frame: UIScreen.main.bounds)

在以前的版本上:

    let window = UIWindow(frame: UIScreen.main.bounds)
    window.rootViewController = UIHostingController(rootView: ContentView())
    self.window = window
    window.makeKeyAndVisible()
Run Code Online (Sandbox Code Playgroud)

在新版本中:

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


Irf*_*ani 5

对我有用的是删除 Xcode 和模拟器的缓存。无需卸载它们。

转到~/Library/Developer/Xcode删除、缓存、ios 设备日志等(所有临时的东西,在那里很清楚哪些东西只是临时的)。

然后转到~/Library/Developer/CoreSimulator并对所有临时文件执行相同的操作。

请注意,很可能这不是您的项目的问题,而是 xcode 和模拟器的问题。