iOSアプリで画像を生成し、バックグラウンド転送を使用してOSを監視するために渡し、画像からNSDataを作成することで、次のように理解しました
- (void)viewDidLoad {
[super viewDidLoad];
if([WCSession isSupported]){
self.watchSession = [WCSession defaultSession];
self.watchSession.delegate = self;
[self.watchSession activateSession];
}
}
-(void)sendDatatoAppleWatch
{
NSMutableArray*barCodesArray=[[NSMutableArray alloc]init];
UIImage* barCodeImage=[self generateBarCode];
NSData *pngData = UIImagePNGRepresentation(barCodeImage);
[barCodeArray addObject:pngData]
if(self.watchSession){
NSError *error = nil;
if(![self.watchSession
updateApplicationContext:
@{@"cardData" : userCardsArray }
error:&error]){
NSLog(@"Updating the context failed: %@", error.localizedDescription);
UIAlertView* errorAlert=[[UIAlertView alloc]initWithTitle:error.localizedDescription message:error.debugDescription delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[errorAlert show];
}
}
//*** Apple Watch Code**//
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
if([WCSession isSupported]){
self.watchSession = [WCSession defaultSession];
self.watchSession.delegate = self;
[self.watchSession activateSession];
}
}
- (void) session:(WCSession *)session didReceiveApplicationContext:(NSDictionary<NSString *,id> *)applicationContext {
NSData* imageData = [[[applicationContext objectForKey:@"cardData"] objectAtIndex:0] valueForKey:@"barCodeImage"];
[self.barcodeImageView setImage:[UIImage imageWithData:imageData]];
}