LoginSignup
1
1

More than 3 years have passed since last update.

WatchOS2でiPhoneとの文字データのやりとりについて

Last updated at Posted at 2015-09-02

WatchOS2でiPhoneに文字を送るやり方

Obj-Cを使ってiPhoneとの連携をまとめてみました。見てくれる人いるかな。

iPhone側はこんな感じのコードです。簡単過ぎて説明いらんですね。

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

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

@end

@implementation ViewController

- (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 *moji = [applicationContext objectForKey:@"moji"];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.Label setText:[NSString stringWithFormat:@"moji:%@",moji]];
    });
}
@end
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)awakeWithContext:(id)context {
    [super awakeWithContext:context];
}

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];
}

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];
}

// 適当にWatchKit AppのInterface.storyboardにボタンを追加して下さい
- (IBAction)Pressed {
    [self sendString:@"🐱"];
}

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

@end

普通に開発したことある人ならこれみればわかるよね?的な感じで書いてます。
Swiftもちゃんと勉強しないとなー。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1