How to get the current location in a WatchKit app? - ios

If I'm not wrong, the App Watch can't take locations itself and needs to request it to the iPhone, so the current location is got in the WatchKit extension at iOS side... right?
I think this is a quite common thing WatchKit developers would do, but I don't find a clear tutorial/example of it, could somebody tell me one or post me some code? In addition, do you know if the way to deal with locations changes in watchOS 2?
EDIT: I've found that in some posts it is said that CoreLocation can be accessed from the WatchKit extension, and in others I've read that only the iOS app can... which is the correct?

You can access CoreLocation and all its methods directly from WatchKit itself. There's no need to employ any sort of additional logic, such as opening the parent app.
In watchOS 1, your WatchKit app is technically running on the iPhone, and you can therefore use (almost) any frameworks available to you on the iPhone (such as CoreLocation).
In watchOS 2, the WatchKit app is running on the Watch itself. I have done some research and I've not found anything that indicates you will have to make any changes to your usage of CoreLocation, and I will therefore assume the frameworks automatically handle the communication between the iPhone and the Watch.
I'll update this answer if I find anything indicating that you will have to employ some other logic to make this work in watchOS 2.

Related

watchOS Complications: How to add context-based deep-links?

I want to create complications that when tapped open the watch app to a specific section in the app. I am using CloudKit and CLKComplicationDataSource.
Apple documentation even recommends deep-linking, but I haven't been able to find out how to do this. There is another similar question but no correct answer.
Note: I need to support watchOS versions below 9, so I cannot use widgetKit and I must use ClockKit (in case that makes a difference)
Here is what Apple says:

Share AsyncStorage from iOS react native app with Apple Watch

I'm developing the main app with react native and the Apple Watch with Swift.
The main app (react native) uses AsyncStorage to store some values, that then are used as params for a fetch that shows a list.
After running the simulator, I found manifest.json inside
~/Library/Developer/CoreSimulator/Devices/D10E869B-040B-446F-9B8B-754F111442EC/data/Containers/Data/Application/AC837AFE-312B-4861-906D-EC9EEE7D029B/Document
s/RCTAsyncLocalStorage_V1
with the data inside.
I need to access those values in the Apple Watch in Xcode to do the same job as the iOS app. How do I access that data?
Since Apple Watch apps run independently from iOS app since watchOS 2, there’s really no way to directly access the data stored locally on iPhone — you’ll need to maintain some form of communication between your iOS and watchOS apps.
The framework intended for this is called WatchConnectivity. I see two more-or-less suitable solutions for your case:
Using transferFile to transfer your file from an iOS device to an Apple Watch whenever it got updated. The viability of this option highly depends on how big your file is.
Or you can just use updateApplicationContext to pass an already serialized data instead of transferring a raw JSON file — again, highly depends on your specific needs.
Unfortunately, I don’t know how WatchConnectivity is supposed to work with ReactNative. If you want a deeper introduction to this framework — I highly recommend watching this amazing video from WWDC 2015.

watchOS Show App When Message Received [duplicate]

I know that an apple watch app can launch its parent application in the background to pass data back and forth but can it work the other way around? Can my iOS app launch the related watchOS app?
For a bit more insight, I'm trying to accomplish this so that I can monitor data periodically for the sake of notifications. The 70 seconds apple gives me to keep an app active and pulling data from healthKit isn't conducive to an app that provides alerts to users based on changes in behavior.
I apologize if this is a basic question. I'm new to watchOS development and haven't been able to find the answer to this anywhere. =/
No it cannot happen.
At the moment communication between the Watch and the Device is limited to the Watch App initialising communication with the iOS app and not the other way around.
This might help:
https://developer.apple.com/library//ios/documentation/General/Conceptual/WatchKitProgrammingGuide/SharingData.html#//apple_ref/doc/uid/TP40014969-CH29-SW1
Yes, you can. Try the startWatchApp(with:completion:) func in HealthStore. However, it can only be used in a workout app that enable the background mode of "workout processing".
Unfortunately I think I found the answer here How to programmatically open Apple Watch companion app from iOS app.
It doesn't look like it can be done. If anyone finds otherwise, let me know!

Can I access healthkit data from iWatch extension?

I am making a iWatch app where I need to show data from healthkit. is it possible to access healthkit APIs from iWatch extension?
No. It's mentioned specifically in the HealthKit Framework Reference:
"You cannot access HealthKit from extensions (like the Today view) or from a WatchKit app."
What you can do is call openParentApplication:reply: to talk to the iPhone app itself and retrieve that data. Search around for that method name and you'll find some examples on how to call it and get data back to the Watch from it.
UPDATE: As others have mentioned below, this has changed for watchOS 2. The documentation has not been updated yet, but check out the WWDC 2015 videos for HealthKit and the Watch for some snippets of code you can use with the Xcode 7 beta.
Yes, in watchOS 2 you will be able to.
Yes, it's possible on watchOS 2.
However, it's a bit confusing, because it's still mentioned in the HealthKit Framework Reference for watchOS
You cannot access HealthKit from extensions (like the Today view) or from a WatchKit app.
Do not care about this.
Follow a few steps below.
Follow Setting Up HealthKit in the reference, you are now ready to access
Follow Accessing HealthKit Data, then you will be able to access
Make sure your application is running on watchOS 2.
Please see the reference for more detail.

Using CoreLocation with Apple Watchkit

The apple iOS/Watch simulator works great with the default Core Location implementation.
However, does this really work with the device later on? Or do I need to build my implementation around a openParentApplication-pattern?
Thanks,
Since your watch extension runs on the phone, it has access to the same features as a normal iOS app and can use CoreLocation. However, it uses the CoreLocation permissions that were granted to your main app, so make sure your main app uses CoreLocation too so that the user will be asked to give your app permission to access those services.
WatchKit extensions have access to the same technologies found in iOS apps but because they are extensions, the use of some technologies may be restricted and the use of others is not recommended.
Apple recommends to avoid using technologies that request user permission, like Core Location.
Using the technology from your WatchKit extension could involve
displaying an unexpected prompt on the user’s iPhone the first time
you make the request. Worse, it could happen at a time when the iPhone
is in the user’s pocket and not visible.
The best solution for performing any long-running tasks is to let your
iOS app perform the task instead. For example, instead of starting
location services in your WatchKit extension, start it in your iOS
app. Your iOS app can gather the needed data and put it in a shared
app group so that your extension can access it later.

Resources