【发布时间】:2016-08-05 03:41:26
【问题描述】:
在我的应用程序中,我必须将信息从手表 InterfaceController 发送到手机 HomeViewController。但是,当我运行我的代码时,这些信息只工作一次。要让它再次工作,我必须删除 Apple Watch 应用并重新安装。
InterfaceController.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 通信的问题,还是代码的问题?
【问题讨论】:
标签: ios objective-c watchos watchconnectivity wcsession