问题一、WatchConnectivity
Error Domain=WCErrorDomain Code=7014 "Payload could not be delivered." UserInfo={NSLocalizedDescription=Payload could not be delivered
原因: 对方收到数据要返回信息给发送者,就是用接收数据时,用第二种方法,不要用第一种方法。否则会报错,what's hell ! 参考网址。
解决办法是:
//收到数据不要用此方法!!
-(void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message{
}
//收到数据,要用此方法
-(void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message replyHandler:(void (^)(NSDictionary<NSString *,id> * _Nonnull))replyHandler{
if (message) {
NSDictionary *dict = @{@"hi":@"hello"};
replyHandler(dict);
[_infoLb setText:message[@"value"]];
}
}
如何实现Watch与iPhone间通讯
a. "InterfaceController.h"中
#import "InterfaceController.h"
#import <WatchConnectivity/WatchConnectivity.h>
@interface InterfaceController()<WCSessionDelegate>
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *infoLb;
@end
@implementation InterfaceController
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
if ([WCSession isSupported])
{
WCSession* session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
- (IBAction)clickBtn
{
NSDictionary *dict = @{@"value":@"iwatch发送"} ;
[self sendDataToPhone:dict];
}
#pragma mark - 发送信息
-(void)sendDataToPhone:(NSDictionary* _Nonnull)dictData
{
if(WCSession.isSupported){
WCSession* session = WCSession.defaultSession;
if(session.reachable)
{
[session sendMessage:dictData replyHandler: ^(NSDictionary<NSString *,id> * __nonnull replyMessage) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@".....replyHandler called --- %@",replyMessage);
});
}
errorHandler:^(NSError * __nonnull error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Error = %@",error.localizedDescription);
});
}
];
}
else
NSLog(@"Session Not reachable");
}
else
NSLog(@"Session Not Supported");
}
#pragma mark - 代理方法
#pragma mark - WCSessionDelegate
-(void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(NSError *)error{
}
-(void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message{
}
-(void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message replyHandler:(void (^)(NSDictionary<NSString *,id> * _Nonnull))replyHandler{
if (message) {
NSDictionary *dict = @{@"hi":@"hello"};
replyHandler(dict);
[_infoLb setText:message[@"value"]];
}
}
//当session改变时,使用激活会话
-(void)sessionWatchStateDidChange:(nonnull WCSession *)session
{
if(WCSession.isSupported){
WCSession* session = WCSession.defaultSession;
session.delegate = self;
[session activateSession];
}
}
@end
b. UIViewController 中
#import "ViewController.h"
#import <WatchConnectivity/WatchConnectivity.h>
@interface ViewController ()<WCSessionDelegate>
@property (weak, nonatomic) IBOutlet UILabel *recevieBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//判断当前系统是否是ios9.0系统.如果低于ios9.0就崩溃
if ([WCSession isSupported])
{
WCSession* session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 点击事件
- (IBAction)clickBtn:(id)sender
{
// [WCSession onqueue_handleDictionaryMessageRequest:withPairingID:nil];
WCSession *session = [WCSession defaultSession];
if([session isReachable]){
[session sendMessage:@{@"value":@"iphone发送"} replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage)
{
NSLog(@"%@",replyMessage);
// NSDictionary *dict = @{@"key":@"value"};
// replyMessage(dict);
} errorHandler:^(NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
}
#pragma mark - 代理方法
#pragma mark - WCSessionDelegate
-(void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message replyHandler:(void (^)(NSDictionary<NSString *,id> * _Nonnull))replyHandler{
if (message) {
NSDictionary *dict = @{@"key":@"value"};
replyHandler(dict);
[_recevieBtn setText:message[@"value"]];
}
}
/** Called when the session has completed activation. If session state is WCSessionActivationStateNotActivated there will be an error with more details. */
- (void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(nullable NSError *)error
{
}
/** ------------------------- iOS App State For Watch ------------------------ */
/** Called when the session can no longer be used to modify or add any new transfers and, all interactive messages will be cancelled, but delegate callbacks for background transfers can still occur. This will happen when the selected watch is being changed. */
- (void)sessionDidBecomeInactive:(WCSession *)session {
}
/** Called when all delegate callbacks for the previously selected watch has occurred. The session can be re-activated for the now selected watch using activateSession. */
- (void)sessionDidDeactivate:(WCSession *)session{
}
-(void)sessionWatchStateDidChange:(nonnull WCSession *)session
{
if(WCSession.isSupported){
WCSession* session = WCSession.defaultSession;
session.delegate = self;
[session activateSession];
if(session.reachable){
NSLog(@"session.reachable");
}
if(session.paired){
if(session.isWatchAppInstalled){
if(session.watchDirectoryURL != nil){}}}}
}
@end
网友评论