how can I use WCSessionUserInfoTransfer in watchos? - ios

is anywhere a simple tutorial, how I can use the WCSessionUserInfoTransfer to change data between my watch and iOS ?
And what must be written in the delegate of the iOS -App?
in my old programming I used:
[WKInterfaceController openParentApplication:#{#"Kommando":#"Radius"} reply:^(NSDictionary *replyInfo, NSError *error) {
if (error) {
NSLog(#"Error from parent: %#", error);
} else {
NSLog(#"Radius from parent: %#", [replyInfo objectForKey:#"Radius"]);
}
}];

The first thing you need to do is to define a WCSession. This must be defined and activated in every class that you're planning on transferring data to and receiving data from. To use WCSession, make sure it's supported, and then activate the default session as shown below.
#import <WatchConnectivity/WatchConnectivity.h>
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
From here, you can use transferUserInfo where ever you need to send data (from the watch or the iOS app):
[[WCSession defaultSession] transferUserInfo:#{#"Kommando":#"Radius"}];
On the receiving end, you would use session:didReceiveUserInfo. Note that this does NOT need to be in the app delegate anymore on the iOS app side, unlike handleWatchKitExtensionRequest. You can use this where ever you need to receive the data. Make sure to activate WCSession, as shown above, in the class where you have didReceiveUserInfo also.
- (void)session:(nonnull WCSession *)session didReceiveUserInfo:(nonnull NSDictionary<NSString *,id> *)userInfo {
NSLog(#"Radius from parent: %#", [userInfo objectForKey:#"Radius"]);
}

Related

watchOS Send Message not getting called

Hi I'm having an issue with my code it looks like send message isn't getting called for some reason, thanks
if ([[WCSession defaultSession] isReachable]) {
NSLog(#"Initiating WCSession to Read iPhone Data");
[[WCSession defaultSession] sendMessage:watchData replyHandler:^(NSDictionary *dataFromPhone) {
NSLog(#"Sending Empty Write Data Array to iPhone...%#", watchData);
}
errorHandler:^(NSError *error) {
// Log error
NSLog(#"Error: %#", error);
}];
} else {
//we aren't in range of the phone, they didn't bring it on their run
NSLog(#"Unable to connect to iPhone");
}
From what I see this is the code that runs on iOS, which controls whether the Apple Watch is reachable, but you have to remember (if you have not done of course) to enable the session from either device with the following code, so enable the communication system
if (WCSession.isSupported()) {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}

didReceiveApplicationContext not being called

I have just set up WCConnectivity in my app using tutorials/sample code and I feel I have it implemented correctly. I am not using the simulator to test. For some reason, the didReceiveApplicationContext is not being called in the watch app, even though everything is set up correctly.
I've tried calling in the the Interface Controller of the WatchKit app, in the ExtensionDelegate and using NSUserDefaults to set the Interface Controller data instead.
iOS App
ViewController.m
- (void) viewDidLoad{
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
-(void) saveTrip{
NSMutableArray *currentTrips = [NSMutableArray arrayWithArray:[self.sharedDefaults objectForKey:#"UserLocations"]];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:newLocation];
[currentTrips addObject:data];
[self.sharedDefaults setObject:currentTrips forKey:#"UserLocations"];
[self.sharedDefaults synchronize];
WCSession *session = [WCSession defaultSession];
NSDictionary *applicationDict = [[NSDictionary alloc] initWithObjects:#[currentTrips] forKeys:#[#"UserLocations"]];;
[session updateApplicationContext:applicationDict error:nil];
}
Watch Extension Code
ExtensionDelegate.m
- (void)applicationDidFinishLaunching {
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
- (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
self.places= [applicationContext objectForKey:#"UserLocations"];
[[NSUserDefaults standardUserDefaults] setObject:self.places forKey:#"UserLocations"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
InterfaceController.m
- (void)willActivate {
[super willActivate];
self.placesData = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"UserLocations"]];
[self loadData];
}
For me it was two things:
Passing invalid values in the dictionary. Can't even pass NSNull() to represent nil values. If you have data in there that can't be represented in a plist, it fails.
If the dictionary doesn't change, subsequent calls to updateApplicationContext won't trigger a corresponding call to didReceiveApplicationContext. To force an update—perhaps in debug builds—you could add a UUID to the payload, e.g.
context["force_send"] = UUID().uuidString
It might prove useful to handle any errors, so change:
[session updateApplicationContext:applicationDict error:nil];
to
NSError *error;
if (![session updateApplicationContext:applicationDict error:&error]) {
NSLog(#"updateApplicationContext failed with error %#", error);
}
If it's not working, it's probably not implemented correctly.
A few issues at first glance:
In your iOS app, you get a reference to the shared session and activate it, but don't assign that local variable to any property on your view controller. This means your local variable will go out of scope, once viewDidLoad exits. You also need to correct that in your watch extension.
In saveTrip, you again create another local session, which isn't activated and doesn't have any delegate. You need to use the first session that you set up and activated earlier.
On your watch, you save data that is received but your interface controller won't know that there is new data that it should load and display.
A few tips:
If you setup and activate the session in application:didFinishLaunchingWithOptions, it will be available app-wide (and activated much earlier in your app's lifecycle).
You should check that your session is valid, as a watch may not be paired, or the watch app may not be installed. Here's a good (Swift) tutorial that covers those cases, and also uses a session manager to make it easier to support using Watch Connectivity throughout your app.
You may want to pass less/lighter data across to the watch, than trying to deal with archiving an array of custom objects.
As an aside, what you're trying to do with NSUserDefaults is very convoluted. It's really meant to persist preferences across launches. It's not appropriate to misuse it as a way to pass your model back and forth between your extension and controller.
I had the same issue and fixed.
I forgot to add WatchConnectivity framework in watch extension.

Using WCSession with more than one ViewController

I found many questions and many answers but no final example for the request:
Can anyone give a final example in Objective C what is best practice to use WCSession with an IOS app and a Watch app (WatchOS2) with more than one ViewController.
What I noticed so far are the following facts:
1.) Activate the WCSession in the parent (IOS) app at the AppDelegate:
- (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];
}
}
2.) On the WatchOS2 side use <WCSessionDelegate>. But the rest is totally unclear for me! Some answers are talking from specifying keys in the passing Dictionary like:
[session updateApplicationContext:#{#"viewController1": #"item1"} error:&error];
[session updateApplicationContext:#{#"viewController2": #"item2"} error:&error];
Others are talking about retrieving the default session
WCSession* session = [WCSession defaultSession];
[session updateApplicationContext:applicationDict error:nil];
Others are talking about different queues? "It is the client's responsibility to dispatch to another queue if necessary. Dispatch back to the main."
I am totally confused. So please give an example how to use WCSession with an IOS app and a WatchOS2 App with more than one ViewController.
I need it for the following case (simplified):
In my parent app I am measuring heart rate, workout time and calories. At the Watch app 1. ViewController I will show the heart rate and the workout time at the 2. ViewController I will show the heart rate, too and the calories burned.
As far as I understand the task you just need synchronisation in a Phone -> Watch direction so in a nutshell a minimum configuration for you:
Phone:
I believe the application:didFinishLaunchingWithOptions: handler is the best place for the WCSession initialisation therefore place the following code there:
if ([WCSession isSupported]) {
// You even don't need to set a delegate because you don't need to receive messages from Watch.
// Everything that you need is just activate a session.
[[WCSession defaultSession] activateSession];
}
Then somewhere in your code that measures a heart rate for example:
NSError *updateContextError;
BOOL isContextUpdated = [[WCSession defaultSession] updateApplicationContext:#{#"heartRate": #"90"} error:&updateContextError]
if (!isContextUpdated) {
NSLog(#"Update failed with error: %#", updateContextError);
}
update:
Watch:
ExtensionDelegate.h:
#import WatchConnectivity;
#import <WatchKit/WatchKit.h>
#interface ExtensionDelegate : NSObject <WKExtensionDelegate, WCSessionDelegate>
#end
ExtensionDelegate.m:
#import "ExtensionDelegate.h"
#implementation ExtensionDelegate
- (void)applicationDidFinishLaunching {
// Session objects are always available on Apple Watch thus there is no use in calling +WCSession.isSupported method.
[WCSession defaultSession].delegate = self;
[[WCSession defaultSession] activateSession];
}
- (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
NSString *heartRate = [applicationContext objectForKey:#"heartRate"];
// Compose a userInfo to pass it using postNotificationName method.
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:heartRate forKey:#"heartRate"];
// Broadcast data outside.
[[NSNotificationCenter defaultCenter] postNotificationName: #"heartRateDidUpdate" object:nil userInfo:userInfo];
}
#end
Somewhere in your Controller, let's name it XYZController1.
XYZController1:
#import "XYZController1.h"
#implementation XYZController1
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleUpdatedHeartRate:) name:#"heartRateDidUpdate" object:nil];
}
-(void)handleUpdatedHeartRate:(NSNotification *)notification {
NSDictionary* userInfo = notification.userInfo;
NSString* heartRate = userInfo[#"heartRate"];
NSLog (#"Successfully received heartRate notification!");
}
#end
Code hasn't been tested I just wrote it as is so there can be some typos.
I think the main idea now is quite clear and a transfer of remaining types of data is not that tough task.
My current WatchConnectivity architecture much more complicated but nevertheless it is based on this logic.
If you still have any questions we might move a further discussion to the chat.
Well, this is simplified version of my solution as requested by Greg Robertson. Sorry it's not in Objective-C anymore; I'm just copy-pasting from existing AppStore-approved project to make sure there will be no mistakes.
Essentially, any WatchDataProviderDelegate can hook to data provider class as that provides array holder for delegates (instead of one weak var).
Incoming WCSessionData are forwarded to all delegates using the notifyDelegates() method.
// MARK: - Data Provider Class
class WatchDataProvider: WCSessionDelegate {
// This class is singleton
static let sharedInstance = WatchDataProvider()
// Sub-Delegates we'll forward to
var delegates = [AnyObject]()
init() {
if WCSession.isSupported() {
WCSession.defaultSession().delegate = self
WCSession.defaultSession().activateSession()
WatchDataProvider.activated = true;
}
}
// MARK: - WCSessionDelegate
public func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
processIncomingMessage(userInfo)
}
public func session(session: WCSession, didReceiveApplicationContext applicationContext: [String: AnyObject]) {
processIncomingMessage(applicationContext)
}
func processIncomingMessage(dictionary: [String:AnyObject] ) {
// do something with incoming data<
notifyDelegates()
}
// MARK: - QLWatchDataProviderDelegate
public func addDelegate(delegate: AnyObject) {
if !(delegates as NSArray).containsObject(delegate) {
delegates.append(delegate)
}
}
public func removeDelegate(delegate: AnyObject) {
if (delegates as NSArray).containsObject(delegate) {
delegates.removeAtIndex((delegates as NSArray).indexOfObject(delegate))
}
}
func notifyDelegates()
{
for delegate in delegates {
if delegate.respondsToSelector("watchDataDidUpdate") {
let validDelegate = delegate as! WatchDataProviderDelegate
validDelegate.watchDataDidUpdate()
}
}
}
}
// MARK: - Watch Glance (or any view controller) listening for changes
class GlanceController: WKInterfaceController, WatchDataProviderDelegate {
// A var in Swift is strong by default
var dataProvider = WatchDataProvider.sharedInstance()
// Obj-C would be: #property (nonatomic, string) WatchDataProvider *dataProvider
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
dataProvider.addDelegate(self)
}
// WatchDataProviderDelegate
func watchDataDidUpdate() {
dispatch_async(dispatch_get_main_queue(), {
// update UI on main thread
})
}}
}
class AnyOtherClass: UIViewController, WatchDataProviderDelegate {
func viewDidLoad() {
WatchDataProvider.sharedInstance().addDelegate(self)
}
// WatchDataProviderDelegate
func watchDataDidUpdate() {
dispatch_async(dispatch_get_main_queue(), {
// update UI on main thread
})
}}
}
Doing the session management (however WCSession is singleton) in a View-Controller smells like MVC violation (and I've seen too many Watch blog posts wrong this way already).
I made an umbrella singleton class over the WCSession, that is first strongly referenced from Watch Extension Delegate to make sure it will load soon and do not get deallocated in the middle of work (e.g. when a View-Controller disappears while transferUserInfo or transferCurrentComplicationUserInfo happens in another watch thread).
Only this class then handles/holds the WCSession and decouples the session data (Model) from all the View-Controller(s) in watch app, exposing data mostly though public static class variables providing at least basic level of thread-safety.
Then this class is used both from complication controller, glance controller and other view controllers. Updates run in the background (or in backgroundFetchHandler), none of the apps (iOS/WatchOS) is required to be in foreground at all (as in case of updateApplicationContext) and the session does not necessarily have to be currently reachable.
I don't say this is ideal solution, but finally it started working once I did it this way. I'd love to hear that this is completely wrong, but since I had lots of issues before going with this approach, I'll stick to it now.
I do not give code example intentionally, as it is pretty long and I don't want anyone to blindly copy-paste it.
I found, with "try and error", a solution. It is working, but I don't know exactly why! If I send a request from the Watch to the IOS app, the delegate of that ViewController of the Watch app gets all the data from the main queue from the IOS app. I added the following code in the - (void)awakeWithContext:(id)context and the - (void)willActivate of all ViewControllers of the Watch app:
By example 0 ViewController:
[self packageAndSendMessage:#{#"request":#"Yes",#"counter":[NSString stringWithFormat:#"%i",0]}];
By example 1 ViewController1:
[self packageAndSendMessage:#{#"request":#"Yes",#"counter":[NSString stringWithFormat:#"%i",1]}];
/*
Helper function - accept Dictionary of values to send them to its phone - using sendMessage - including replay from phone
*/
-(void)packageAndSendMessage:(NSDictionary*)request
{
if(WCSession.isSupported){
WCSession* session = WCSession.defaultSession;
session.delegate = self;
[session activateSession];
if(session.reachable)
{
[session sendMessage:request
replyHandler:
^(NSDictionary<NSString *,id> * __nonnull replyMessage) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#".....replyHandler called --- %#",replyMessage);
NSDictionary* message = replyMessage;
NSString* response = message[#"response"];
[[WKInterfaceDevice currentDevice] playHaptic:WKHapticTypeSuccess];
if(response)
NSLog(#"WK InterfaceController - (void)packageAndSendMessage = %#", response);
else
NSLog(#"WK InterfaceController - (void)packageAndSendMessage = %#", response);
});
}
errorHandler:^(NSError * __nonnull error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"WK InterfaceController - (void)packageAndSendMessage = %#", error.localizedDescription);
});
}
];
}
else
{
NSLog(#"WK InterfaceController - (void)packageAndSendMessage = %#", #"Session Not reachable");
}
}
else
{
NSLog(#"WK InterfaceController - (void)packageAndSendMessage = %#", #"Session Not Supported");
}
}

WatchKit openParentApplication:reply

I have currently a problem.
My wanted behaviour: If I open my WatchKit App, I call "openParentApplication". I receive my wanted data.
But if I tested on real devices, it doesnt work since I open the parent app in the iPhone.
But when I'm testing in simulator it works without to open the parent app.
My Xcode Version is 6.3.2 and iOS 8.3.
What could be the problem?
InterfaceController.m
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
NSDictionary *userInfo = #{#"request":#"refreshData"};
[WKInterfaceController openParentApplication:userInfo reply:^(NSDictionary *replyInfo, NSError *error)
{
entries = replyInfo;
NSLog(#"Reply: %#",replyInfo);
[self reloadTable];
[self.city setText:[entries valueForKey:#"city"][0] ];
}];
}
AppDelegate.m
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
NSString *refresh = [userInfo valueForKey:#"request"];
if([refresh isEqualToString:#"refreshData"])
{
NSString *city = [[NSUserDefaults standardUserDefaults] stringForKey:#"City"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:[NSString stringWithFormat:#"http://blackdriver.adappter.de/api/retrieve.php?city=%#",[city stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
reply(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"Error: %#", error);
}];
}
}
EDIT - Correct answer:
See the link from mohammed alwaili in the comments
A openParentApplication:reply request must return immediately, so you'll have to request extra time for your asynchronous request to finish (alternately run a synchronous request, but this is terrible practice).
From the Apple WatchKit Developer Tips and Best Practices:
If your app on Apple Watch needs to perform longer running background
tasks, such as networking calls, you should rely on your iPhone app to do the work. Use the openParentApplication:reply: method in WKInterfaceController to wake up your iPhone app in the background and return the data that your WatchKit extension needs. The UIApplicationDelegate method that handles the WatchKit request must return immediately. If an asynchronous call is required, to perform networking for example, use a background task to make sure your app is not suspended before it has a chance to send its reply.
I had a similar issue and had to work through a few problems to have my WatchKit app successfully call iOS app which made an asynchronous API call.
Here's a working code snippet
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
let backgroundProcessingToken = application.beginBackgroundTaskWithName("backgroundApiCall", expirationHandler: { () -> Void in
reply(["response":["error":"SOME_ERROR_CODE_INDICATING_TIMEOUT"]])
})
request(.GET, "https://api.forecast.io/forecast/[INSERT DARK SKY API CODE]/37.8267,-122.423").responseJSON(options: NSJSONReadingOptions.AllowFragments, completionHandler:{request, response, data, error in
if(error != nil || data == nil){
reply(["response":["error":"SOME_ERROR_CODE_INDICATING_FAILURE"]])
}
if let json = data as? NSDictionary {
reply(["response":["data":json]])
}
application.endBackgroundTask(backgroundProcessingToken)
})
}
Ultimately, you need to register as a background task to ensure your app doesn't get killed by the operating system.
I also have a working example here on github FWIW

Apple watch app does not work when iPhone app is in terminated state?

I am working on an apple watch app, but facing a weird issue, i.e. my watch app works only when I manually open the iPhone app or when iPhone app is in background. When I terminate my iPhone app and test apple watch app then it does not work any more.
Here I am mentioning watch app flow:
When apple watch app starts, I call a web api to fetch response from server.
I used openParentApplication:reply: method to call web api from parent app
I understand that, I will have to call web api method in a background thread because, openParentApplication:reply: method automatically open the parent app in iPhone and suspends in a mean time, so if we are processing a time taking task using this method then we should use a background thread as mentioned under WatchKit Development Tips. So I am using a background thread to call web api.
When I get response I pass it to watch app.
Here is the attached snippet:
Watch App - InitialInterfaceController.m
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
}
- (void)willActivate {
[super willActivate];
[self getDetails];
}
- (void)getDetails{
//Open parent app
[WKInterfaceController openParentApplication:#{#“request”:#“details”}
reply:^(NSDictionary *replyInfo, NSError *error) {
if (!error) {
NSLog(#“Success”);
[self parseKPI:replyInfo];
}
else{
NSLog(#"Error - %#", error.localizedDescription);
}
}];
}
iPhone App - AppDelegate.m
- (void)application:(UIApplication *)application
handleWatchKitExtensionRequest:(NSDictionary *)userInfo
reply:(void (^)(NSDictionary *))reply{
NSString *request = [userInfo objectForKey:#“request”];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// Get Details
if ([request isEqualToString:#"details"]) {
APIHandler *api = [[APIHandler alloc] init];
[api getDetailsUsername:#“my_user_name”
onSuccess:^(NSDictionary *details) {
dispatch_async(dispatch_get_main_queue(), ^{
reply(details);
});
} onFailure:^(NSString *message) {
dispatch_async(dispatch_get_main_queue(), ^{
reply(#{#"Error":message});
});
}];
}
}
iPhone App - APIHandler.m
- (void) getDetailsUsername:(NSString *)username
onSuccess:(void(^)(NSDictionary * details))success
onFailure:(void(^)(NSString *message))failure{
NSString *urlString = [NSString stringWithFormat:#"%#%#", HOST, DETAILS_API];
urlString = [urlString stringByAppendingFormat:#"?username=%#",username];
urlString = [urlString stringByAppendingFormat:#"&%#", self.APIKeyParameter];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *mutableURL = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:mutableURL
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
NSError *error = nil;
NSDictionary *details = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableLeaves
error:&error];
success(details);
}
else{
failure(#"Connection Error!");
}
}];
}
But this approach is not working for me.
I found one more issue in watch app simulator i.e.
- (void)awakeWithContext:(id)context for my initial view controller is called, but - (void)willActivate method is not being called sometimes and I just see watch app spinner. Some times it works. It’s quite strange. I have around 15 controls (including all groups) in initial interface controller added using storyboard.
I have also referred Watchkit not calling willActivate method and modified my code but still facing same issue.
Can any one let me know why is this issue persisting in my app?
You'll need to handle the request in the iPhone app a bit differently in order for your app to not be killed off by the OS. I've shared a similar answer here: https://stackoverflow.com/a/29848521/3704092

Resources