簡體   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