0

我正在尝试创建一个应用程序,我可以在其中将信息从苹果手表发送到我的 ios 父应用程序。我已经为它编写了代码,但是当我运行 WatchConnectivity 应用程序时,信息不会在 Apple Watch 和父 ios 应用程序之间传输。这可能是我的代码有问题,也可能是由于某种原因手表没有从应用程序启动。我必须去模拟器并单击应用程序才能启动它。这就是我的代码不起作用的原因吗?

接口控制器.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;
}


- (IBAction)catPressed {
     [self sendText:@"cat"];
}
- (IBAction)dogPressed {
     [self sendText:@"dog"];
}
- (IBAction)pandaPressed {
    [self sendText:@"panda"];
}
- (IBAction)bunnyPressed {
    [self sendText:@"bunny"];
 }

-(void)sendText:(NSString *)text {
     NSDictionary *applicationDict = @{@"emoji":text};
     [self.session updateApplicationContext:applicationDict error:nil];

}

视图控制器.m

#import "ViewController.h"
#import <WatchConnectivity/WatchConnectivity.h>

@interface ViewController () <WCSessionDelegate>
@property (weak, nonatomic) IBOutlet UILabel *textLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
     [super viewDidLoad];

    if ([WCSession isSupported]) {
         WCSession *session = [WCSession defaultSession];
         session.delegate = self;
         [session activateSession];

         NSLog(@"HIIII");
     }
 }

 - (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {

    NSString *text = [applicationContext objectForKey:@"text"];

    dispatch_async(dispatch_get_main_queue(), ^{
    [self.textLabel setText:[NSString stringWithFormat:@"Text: %@", text]];
    });
}
4

1 回答 1

0

事实证明,我需要先在 iPhone 上打开父应用程序才能开始在 iPhone 和 Watch 之间共享信息。感谢 MSU_Bulldog 提出这个想法。

于 2016-08-05T17:19:19.183 回答