Something like shared NSUserDefaults and App Groups in watchOS 2? - swift

I am currently updating an app of mine for watchOS 2 and got some trouble with communication between the devices: previously I had been using shared NSUserDefaults to enable both the Watch and the iOS device to read and write several values from one App Group independently.
Now that watchOS 2 apps are running natively on the Watch and the Watch Connectivity Framework replaced previous communication options, this no longer seems applicable. However, from what I read on the reference, implementing the same functionality I had in watchOS 1 with Watch Connectivity is rather cumbersome…
Is there any other option for me to create some sort of container/file/database/whatsoever that I can access and update from both my Watch app and the related iOS app?

With watchOS 2 both the WatchKit App and Extension run natively on Apple Watch, so even if you use shared user defaults or app groups the data you put inside them will be on the watch and so not accessible from the iOS app.
To send data to the watch you can use the WatchConnectivity framework, or use a NSURLSession to download data from a server if that's your case.

Related

IOS & WatchOS background communication

I have a IOS application with a companion WatchOS application. I am wondering if there is a way I can communicate with the IOS application in real time while it not running as a foreground application.
The best example I can think of is how your are able to use the Spotify watch app to change songs, like songs or even change the volume of the phone while the phone is locked.
How could I implement something like this for an application that could possibly send data frequently from the watch to the phone while the phone is locked.
Watch apps can launch their companion iOS apps by messaging them:. https://developer.apple.com/documentation/watchconnectivity/wcsession/1615687-sendmessage
That won’t work if the iOS device requires unlock because it’s just booted up.
I just started WatchOS development and needed to send a message to the watch app - if it's available I use sendMessage. If the watch app is backgrounded, I use transferUserInfo. Link to code snippets. I check isReachable and isPaired (on the phone, isPaired does not exist in WatchOS).

How to release only independent Apple Watch app to App Store from combined iOS/Watch Xcode project

I used the iOS app with Watch app Xcode template and plan to eventually release both an iOS app and Apple Watch app. The Apple Watch app is simpler so I'd like to release just the watch app on the App Store initially.
Is it possible to just release the Watch app portion of an Xcode project to the App Store?
First of all You need to have three target at least, iOS app, WatchOS app, and Watchkit Extension app.
Select watch app and make sure Build identifier follows the pattern .watchkitapp (example: com.ryanwatchapp.app.wathckitapp)
Select watchkit extension app and make sure Build identifier follows the pattern .watchkitapp.watchkitextension (example: com.ryanwatchapp.app.wathckitapp.watchkitextension)
Other processes are same like iOS apps; archive,increase the build number and submit.

Stand-alone Apple Watch app - CoreBluetooth

Is it possible to create an Apple Watch application without a need of an iPhone app ? I want to create a simple app that uses the bluetooth framework of the Apple WAtch and search for near devices. (without having an iphone around). Thank you
In iOS 13 it's possible to create independent watchOS application. More information can be found here. You have to download Xcode 11 to be able to create independent watchOS application.
To create it simply go to File -> New -> Project and select watchOS -> App as it's shown on a screenshot:
Be aware that even though it's possible to create indepentent watchOS app (e.g. with CoreBluetooth) there will be some limitations with methods usage. For example it's not possible to call this method of CBMutableService:
- (instancetype)initWithType:(CBUUID *)UUID primary:(BOOL)isPrimary NS_DESIGNATED_INITIALIZER __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
You cannot create a watchOS application without an iOS app. First of all, you can only add a Watch extension to an existing iOS project in Xcode. Secondly, even if you could create a standalone watchOS project in Xcode, you wouldn't be able to publish it to the AppStore, since each WatchKit app needs to have a functioning iOS counterpart to be accepted in the AppStore, so you cannot just create a dummy iOS app.

Can I build a stand alone Apple Watch app?

In WWDC 2015 it is mentioned that watch supports native apps but I fail to see the option in new Xcode 7 for the way to just build an apple watch app without the companion app. Please let me know the way to do so, if it exists.
In fact, watchOS apps are linked to the iOS companion app.
In both versions, each watchOS app has two targets: WatchKit App - which contains storyboard, icons, etc - and WatchKit Extension, which contains the code.
In watchOS 1 (old WatchKit) + iOS 8 combination, WatchKit App is installed on Apple Watch, and both WatchKit Extension and iOS app are installed on user's iPhone.
In other way, in watchOS 2 + iOS 9 combination, both WatchKit App and WatchKit Extension are installed on user's Apple Watch, and iOS App is the only code and view installed on iPhone.
Running iOS app only requires one target, so it can run without Apple Watch (since the first iPhone OS).
But running watchOS apps needs both WatchKit App and WatchKit Extension. In watchOS 1 (the old WatchKit) because one is on iPhone and other on Apple Watch, so running apps need connecting between them with Bluetooth, so you have to carry your iPhone nearby. In watchOS 2, both are on Apple Watch, so running apps does not need connecting to iPhone and having iPhone nearby. You can connect them with WatchConnectivity framework included in watchOS 2 SDK (in Xcode 7).
So in watchOS 2 apps can run standalone, but they are not standalone at all. They install on Apple Watch when installing on iPhone, and they can connect with each other. When you create a new watchOS App, you must include it with your iPhone app when submitting it to the iTunes Connect.
NOTE: This makes watchOS apps faster and more reliable.
You can't build native watch apps yet. Maybe in a later build of Xcode 7, but so far all Apple has announced is that "at some point in the near future" you will be able to build a native Watch app.
In watchOS 2, watch apps are "native" in the sense that the WatchKit extension now runs on the Apple Watch instead of the user's iPhone. This means it can no longer share data with the iPhone app using shared app groups but must instead use the new Watch Connectivity framework to pass data back and forth. It also means that watch apps can be much faster and much more capable without the phone being present.
However, a watch app must still be bundled with a companion iOS app, at least for this year.
In iOS 13 it's possible to create independent watchOS application. More information can be found here. You have to download Xcode 11 to be able to create independent watchOS application.
To create it simply go to File -> New -> Project and select watchOS -> App as it's shown on a screenshot:

How to send msg between iPhone app and WatchKit app?

I am newer one for Apple Watch development.
Can you please provide me about how to communicate from iPhone app to Watch app and from Watch app to iPhone app ?
As I can see, there is a notification controller which is related Apple Push notification. I would like to connect to Watch app directly via Bluetooth or something. For example, As soon as iPhone app sends a MGS to Watch app, the watch app shows this MSG without any delay.
Thank you.
In order to communicate with the iPhone app from your Watch extension you can use openParentApplication(_:reply:) since Beta 2.
For the other way around I have been putting a file into the shared app group folder and monitor it from the Watch extension. If the iPhone app modifies the file the Watch extension will be notified and can act on it.