How to Send Data from iPhone to Apple Watch in Os2 in Objective-C

How to send data from Iphone to Apple Watch in OS2 in Objective-C


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.

How to pass an array data from iphone to apple watch in objective c

If you use watchOS 1, you can share data between your watch and iOS App with App Groups.

ref.

  • Share Data in your Swift WatchKit Apps with App Groups
  • WATCHKIT: BEST PRACTICES FOR SHARING DATA BETWEEN YOUR WATCH AND IOS APP

EDIT:

On the iphone side, serialize your data.

NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:resultTracks];
NSUserDefaults *defaults = [[NSUserDefaults alloc]
initWithSuiteName:@"group.com.example.mygroup"];
[defaults setObject:encodedObject forKey:@"WatchHomeViewTableList"];
[defaults synchronize];

And unserialize your data.

NSUserDefaults *defaults = [[NSUserDefaults alloc]
initWithSuiteName:@"group.com.example.mygroup"];
NSData *encodedObject = [defaults objectForKey:@"WatchHomeViewTableList"];
NSMutableArray *resultTracks = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];

Send messages between iOS and WatchOS with WatchConnectivity in watchOS2

WatchConnectivity

First the two classes that are supposed to communicate with each other (iOS and watchOS) need to conform the <WCSessionDelegate> and #import the WatchConnectivity framework

Before you can send data you need to check if your device is able to send data

if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
NSLog(@"WCSession is supported");
}

Then if you wish to use "interactive messaging" (sendMessage APIs) you will need to see if the other device is reachable first:

if ([[WCSession defaultSession] isReachable]) {
//Here is where you will send you data
}

The "background operations" APIs do not require the counterpart device to be reachable at the point in time you call the WCSession API.

You have several options when it comes to transferring data between your apps, in the Apple Documentation they are described like this:

  • Use the updateApplicationContext:error: method to communicate only the most recent state information to the counterpart. When the counterpart wakes, it can use this information to update its own state and remain in sync. Sending a new dictionary with this method overwrites the previous dictionary.

  • Use the sendMessage:replyHandler:errorHandler: or sendMessageData:replyHandler:errorHandler: method to transfer data immediately to the counterpart. These methods are intended for immediate communication when your iOS app and WatchKit extension are both active.

  • Use the transferUserInfo: method to transfer a dictionary of data in the background. The dictionaries you send are queued for delivery to the counterpart and transfers continue when the current app is suspended or terminated.

  • Use the transferFile:metadata: method to transfer files in the background. Use this method in cases where you want to send more than a simple dictionary of values. For example, use this method to send images or file-based documents.

I will give you an example how to send/receive data with Application Context

Send data:

WCSession *session = [WCSession defaultSession];
NSError *error;

[session updateApplicationContext:@{@"firstItem": @"item1", @"secondItem":[NSNumber numberWithInt:2]} error:&error];

Receive data:

- (void) session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {

NSLog(@"%@", applicationContext);

NSString *item1 = [applicationContext objectForKey:@"firstItem"];
int item2 = [[applicationContext objectForKey:@"secondItem"] intValue];
}

For more information about WatchConnectivity I really recommend watching the WWDC2015 session video and reading the Apple Documentation on WatchConnectivity

How can I send an array to the Apple Watch from my iPhone with Objective-C?

In watchOS2 they introduces a nice feature called WatchConnectivity. Please look at my reply to this question: How to send data from Iphone to Apple Watch in OS2 in Objective-C

Its easy to send an array instead of String/NSNumber.

Apple Watch OS 2: Is there a to transfer data from iPhone to Watch App without Watch app being in foreground?

What you can do is to use the updateApplicationContext:error: method on WCSession object to send updated data to your watch. When your watch app wakes up, it will receive the context object with the updated data.

watchkit , iOS sending data between watch and iphone

AFAIK, you can not share data directly, like send some datas between them.

What you can do is write data to same file.

See this blog post:

http://www.atomicbird.com/blog/sharing-with-app-extensions

How to send data from Iphone to Watchkit in OS2 in SWIFT

This blog post should help you out.

From that post: First, you'll create and activate a WCSession like so:

if (WCSession.isSupported()) {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}

For transferring a dictionary:

let applicationDict = // Create a dict of application data
let transfer = WCSession.defaultSession().transferUserInfo(applicationDict)

Then, on the receiving end, you'll need to implement session:didReceiveUserInfo: (Developer documentation). Note, according to Apple's "watchOS2 Transition Guide,"

To begin communication, both your Watch app and your iOS app must have an active WCSession object. Typically, each app creates, configures, and activates a session object at launch time and stores a reference to it in a central location. When you want to send data, you retrieve the session object and call its methods.



Related Topics



Leave a reply



Submit