近期在bug后臺(tái)收到一條NSInternalInconsistencyException導(dǎo)致閃退的記錄瘾英。
出錯(cuò)堆棧如下:-[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] Inconsistent state
同時(shí)注意到該錯(cuò)誤全部發(fā)生在iPad上。
最后查到原因是在iPad上開啟了畫中畫的情況下胰舆,使用AVCaptureSession拍照由于session處于被interrupt的狀態(tài)熊响,所以會(huì)導(dǎo)致閃退昌抠。還有其他的原因例如打電話锄贷,鬧鈴,處在分屏模式下等牢酵。
解決辦法:在session startRunning前添加AVCaptureSessionWasInterrupted通知的觀察悬包。樣例代碼如下:
#pragma mark - Session Notifications
- (void) addObservers
{
/*
A session can only run when the app is full screen. It will be interrupted
in a multi-app layout, introduced in iOS 9, see also the documentation of
AVCaptureSessionInterruptionReason. Add observers to handle these session
interruptions and show a preview is paused message. See the documentation
of AVCaptureSessionWasInterruptedNotification for other interruption reasons.
*/
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionWasInterrupted:) name:AVCaptureSessionWasInterruptedNotification object:self.session];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionInterruptionEnded:) name:AVCaptureSessionInterruptionEndedNotification object:self.session];
}
- (void) removeObservers
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void) sessionWasInterrupted:(NSNotification*)notification
{
AVCaptureSessionInterruptionReason reason = [notification.userInfo[AVCaptureSessionInterruptionReasonKey] integerValue];
NSLog(@"Capture session was interrupted with reason %ld", (long)reason);
//在此禁止掉拍照功能,同時(shí)添加提醒
if (reason == AVCaptureSessionInterruptionReasonAudioDeviceInUseByAnotherClient ||
reason == AVCaptureSessionInterruptionReasonVideoDeviceInUseByAnotherClient) {
}
else if (reason == AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableWithMultipleForegroundApps) {
}
else if (@available(iOS 11.1, *)) {
if (reason == AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableDueToSystemPressure) {
}
} else {
// Fallback on earlier versions
}
}
- (void) sessionInterruptionEnded:(NSNotification*)notification
{
//在此可以恢復(fù)拍照功能
}
具體可以查看蘋果的樣例代碼