最近在进行applewatch的开发,打算做一个微信的demo出来。
由于数据处理和业务逻辑都会放在app端,watchkit只是用来展示用途,在app处于非激活的状态下无法进行请求的问题还未解决,这个demo只是watch和app简单的相互传值通信。其中app端中CFNotificationCenterPostNotification 方法中的userInfo参数无法成功传值到watch端中,还在尝试用其他的方法实现,解决到这个问题的朋友麻烦留言探讨一下实现方案。
从app端传值到watch端:
发送端:
CFStringRef observedObject = CFSTR("xiaok"); NSMutableDictionary *userInfo2 = [NSMutableDictionary new]; [userInfo2 setValue:@"PicName" forKey:@"PicName"]; [userInfo2 setValue:@"Source" forKey:@"Source"]; CFDictionaryRef userInfo = (__bridge CFDictionaryRef)(userInfo2); CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),(__bridge CFStringRef)@"xiaok",observedObject,userInfo,YES );
接受端:
- (void) watchForDataChanges { // Listen for notifications on Darwing CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), NotificationReceivedCallback, CFSTR("xiaok"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cocoaNotificationCallBack) name:@"playersChangedCocoa" object:nil]; } static void NotificationReceivedCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { [[NSNotificationCenter defaultCenter] postNotificationName:@"playersChangedCocoa" object:nil]; } void darwinNotificationCenterCallBack() { NSLog(@"Notification received from iPhone app!"); // Go from Darwin to Cocoa land [[NSNotificationCenter defaultCenter] postNotificationName:@"playersChangedCocoa" object:nil]; } - (void) cocoaNotificationCallBack { NSLog(@"Notification received from ourselves in Cocoa"); [self.displayLB setText:@"app点击了噢"]; }
从watch传值到app端:
发送端:
- (IBAction)btnClick1 { [WKInterfaceController openParentApplication:@{@"tag":@"1"} reply:^(NSDictionary *replyInfo, NSError *error) { }]; }
接受端:在appdelegate.m
-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply { NSString *type = userInfo[@"tag"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"WATCH_CLICK" object:type]; }
demo github:https://github.com/lkjxshi/WatchToAppDemo.git
相关推荐
Apple Watch要装的12个应用 Apple Watch必备App.pdfApple Watch要装的12个应用 Apple Watch必备App.pdf
Apple Watch App Development introduces you to the architecture and possibilities of the Apple Watch platform, as well as an in-depth look at how to work with Xcode playgrounds. Benefit from a rapid ...
Build real-world applications for the Apple Watch platform using the WatchKit framework and Swift 2.0 About This Book Find out how to download and install the Xcode development tools before learning ...
You've got a great idea for an Apple Watch app. But how do you get your app from idea to wrist? This book shows you how to make native watchOS apps for Apple's most personal device yet. You'll learn ...
WatchKit应用程序和WatchKit应用程序开发的简介。 本书面向具有使用Xcode和Swift编程语言进行iOS开发的一些现有经验的读者。
Advice & Techniques from 5 Top Professionals gives you the base-knowledge and valuable secrets you'll need for your own projects from a core team of successful, experienced Apple Watch app-...
用app inventor制作的一个wifi通信app,可实现此app连接上已开启热点的wifi模块,进而控制32开发板
android studio 使用Socket制作app在手机间通信,同一个网段下使用两台手机实现通信。
接下来就以我之前写的UIActivityViewController系统原生分享-仿简书分享和iOS开源小项目-WSL两个Demo为例,让我们看下怎么可以让UIActivityViewController系统原生分享-仿简书分享唤起iOS开源小项目-WSL并进行通信、...
主要为大家详细介绍了Android使用AIDL实现两个App间通信,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
PC 通过USB 与android APP 通信
Apple Watch开发快速入门教程-OC版
app如何与服务端进行通信?用什么格式?xml?json?或者其他?在更新的时候又如何更省流量呢?
特点5分钟粒度与所有手表型号(包括Series 4)兼容趋势指示器几乎:1或TimeBoard-冗长的Apple Watch Face此应用程序/表盘通过文字显示时间。 特点5分钟粒度与所有手表型号(包括4系列)兼容趋势指示器差不多:在指示...
喵呜手机端APP通信协议
Appke watch 真机调试包 5.1 & 5.2 (16T5181e) From Xcode_10.2_Beta
Advice & Techniques from 5 Top Professionals gives you the base-knowledge and valuable secrets you'll need for your own projects from a core team of successful, experienced Apple Watch app-...
android app通过usb连接电脑,用socket建立连接。 适用无网环境(无wifi),通过usb来组建局域网。一般银行单位或是需要保密的电脑用得上
Cordova 插件,可通过 MMWormhole 由mutualmobile 从javascript 与Apple Watch(watchkit)进行通信 介绍 这个插件是一个MMWormhole的包装器,它由mutualmobile 通过应用程序组直接从 JavaScript 与 Apple Watch ...