我通过在 iOS 应用程序中生成图像并使用后台传输将其传递给观看操作系统,通过从图像创建 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]];
}