我跟着How to get the status of bluetooth (ON/OFF) in iphone programatically的代碼來獲取藍牙的狀態。Objective C - CBCentralManager NSInternalInconsistencyException退出ViewController時
但是,當我通過後退導航按鈕彈出viewController時,應用程序崩潰,出現以下錯誤。
[CoreBluetooth] XPC connection invalid
***終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,原因:「一個實例0x170261c80類CBCentralManager被釋放,而鍵值觀察家仍用它註冊。現有的觀測信息:(背景:0x1aa9c3710,物業:0x170056b90>上下文:0x1aa9c3710,物業:0x170056b00>上下文:0x1aa9c3710,物業:0x170056b90>上下文:0x1aa9c3710,物業:0x170056b00>)」
我試圖刪除觀察者在調用viewWillDisappear時稱上面提到,但是錯誤仍然存在。
[self.bluetoothManager removeObserver:self forKeyPath:@"state"];
[self.bluetoothManager removeObserver:self forKeyPath:@"delegate"];
我試着
self.bluetoothManager.delegate = nil;
而且
self.bluetoothManager = nil;
可悲的是他們都沒有工作。
請給予幫助。
更新:
我想顯示系統彈出警報,而尚未打開藍牙打電話時ON - (無效)detectBluetooth。所以我加
[self.bluetoothManager init];
in - (void)detectBluetooth。
我發現發生錯誤。
但我找不出另一種方法來顯示默認彈出窗口(其中設置按鈕)。
建議:你可以使用一個Singleton對象爲它(有可能委託/通知),讓您的推,或簡單地用一個延遲初始化:' - (CBCentralManager )bluetoothManager {if(!self.bluuetoothManager){_ bluetoothManager = alloc/init/settings等}} return _bluetoothManager;}'在你的代碼中,總是用'self.bluetoothManager'調用你的中央管理器 – Larme
非常感謝建議! – KTang