ios - 仅使用商店应用程序时应用程序崩溃 'NSInvalidArgumentException'

标签 ios xcode swift ios7

我有一个 iOS 应用程序,可以在 Appstore 中找到。今天我测试了该应用程序,当应用程序尝试打开错误对话框时,它在我的 iPhone4 (iOS7) 上崩溃了。在 iPhone5s 上没有问题。

当我从 XCode 安装应用程序时,它可以工作...XCode/Appstore 是相同的版本。 我阅读了 XCode 中的控制台来找出应用程序崩溃的原因。我收到此错误:

following error: : *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target

我做错了什么? 当我调用错误对话框时,我这样做:

self.showAlert("Error.", message: "Wrong credentials.", owner: self);

报错方法:

func showAlert(title:NSString, message:NSString,owner:UIViewController) {
    if let gotModernAlert: AnyClass = NSClassFromString("UIAlertController") {
        var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert);
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil));
        owner.presentViewController(alert, animated: true, completion: nil);
    }
    else {
        let alertView = UIAlertView(title: title, message: message, delegate: self, cancelButtonTitle: nil, otherButtonTitles: "OK");
        alertView.alertViewStyle = .Default;
        alertView.show();
    }
}

最诚挚的问候

最佳答案

使用此方法检查类

+ (BOOL) checkIfClassExists:(NSString *)className {
id c = objc_getClass([className cStringUsingEncoding:NSASCIIStringEncoding]);
if (c != nil) {
    return YES;
} else {
    return NO;
}

}

& 快速用作

if ObjCFix.checkIfClassExists("UIAlertController") {
//iOS 8 code here
} else {
//iOS 7 code here

}

关于ios - 仅使用商店应用程序时应用程序崩溃 'NSInvalidArgumentException',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31206371/

相关文章:

xcode - 初始化 cocoa IB 绑定(bind)

ios - 尝试使用闭包在 ViewController 之间传递数据时我犯了什么错误?

ios - 尝试在 MonoTouch.CoreGraphics.CGContext.DrawPDFPage 中进行 JIT 编译方法异常

ios - 无法使用 GCDAsyncSocket 从服务器读取数据

objective-c - 如何将 for 循环中的 int 解析为 xcode 中的方法?

ios - 如何在 ios 中使用 nexmo 向多个号码发送短信?

ios - 如何以编程方式将 UIBarButtonItem 添加到拖到 View 上的 UINavigationBar

swift - 在 ViewController 中为 UITableView 编辑和添加按钮

iphone - iOS写入文件

iphone - EXC_BAD_ACCESS 在 Objective C 中泛洪填充时