2016-08-01 7 views
0

사과 시계에서 정보를 부모 앱으로 보낼 수있는 앱을 만들려고합니다. 코드를 작성했지만 WatchConnectivity 앱을 실행할 때 정보가 사과 시계와 상위 iOS 앱간에 전송되지 않습니다. 내 코드에 문제가 있거나 어떤 이유로 시계가 앱으로 시작하지 않을 수 있습니다. 시뮬레이터로 가서 앱을 클릭하여 시작해야합니다. 이것이 내 코드가 작동하지 않는 이유입니까?시계 연결이 작동하지 않습니다.

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; 
} 


- (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]; 

} 

ViewController.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]]; 
    }); 
} 
+0

나는 이것을 경험했기 때문에 아이폰과 부모 간의 정보 공유를 시작하기 위해 iPhone에서 부모 앱을 먼저 열어야했다. –

답변

0

내가 먼저 아이폰간에 정보 공유를 시작하기 위해 아이폰에 부모 응용 프로그램을 열 필요가 있다고 밝혀 시계. 이 아이디어를 제안한 MSU_Bulldog에게 감사드립니다.