在我的应用程序中,我必须将信息从手表 InterfaceController 发送到手机 HomeViewController。但是,当我运行我的代码时,这些信息只工作一次。为了让它再次工作,我必须删除 Apple Watch 应用程序并重新安装它。
接口控制器.m:
#import "InterfaceController.h"
#import <WatchConnectivity/WatchConnectivity.h>
@interface InterfaceController() <WCSessionDelegate>
@property (strong, nonatomic) WCSession *session;
@end
@implementation InterfaceController
-(instancetype)init {
self = [super init];
if (self) {
if ([WCSession isSupported]) {
self.session = [WCSession defaultSession];
self.session.delegate = self;
[self.session activateSession];
}
}
return self;
}
-(void)sendText:(NSString *)text {
NSDictionary *applicationDict = @{@"text":text};
[self.session updateApplicationContext:applicationDict error:nil];
}
- (IBAction)ButtonPressed {
[self sendText:@"Hello World"];
}
HomeViewController.m:
#import "HomeViewController.h"
#import <WatchConnectivity/WatchConnectivity.h>
@interface HomeViewController ()<WCSessionDelegate>
@end
@implementation HomeViewController
@synthesize TextLabel;
- (void)viewDidLoad {
[super viewDidLoad];
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
- (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
NSString *text = [applicationContext objectForKey:@"text"];
dispatch_async(dispatch_get_main_queue(), ^{
[TextLabel setText:text];
});
}
如前所述,iOS 标签只更改为“Hello World”一次。在我重新启动 iOS 应用程序后,它的文本标签不再显示“Hello World”,我无法让手表再次将 iOS 文本标签更改回“Hello World”。
这是手表和iPhone通讯的问题,还是代码的问题?