gpt4 book ai didi

ios - didReceiveApplicationContext 未被调用

转载 作者:可可西里 更新时间:2023-11-01 06:18:25 24 4
gpt4 key购买 nike

我刚刚使用教程/示例代码在我的应用程序中设置了 WCConnectivity,我觉得我已经正确实现了它。我没有使用模拟器进行测试。出于某种原因,即使一切设置正确,didReceiveApplicationContext 也没有在 watch 应用程序中被调用。

我尝试在 WatchKit 应用程序的接口(interface) Controller 中调用 ExtensionDelegate 并使用 NSUserDefaults 来设置接口(interface) Controller 数据。

iOS 应用

ViewController.m

- (void) viewDidLoad{
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}

-(void) saveTrip{
NSMutableArray *currentTrips = [NSMutableArray arrayWithArray:[self.sharedDefaults objectForKey:@"UserLocations"]];

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:newLocation];
[currentTrips addObject:data];
[self.sharedDefaults setObject:currentTrips forKey:@"UserLocations"];
[self.sharedDefaults synchronize];

WCSession *session = [WCSession defaultSession];
NSDictionary *applicationDict = [[NSDictionary alloc] initWithObjects:@[currentTrips] forKeys:@[@"UserLocations"]];;
[session updateApplicationContext:applicationDict error:nil];
}

观看扩展代码

ExtensionDelegate.m

- (void)applicationDidFinishLaunching {
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
- (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
self.places= [applicationContext objectForKey:@"UserLocations"];
[[NSUserDefaults standardUserDefaults] setObject:self.places forKey:@"UserLocations"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

InterfaceController.m

- (void)willActivate {
[super willActivate];
self.placesData = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"UserLocations"]];
[self loadData];
}

最佳答案

对我来说有两件事:

  1. 在字典中传递无效值。甚至不能通过 NSNull() 来表示 nil 值。如果其中有无法在 plist 中表示的数据,则它会失败。

  2. 如果字典没有改变,对 updateApplicationContext 的后续调用将不会触发对 didReceiveApplicationContext 的相应调用。要强制更新(可能在调试版本中),您可以将 UUID 添加到有效负载中,例如

    context["force_send"] = UUID().uuidString

关于ios - didReceiveApplicationContext 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35854473/

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