gpt4 book ai didi

ios - CoreBluetooth 状态保存问题

转载 作者:行者123 更新时间:2023-12-01 16:33:29 31 4
gpt4 key购买 nike

The app is a simple heart rate monitor from this link现在我只是在玩核心蓝牙,我正在尝试“在后台执行长期操作”,其中涉及 Adding Support for State Preservation and Restoration

要做的第一件事是更改 info.plist 文件,然后我通过替换“选择加入状态保存和恢复”

CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

在我的 MainViewController.m 中
CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionRestoreIdentifierKey:@"myCentralManager"}];

第二步涉及“在系统重新启动您的应用程序后重新实例化任何中央或外围管理器对象”。

我通过首先将此属性添加到 MainViewController.m 中来做到这一点
@property (weak,nonatomic) id <CBCentralManagerDelegate> delegate;

然后在 appDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

self.centralManager = [[CBCentralManager alloc] initWithDelegate:MainVC.delegate queue:nil options:@{CBCentralManagerOptionRestoreIdentifierKey:@“myCentralManager”}];
return YES;
}

下一步是“实现适当的恢复委托(delegate)方法”
-(void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)dict {
NSLog(@"willRestoreState called");
/// self.myPeripheral = [dict[CBCentralManagerRestoredStatePeripheralsKey]
///firstItem];
/// self.myPeripheral.delegate = self;
}

我收到错误
*** Assertion failure in -[CBCentralManager initWithDelegate:queue:options:],               /SourceCache/CoreBluetooth/CoreBluetooth-242.1/CBCentralManager.m:194
2015-05-22 00:42:16.049 HeartMonitor[9694:793663] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<CBCentralManager: 0x17009e050> has provided a restore identifier but the delegate doesn't implement the centralManager:willRestoreState: method'

谢谢

最佳答案

您需要实现委托(delegate)方法,
例如,如果我们在 AppDelegate 中实现它,它将是这样的。

@interface AppDelegate () <CBCentralManagerDelegate>

@property (strong, nonatomic) CBCentralManager *centralManager;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self
queue:nil
options:@{CBCentralManagerOptionRestoreIdentifierKey:@“myCentralManager”}];
return YES;
}

- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)dict
{
NSLog(@"willRestoreState called");
}

@end

关于ios - CoreBluetooth 状态保存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30377261/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com