I have this code it works when I open the app in iPhone simulator
and call this in ViewDidLoad
. But if I call this in app delegate I cant call:
- (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext
in watch Part
if ([WCSession isSupported]) {
self.session = [WCSession defaultSession];
self.session.delegate = self;
[self.session activateSession];
return YES;
}
but I want it to work the app in background. How can I do this?
It's .delegate from iPhone
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *mainViewController = [storyboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];
if ([WCSession isSupported]) {
self.session = [WCSession defaultSession];
self.session.delegate = self;
[self.session activateSession];
}
return YES;
}
in ViewController
-(void)senddata:(NSString *)data {
WCSession* session = [WCSession defaultSession];
NSDictionary *applicationDict = ///
[session updateApplicationContext:applicationDict error:nil];
}
It's .m from Extension
- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
[super willActivate];
if ([WCSession isSupported]) {
self.session = [WCSession defaultSession];
self.session.delegate = self;
[self.session activateSession];
}
[self.session updateApplicationContext:applicationDict error:nil];