Ios 与WatchConnectivity相关的协助-目标-c
我一直试图将WatchKit目标添加到用Objc编写的现有应用程序中,但完全无法使消息传递正常工作。当我运行扩展、启动会话并检查可访问性时,一切正常,我显示容器应用程序可以从扩展访问。但是,当我发送消息时,手机应用程序不会唤醒。从我看到的所有文档和示例来看,手机应用程序应该在后台运行,但是,这并没有发生。我错过什么了吗?任何帮助都将不胜感激 附件为相关代码: 从分机的InterfaceController.m,我呼叫:Ios 与WatchConnectivity相关的协助-目标-c,ios,objective-c,iphone,watchkit,Ios,Objective C,Iphone,Watchkit,我一直试图将WatchKit目标添加到用Objc编写的现有应用程序中,但完全无法使消息传递正常工作。当我运行扩展、启动会话并检查可访问性时,一切正常,我显示容器应用程序可以从扩展访问。但是,当我发送消息时,手机应用程序不会唤醒。从我看到的所有文档和示例来看,手机应用程序应该在后台运行,但是,这并没有发生。我错过什么了吗?任何帮助都将不胜感激 附件为相关代码: 从分机的InterfaceController.m,我呼叫: [[WCSession defaultSession] sendMessag
[[WCSession defaultSession] sendMessage:@{@"message":@"begin"} replyHandler:nil errorHandler:nil];
extensionelegate.h
IOS AppDelegate.h
#导入
#导入“objShoppingList.h”
#导入“databaseController.h”
#进口
@接口AppDelegate:UIResponder
@结束
IOS AppDelegate.m
#导入“AppDelegate.h”
@进口货位;
@接口AppDelegate(){
CLLocationManager*locationManager;
WCSession*届会;
}
@结束
@实现AppDelegate
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
//应用程序启动后自定义的覆盖点。
如果([WCSession isSupported]){
[WCSession defaultSession]。委托=self;
会话=[WCSession defaultSession];
session.delegate=self;
NSLog(@“电话上的会话启动”);
[[WCSession defaultSession]activateSession];
}
NSLog(@“iphone应用程序启动”);
. . .
返回YES;
}
-(void)session:(非null WCSession*)session didReceiveMessage:(非null NSDictionary*)message replyHandler:(非null void(^)(NSDictionary*\uu nonnull))replyHandler{
//在这种情况下,从应用程序发送的消息内容是一条简单的开始消息。这告诉应用程序醒来并开始向手表发送位置信息。
NSLog(@“已访问IOS应用程序”);
}
-(void)会话:(WCSession*)会话didReceiveMessage:(NSDictionary*)消息{
NSLog(@“已访问IOS应用程序”);
}
@结束
每次测试完成后,IOS应用程序都不会启动,我也不会收到来自didfishlaunchingwithoptions的NSLog消息
非常感谢您的帮助。您确定应用程序不会启动吗?主应用程序的日志消息不会显示在您调试watch应用程序的控制台中。谢谢。应用程序正在启动(通过删除它,然后重新运行,并看到位置服务的提示,发现了这一点)。您可能会发现使用类似的内容来帮助跟踪后台应用程序的功能非常有用。我相信,当您调用
sendMethod
,ios应用程序和watch应用程序都需要位于前台,以便对方接收消息。在调用sendMessage的InterfaceController.m代码中,尝试添加错误处理程序并打印错误。是否返回任何错误?
#import <WatchKit/WatchKit.h>
#import <WatchConnectivity/WatchConnectivity.h>
@interface ExtensionDelegate : NSObject <WKExtensionDelegate,WCSessionDelegate>
@end
- (void)applicationDidFinishLaunching {
// Perform any final initialization of your application.
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
if (session.isReachable) {
NSLog(@"Able to reach app.");
}
}
}
#import <UIKit/UIKit.h>
#import "objShoppingList.h"
#import "databaseController.h"
#import <WatchConnectivity/WatchConnectivity.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end
#import "AppDelegate.h"
@import CoreLocation;
@interface AppDelegate ()<CLLocationManagerDelegate, WCSessionDelegate>{
CLLocationManager *locationManager;
WCSession *session;
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if ([WCSession isSupported]) {
[WCSession defaultSession].delegate = self;
session = [WCSession defaultSession];
session.delegate = self;
NSLog(@"Sessiopn on phone starting");
[[WCSession defaultSession] activateSession];
}
NSLog(@"iphone app starting");
. . .
return YES;
}
- (void)session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary<NSString *,id> *)message replyHandler:(nonnull void (^)(NSDictionary<NSString *,id> * __nonnull))replyHandler {
// In this case, the message content being sent from the app is a simple begin message. This tells the app to wake up and begin sending location information to the watch.
NSLog(@"Reached IOS APP");
}
-(void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message{
NSLog(@"Reached IOS APP");
}