简体   繁体   English

Xcode Watchkit OpenParentApplication目标-C

[英]Xcode watchkit openParentApplication objective-c

Hey I have made an application for my phone that can unlock/lock etc my car. 嘿,我已经为我的手机申请了可以解锁/锁定汽车的应用程序。 Basically the iphone interface is just 4 buttons: lock, unlock, trunk, and connect. iphone界面基本上只有4个按钮:锁定,解锁,中继和连接。 When ever I press a button a writes something to my arduino located inside of my car. 每当我按下按钮时,都会向位于车内的arduino写一些东西。 I was wondering if I could "copy" these four buttons onto my apple watch. 我想知道是否可以将这四个按钮“复制”到苹果手表上。 What I mean by that is that can I use openParentApplication to do that or is there some other command I could use to simulate so say button presses on my iphone when the button is clicked on my apple watch. 我的意思是,我可以使用openParentApplication来执行此操作,还是可以使用其他一些命令来模拟,因此可以说,当在Apple Watch上单击按钮时,按下iPhone上的按钮即可。

Code for Iphone buttons: Iphone按钮的代码:

- (IBAction)lockCar:(UIButton *)sender;{
    NSString *string = @"l";
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    if (bleShield.activePeripheral.state == CBPeripheralStateConnected) {
        [bleShield write:data];
    }
}

- (IBAction)trunkCar:(UIButton *)sender;{
    NSString *string = @"t";
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    if (bleShield.activePeripheral.state == CBPeripheralStateConnected) {
        [bleShield write:data];
    }

}

- (IBAction)unlockCar:(UIButton *)sender;{
    NSString *string = @"u";
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    if (bleShield.activePeripheral.state == CBPeripheralStateConnected) {
        [bleShield write:data];
    }
}

Code for Apple Watch so far: 到目前为止的Apple Watch代码:

- (IBAction)carConnect {
}

- (IBAction)carUnlock {
}

- (IBAction)carLock {
}

- (IBAction)carTrunk {
}

You can use WCSession to establish communication between the WatchKit extension and the companion app. 您可以使用WCSession在WatchKit扩展程序和配套应用程序之间建立通信。 Communication channel is established as: 沟通渠道建立为:

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

The WCSessionDelegate must also be implemented to respond messages. 还必须实现WCSessionDelegate来响应消息。

The session must be activated on each side. 会话必须在每一侧都被激活。 Then you can send messages with sendMessage:replyHandler:errorHandler: and receive with session:didReceiveMessage:replyHandler: to execute those tasks. 然后,您可以使用sendMessage:replyHandler:errorHandler:发送消息,并使用session:didReceiveMessage:replyHandler:进行接收以执行这些任务。

I recommend reading the WatchKit Class reference and this article on Watchkit communication patterns . 我建议阅读WatchKit类参考有关Watchkit通信模式的这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM