2016-08-01 12 views
0

Appleの時計からiOSの親アプリケーションに情報を送信できるアプリケーションを作成しようとしています。私はそれのためのコードを書いたが、私は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とWatchの間で情報の共有を開始するには、最初にiPhoneで親アプリを開く必要がありました –

答えて

0

それは私が最初にiPhoneの間で情報の共有を開始するためにiPhone上の親アプリを開くために必要なことが判明と時計。このアイディアを提案してくれたMSU_Bulldogに感謝します。

関連する問題