2

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];
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
bilioo
  • 53
  • 7

1 Answers1

0

Activating the WCSession in a view controller on the iOS side is not a good choice as if the app is launched in the background view controllers will never get loaded. You should activate it in application:didFinishLaunchingWithOptions: method of your app delegate.

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Any other code you might have

    if ([WCSession isSupported]) {
        self.session = [WCSession defaultSession];
        self.session.delegate = self;
        [self.session activateSession];
    }
}

Here I've assumed you also moved your WCSessionDelegate method to you app delegate.

Then when you need to send any type of data to the watch you can retrive the default session and call methods like updateApplicationContext:error:

WCSession* session = [WCSession defaultSession];
[session updateApplicationContext:applicationDict error:nil];
Marco Boschi
  • 2,321
  • 1
  • 16
  • 30
  • Thanks but How add [self.session updateApplicationContext:applicationDict error:nil]; to .M from iPhone i need to call self.session = [WCSession defaultSession]; self.session1.delegate = self; [self.session1 activateSession]; – bilioo Aug 27 '15 at 18:30
  • You have to move the code you have in `viewDidLoad` inside the .m file of your view controller to the method `application:didFinishLaunchingWithOptions:` inside `AppDelegate.m`, this method should be added by Xcode when you created the project, if it's not there add it. [UIApplicationDelegate reference](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:didFinishLaunchingWithOptions:) – Marco Boschi Aug 27 '15 at 18:51
  • i good added in application:didFinishLaunchingWithOptions but for view controller .M i need to call session again and it's dosent work if is possible please give me on example or writing the correct codes Thanks – bilioo Aug 27 '15 at 19:03
  • i need to call in My View Controller there for i need call session in viewController to after app delegate but it's dosent work [self.session updateApplicationContext:applicationDict error:nil]; – bilioo Aug 27 '15 at 19:05
  • when i call session in application:didFinishLaunchingWithOptions: before return after all data can't send to Viewcontroller between watch and view controller – bilioo Aug 27 '15 at 20:08
  • the problem is WCSession in Appdelegate – bilioo Aug 27 '15 at 20:10
  • Have you declared a property named `session` in your app delegate? If not that should be your problem, if don't want to you can also use a local variable `WCSession* session = [WCSession defaultSession]; ...` – Marco Boschi Aug 27 '15 at 20:15
  • i adding import WatchConnectivity , WCSessionDelegate , property (strong, nonatomic) WCSession *session , synthesize session; but dosent work – bilioo Aug 27 '15 at 20:21
  • It doesn't work or it doesn't compile? In the second case have you made your app delegate implements WatchConnectivityDelegate? In the second case make sure your app delegate has appropriate method to receive data from the watch. – Marco Boschi Aug 27 '15 at 20:24
  • is that need to add Appdelegate.h to View Controller ? – bilioo Aug 27 '15 at 20:32
  • In the setup I proposed there is no need for it. What errors Xcode gives you? – Marco Boschi Aug 27 '15 at 20:34
  • i don't give error just i can't call to - (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary *)applicationContext { in watch part – bilioo Aug 27 '15 at 20:36
  • if i added in viewDidLoad dosent problem – bilioo Aug 27 '15 at 20:37
  • Please update your code to show how you activate your session, send data to the watch and receive it after you changed it. – Marco Boschi Aug 27 '15 at 20:40
  • becaground and when open iPhone app dosent call - (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary *)applicationContext { in watch part – bilioo Aug 27 '15 at 20:44
  • I understand that, can you please show the whole app delegate .m file and the lines you use to send the application context? – Marco Boschi Aug 27 '15 at 20:46
  • Have you checked that `senddata` is called after you activate the session? If not, activate the session before you instantiate the view controller in app delegate – Marco Boschi Aug 28 '15 at 06:46
  • If you still have troubles understanding WCSession I suggest to watch session 713 from this year WWDC. They explain where to activate the session and how to send data, they use Swift code but it should be easy to follow. https://developer.apple.com/videos/wwdc/2015/?id=713 – Marco Boschi Aug 28 '15 at 13:15
  • Hello is that work with this system ? and how to add in senddata http://stackoverflow.com/questions/31496584/close-deactivate-wcsession – bilioo Aug 28 '15 at 18:37
  • You can use the same `senddata:` function in other view controller changing dictionary keys as appropriate. But I will suggest to leave the activation of the default session in app delegate and then retrieve the default session when you need as you already do in `senddata:`. Also pay attention to the fact that `sendApplicationContext` will override already sent context if it hasn't been already transferred to the watch as clearly stated in the video in linked before. – Marco Boschi Aug 28 '15 at 18:49