0

時計にボタンを1つ作成し、時計をタップしながらiosアプリへの1つのプロセスを開始したいと考えています。2 つのデバイス間でデータを送信するにはどうすればよいですか

-(void)viewWillAppear:(BOOL)animated

{
 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(sayHello:) name: @"sayHelloNotification" object: nil];

}

プラス

 [[NSNotificationCenter defaultCenter] postNotificationName: @"sayHelloNotification" object: nil];

私のボタン時計では、それは動作しません

4

7 に答える 7

10

親アプリにデータを送信したい場合は、.

[WKInterfaceController openParentApplication:userInfo reply:^(NSDictionary *replyInfo, NSError *error){}];

時計アプリでこのメソッドを呼び出すと、AppDelegate でコールバックが発生します

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply;

送信された userInfo ディクショナリでデータを定義する必要があります。

于 2014-12-17T16:07:35.213 に答える
3

私の知る限り、それらの間でいくつかのデータを送信するなど、データを直接共有することはできません。
できることは、同じファイルにデータを書き込むことです。

このブログ投稿を参照してください:
http://www.atomicbird.com/blog/sharing-with-app-extensions

于 2014-11-22T11:38:01.250 に答える
1

残念ながら、NSNotificationCenter はアプリ間で機能しません。MMWormholeを使用して、WatchKit と iOS の間でメッセージを渡します。

于 2015-03-16T10:54:49.330 に答える
0

このようなデータを送信できます。

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
    NSLog(@"Username %@",[userInfo objectForKey:@"username"]);
    NSLog(@"Password %@",[userInfo objectForKey:@"password"]);
}


- (IBAction)passUserInfo:(id)sender
{
    NSDictionary *userInfo = @{@"username":@"hi",@"password":@"123456"};

    [WKInterfaceController openParentApplication:userInfo reply:^(NSDictionary *replyInfo, NSError *error){

    }];// userinfo must be non-nil
}
于 2015-04-08T14:19:59.837 に答える
0

データが冗長な場合はupdateApplicationContext ()を使用して Watch から iPhone にデータを送信することをお勧めします &&データを頻繁に更新するには:-

iPhoneでデータを受信

func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
        let type = applicationContext["watchType"]!
         DispatchQueue.main.async {
            self.updateLabel.text =  " Type: \(type)"
            UserDefaults.standard.set(type, forKey: "savedState") //setObject
        }
    }

監視してデータを送信する

func updateContext(value:String) {
    let dictionary = [ "watchType" : value ]
    do {
        try session?.updateApplicationContext(dictionary)
    }
    catch{
    }
}

デモアプリ

代替sendMessage () を使用できます

iPhoneでデータ送信

 @IBAction func sendTextToWatch(_ sender: Any) {
            print("send text to watch amount")
             if let textName = textWord.text {
                session?.sendMessage(["textIndex" : textName as String], replyHandler: nil, errorHandler: nil)
            }

監視してデータを受信する

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
       let message:String = message["textIndex"] as! String
       textLabel.setText(message)
       print(message)
   }
  }

デモアプリ2

非推奨 の openParentApplication:reply: ウォッチ OS-2 ではサポートされていません

于 2016-11-29T07:42:18.403 に答える
0

watchOS 2.0 以降では、2 つのデバイス間でメッセージを送信するだけで済みます。いつでもメッセージWatch->iPhoneを送信でき (iPhone の相手が実行されていない場合はイベント) 、時計の相手が表示されている場合はiPhone->Watchを送信できます。[WCSession defaultSession].isReachableメッセージを送信できることを確認してください。

両方のプラットフォームのコード例を次に示します。

@import WatchConnectivity;

...

if ([WCSession defaultSession].isReachable) {
     [[WCSession defaultSession] sendMessage:@{
                                   @"Key" : @"Value"
                              } replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
                                   NSLog(@"Sent update is OK");
                              } errorHandler:^(NSError * _Nonnull error) {
                                   NSLog(@"Sent update with error %@", error);
                              }];
}

このメッセージに反応するには、相手に実装する必要がありますWCSessionDelegate:

- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message;

また

- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message replyHandler:(void(^)(NSDictionary<NSString *, id> *replyMessage))replyHandler;
于 2016-04-19T07:52:02.770 に答える