ios - 更改照片设置后应用程序崩溃

标签 ios swift

我有一个应用程序需要访问您设备上的照片。我检查设备状态,如果他们拒绝访问,我会触发一个模式,警告他们没有提供必要的访问权限,然后让他们选择进入设置并更正选择。

发生这种情况时,我的应用程序崩溃并出现以下错误:

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.

这是我触发重定向的代码。有什么想法可能导致此问题或建议我应该如何做得更好?

let title = "You didn't allow us to view your photos!"
let message = "Without access, we cannot help you add photos from your device."
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

alertController.addAction(UIAlertAction(title: "I know!", style: UIAlertActionStyle.Cancel, handler: nil))
alertController.addAction(UIAlertAction(title: "Settings (Required)", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction) -> Void in

    let settingsURL = NSURL(string: UIApplicationOpenSettingsURLString)
    UIApplication.sharedApplication().openURL(settingsURL!)
}))

self.presentViewController(alertController, animated: true, completion: nil)

此代码是在我从以下位置调用的方法中调用的:

PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) -> Void in
    if(status == .Authorized){
        self.getPhonePhotos()
    }else{
        self.showDeniedPhotosPopup()
    }
})

提前致谢。

更新

我刚刚意识到了一些我之前没有意识到的事情。仅当我激活设置中的“照片”开关时,代码才会崩溃。导航本身不会导致应用程序崩溃,它会在应用程序仍在运行时更改照片设置配置。为了测试我的理论,我从未触发弹出窗口,只是进入设置,然后激活照片开关,应用程序崩溃了。因此,崩溃肯定是由照片设置的更改引起的。

最佳答案

从错误看来,您正在一个不是主线程的线程上呈现 View Controller ,从而导致在后台线程上的新 View Controller 上实现 UI 内容(自动布局等)。

尝试将 View Controller 呈现代码封装在主队列上。像这样的事情:

weak var aBlockSelf = self

dispatch_async(dispatch_get_main_queue(), {
    aBlockSelf.presentViewController(alertController, animated: true, completion: nil)
})

如果仍然崩溃,请分析您的应用程序以找出确切的罪魁祸首。

关于ios - 更改照片设置后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33459198/

相关文章:

ios - 字符串不符合协议(protocol) "intervalType"

ios - Swift 无法解析 JSON : Could not cast value of type '__NSCFDictionary' to 'NSArray' error

ios - 在 Swift 中寻找像 Uber 这样的司机动画

iOS 8 文本字段色调颜色

ios - 具有动态原型(prototype)内容和多部分的 UITableView

ios - 如何使用 AudioServicesPlaySystemSound(1336) 不振动

ios - "uncaught exception"是什么?

ios - 如何在新 View 中显示 UISearchController 结果而不显示 tableView 的背景?

ios - 带有 NSMutableArray 的表的奇怪行为

ios - 我如何通过用户按下按钮来隐藏按钮