繁体   English   中英

Xcode Watchkit OpenParentApplication目标-C

[英]Xcode watchkit openParentApplication objective-c

嘿,我已经为我的手机申请了可以解锁/锁定汽车的应用程序。 iphone界面基本上只有4个按钮:锁定,解锁,中继和连接。 每当我按下按钮时,都会向位于车内的arduino写一些东西。 我想知道是否可以将这四个按钮“复制”到苹果手表上。 我的意思是,我可以使用openParentApplication来执行此操作,还是可以使用其他一些命令来模拟,因此可以说,当在Apple Watch上单击按钮时,按下iPhone上的按钮即可。

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

到目前为止的Apple Watch代码:

- (IBAction)carConnect {
}

- (IBAction)carUnlock {
}

- (IBAction)carLock {
}

- (IBAction)carTrunk {
}

您可以使用WCSession在WatchKit扩展程序和配套应用程序之间建立通信。 沟通渠道建立为:

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

还必须实现WCSessionDelegate来响应消息。

会话必须在每一侧都被激活。 然后,您可以使用sendMessage:replyHandler:errorHandler:发送消息,并使用session:didReceiveMessage:replyHandler:进行接收以执行这些任务。

我建议阅读WatchKit类参考有关Watchkit通信模式的这篇文章

暂无
暂无

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

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