WatchConnectivity を使用して、単純な辞書を iPhone から Apple Watch に送信しています。
Apple Watch 側では、アプリを開いたときにコンテキストがキューに入れられない可能性があるという事実を回避するために、最後に受信したデータが UserDefaults に保存され、WatchKit テーブルをセットアップするときにキューに何もない場合は取得されます。これを別のWatchKitアプリに実装しましたが、すべてうまくいきましたが、この1つのデータではWatchが受信することはありません.
私のアプリは時計で永遠に回転し、読み込まれないため、シミュレーターでのみ試しました (読み込み画面は WatchOs 1 画面のように見えますか?)。WatchConnectivity フレームワークは、各製品 (拡張機能および iPhone アプリ) に含まれています。ご協力いただきありがとうございます。
iPhone のコードは次のとおりです (ViewController に実装されています)。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
- (void)viewDidAppear:(BOOL)animated {
NSDictionary *toPass = [[NSDictionary alloc] initWithObjectsAndKeys:AppDelegate.profiles,@"profiles", nil];
[[WCSession defaultSession] updateApplicationContext:toPass error:nil];
NSLog(@"sent data");
[self.tableView reloadData];
}
そしてApple Watchコード:
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
// Configure interface objects here.
self.profiles = [[NSMutableArray alloc] init];
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
[self setupTable];
}
- (void)session:(WCSession *)session didReceiveApplicationContext:(NSDictionary<NSString *,id> *)applicationContext {
NSDictionary *receieved = [[NSDictionary alloc] init];
receieved = applicationContext;
NSMutableArray *profiles = [[NSMutableArray alloc] init];
profiles = [receieved objectForKey:@"profiles"];
self.profiles = [[NSMutableArray alloc] init];
self.profiles = profiles;
NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:self.profiles];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:arrayData forKey:@"bookmarks"];
[self setupTable];
NSLog(@"new");
}
- (void)setupTable {
...
After some setup code
if (self.profiles.count == 0) {
NSLog(@"Nothing in the queue, checking defaults");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *got = [defaults objectForKey:@"bookmarks"];
NSLog(@"Got Defaults!");
self.profiles = [[NSMutableArray alloc] init];
self.profiles = [NSKeyedUnarchiver unarchiveObjectWithData:got];
}
...
More setup code later
}