I am using WCSession to pass data between my iWatch and iPhone app. I need a way to see if the phone has already sent data when the watch opens up.
I have tried didReceiveApplicationContext, didReceiveUserInfo and didReceiveMessage but they only get called if there is data. Is a timer the only way to go?
WCSession *session;
if ([WCSession isSupported]) {
session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
What I ended up doing was sending dummy data when I open the watch app and checking the didFinishUserInfoTransfer.
Related
I would like to have my watch app respond to the parent app on the phone being killed. When the watch app is running and the phone app is killed I get no callback from either sessionReachabilityDidChange or sessionWatchStateDidChange. Based on apple documentation:
This method is called to let the current process know that its
counterpart session’s reachability changed.
So, it seems that I should get a callback. I've set the WCSession delegate to my class on the watch. The session on the watch receives callbacks for application context. Why am i not getting a reachability callback?
Code Below..
+ (SomeClass *)sharedInstance {
static dispatch_once_t pred;
static SomeClass *shared = nil;
dispatch_once(&pred, ^{
shared = [[SomeClass alloc] init];
[Model sharedInstance].delegate = shared;
});
return shared;
}
#pragma mark - setup
- (void)initializeSession {
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
[self sync];
}
}
-(BOOL) hasValidWCSession {
return ([WCSession isSupported] && [WCSession defaultSession].isReachable);
}
#pragma mark - WCSessionDelegate
- (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *, id> *)applicationContext {
NSLog(#"application context received on watch: %#", applicationContext);
[[Model sharedInstance] process:applicationContext];
}
- (void)sessionWatchStateDidChange:(WCSession *)session {
NSLog(#"wcession state changed on watch");
}
- (void)sessionReachabilityDidChange:(WCSession *)session {
NSLog(#"wcsession reachability changed on watch");
}
The iPhone app counterpart does not need to be running in order for reachable to be true from the perspective of the Apple Watch app. If the iPhone itself is paired and reachable, sending messages using WCSession from the watch app will launch the iPhone app in the background.
Thus, I would not expect sessionReachabilityDidChange: to be called on the watch app when the iOS app is killed. This also means that your iPhone app should be prepared to be launched in the background at any time, and activate WCSession promptly, to handle incoming requests from the watch app (similar to how certain push notifications, etc., can launch an iOS app).
However, from the perspective of the iPhone app, the watch app counterpart is only considered reachable when "a paired and active Apple Watch is in range and the associated Watch app is running in the foreground" (documentation).
Also, note that sessionWatchStateDidChange: is invoked when "the value in the paired, watchAppInstalled, complicationEnabled, or watchDirectoryURL properties of the WCSession object changes".
I tried to send message to iPhone app from iwatchExtension(on ButtonClick) using WCSession method like
[[WCSession defaultSession] sendMessage:applicationData
replyHandler:^(NSDictionary *reply)
But delegate method
- (void)session:(WCSession *)session didReceiveMessage: is not getting called.
when I tried same while passing data from iPhone to iWatch, it works properly. All delegate methods get called properly in "Extension Delegate" class.
If you check the definition of WCSession delegate methods you will find a minor difference about their receiving nature. Check the bold quote.
/** Called on the delegate of the receiver. Will be called on startup
if the incoming message caused the receiver to launch. */
- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary *)message;
/** Called on the delegate of the receiver when the sender sends a
message that expects a reply. Will be called on startup if the
incoming message caused the receiver to launch. */
- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary *)message
replyHandler:(void(^)(NSDictionary
*replyMessage))replyHandler;
As you are providing a non-null parameter for replyHandler:, you should be receiving the message in other delegate i.e. -(void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message.
You are using [WCSession defaultSession] sendMessage: ...] for communication, which lacks the delegate assignment plus you have not activated the session.
In your Extention class, create an ivar for WCSession and write this code
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
// Configure interface objects here.
if([WCSession isSupported])
{
session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
and then send message like [session sendMessage:applicationData replyHandler:^(NSDictionary *reply).
In your AppDelegate.m file just create and instance of WCSession and activate it and use the correct delegate method to capture the message.
if([WCSession isSupported]) {
appSession = [WCSession defaultSession];
appSession.delegate = self;
[appSession activateSession];
}
I believe this should solve your problem.
I have been trying to use WCSession on WatchKit 2.0 and iOS 9 but it does not seem to work.
I keep getting the same error message:
Error Domain=WCErrorDomain Code=7005 "Device is not paired." UserInfo={NSLocalizedDescription=Device is not paired., NSLocalizedRecoverySuggestion=Pair the device with a Watch.}
In the App Watch Interface I added WCSessionDelegate and in awakeWithContext: initialize the session like this:
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
In the iOS app I added WCSessionDelegate and initialized in application: didFinishLaunchingWithOptions: the same way but it is never paired on either location.
The Apple Watch IS Paired it just does not work with WCSession
And when I call the following I got the error.
[session sendMessage:userInfo replyHandler:^(NSDictionary *reply) {
NSLog(#"AW open parent: Done");
}
errorHandler:^(NSError *error) {
NSLog(#"AW open parent ERROR: %#",error.description);
}
];
Any ideas what is going on?
UPDATE:
I restarted all devices + Xcode
Apple Watch is 100% paired
Delegates are set and session is activated
iOS app does not starts at all but it works more or less with openParentApplication: reply: (The app supposed to do some heavy lifting but it terminates the app in the middle of it even though it is beginBackgroundTask is used)
I want to send instant message to iOS app from watch app. Implemented the following code in XCode7 beta 4 version and keeping the application in foreground in both simulators. here is the code I implemented
In watchkit interfaceController
-(void)willActivate
{
[super willActivate];
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
-(IBAction)buttonClicked
{
NSDictionary *applicationDict = [[NSDictionary alloc] initWithObjects:#[#"Hi"] forKeys:#[#"key"]];
if([[WCSession defaultSession] isReachable])
{
[[WCSession defaultSession] sendMessage:applicationDict
replyHandler:^(NSDictionary *reply) {
NSLog(#"%#",reply);
}
errorHandler:^(NSError *error) {
NSLog(#"%#",error);
}];
}
}
In iOS app class
-(void)viewDidLoad
{
[super viewDidLoad];
if ([WCSession isSupported]){
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
-(void)session:(nonnull WCSession *)session
didReceiveMessage:(nonnull NSDictionary *)message replyHandler:(nonnull void (^)(NSDictionary * __nonnull))replyHandler
{
dispatch_async(dispatch_get_main_queue(), ^{
self.testLbl.text = [message objectForKey:#"key"];
[self.view setNeedsDisplay];
});
}
Do you have to use the sendMessage APIs? I found them unreliable and unpredictable as well. I ended up using the applicationContext API's. The watch doesn't have to be reachable, but if it is, it arrives immediately, If not reachable, it gets delivered on app launch. Each time you update the application context, it overwrites the previous version, which might not be what you are looking for.
I found in an iPhone app I am currently working on that I needed to have the WCSession activation code in both the AppDelegate and the current View Controller. ...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
...
Like you, that does not marry with my understanding of what is supposed to be required, but it is what got session.reachable (Swift) to equal true
First you should check if the Watch Connectivity Framework is linked correctly, also check your code. After that try with "Reset content and settings" from both simulators, this worked for me. In case it doesn't work yet, try uninstalling and reinstalling both apps from simulators. If it still doesn't work, try with removing the watch app extension from settings on the Watch app installed on the phone. Hope this helps!
I've seen a similar question posted on how to send data back and forth in Swift. I'm asking the same question but in Objective-C. I've also viewed Apple's transition docs.
I work best with clear examples, rather than lecture material. So if someone has implemented this and wouldn't mind sharing, that would be much appreciated.
Here´s a link to a Q/A about WatchConnectivity: Send messages between iOS and WatchOS with WatchConnectivity in watchOS2
I will give you an example go ApplicationContext, there are 2 other messaging techniques with WatchConnectivity. Please watch WWDC2015 session video for those.
First you need to conform to the WCSessionDelegate protocol in the classes you want to send and receive data from/to. E.g both on watch and iPhone.
Basic checking before: (this is just an example, implement better than this)
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
NSLog(#"SESSION AVAIBLE");
}
//Objective-C
if ([[WCSession defaultSession] isReachable]) {
NSLog(#"SESSION REACHABLE");
}
This will send the data from the phone to the watch.
WCSession *session = [WCSession defaultSession];
NSError *error;
[session updateApplicationContext:#{#"firstItem": #"item1", #"secondItem":[NSNumber numberWithInt:2]} error:&error];
This will receive the data from the phone on the watch.
- (void) session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
NSLog(#"%#", applicationContext);
item1 = [applicationContext objectForKey:#"firstItem"];
item2 = [[applicationContext objectForKey:#"secondItem"] intValue];
}
The WWDC2015 video on WatchConnectivity is really great, I recommend to check it out.