Pass text from iOS label to WatchOS label - swift- - ios

I've a problem, with my app.. I try to follow some guide like Passing data to Apple Watch app , but I'm not sure it will fit for my case.
I've some label with text on my iOS app, then I want to show this text on my watchOS app label, and I don't know which is the best way to pass this text and keep it synchronized with the iOS app..
Thanks a lot for your help!

For watchOS1, Since Watch App is included as an extension in your Host App. So, you can use App Group to share data between your Host App and your App Extension.
Refer to https://stackoverflow.com/a/44654185/5716829 for more on using App Groups.

Since watchOS2, you don't have any built in function for communicating between the iOS and watchOS app than the WatchConnectivity framework.
From the information provided in your question, the updateApplicationContext(_:) function seems to be the best solution for your problem. You can send a dictionary of data with this function and the system tries to make sure that the data is received by the time your app is displayed to the user. If the function is called several times before the app would be visible to the user (run in the foreground), the system overwrites the previous data, so the Watch app only receives the most recent data to display.

The right method for this is WCSession.updateApplicationContext(_ applicationContext: [String : Any]) https://developer.apple.com/documentation/watchconnectivity/wcsession/1615621-updateapplicationcontext "Sends a dictionary of values that a paired and active device can use to synchronize its state."

Related

iOS - How to pass string from main app to widget without using app group

As title, have any idea let widget get main app data?
As I know using App group to save & load the same suite userdefault.
Does have another way to achieve this feature?
Thanks.
AFAIK as you just mentioned the App groups is the way to go when sharing content between your main App and your widget.
Apple is really restrict about user data safety, and don't think there is there any other way to share content without using the app group as we can see on their documentation available here (Sharing Data with Your Containing App)
Something that would try is using a local notification I think this might work to trigger a local notification to your extension and handle the string on the other side :)
More details available here

Retrieve user defaults information from an Apple Watch

I have an iPhone and an Apple Watch paired together, and there are some values stored in the user defaults of the phone. I want my Apple Watch to be able to retrieve that stored information somehow and bring it back to the Watch. What is the best method of achieving this with Swift?
Since watchOS2, you don't have any built in function for communicating between the iOS and watchOS app other than the WatchConnectivity framework. Due to the fact that Watch apps are no longer considered App Extensions, they don't have access to AppGroups and hence to UserDefaults on the iPhone.
For syncing UserDefaults, the updateApplicationContext(_:) function seems to be the best solution. You can send a dictionary of data with this function (the data you just saved to UserDefaults on the iPhone) and the system tries to make sure that the data is received by the time your app is displayed to the user. If the function is called several times before the app would be visible to the user (run in the foreground), the system overwrites the previous data, so the Watch app only receives the most recent data to display.

Multiple WatchKit Apps For 1 iOS App

According to the Apple Watch Programming Guide, developers are only allowed to include 1 glance in their AppleWatch app. This creates a bit of a challenge for me since my app is an aggregate of different types of data so I'm not sure which data set to include in the glance.
That being said, could I get around this by creating multiple AppleWatch apps for my iOS app which would then allow me to create a single glance for each AppleWatch app?
No. Confirmed one Watch app per iOS app.
There is no support for multiple WatchKit apps per iPhone app at this
time.
- Apple's WatchKit Evangelist
You can implement multiple Glance views depending on the user's context (time and location) using WKInterfaceGroups and hiding/unhiding when necessary.
See my answer to the exact same question here https://stackoverflow.com/a/28214768/3588917
Will a timer work for you? For example, when a certain time elapsed, you can change the UI of the glance to show a different type of data? When a user taps a glance, you will be able to tell what data is showing in the glance at the time and pass relevant contextual data and redirect them to an appropriate interface controller?
"When the user taps a glance, Apple Watch launches the corresponding WatchKit app. Normally, launching the app displays its main interface controller. If you want to display a different interface controller at launch time, call the updateUserActivity:userInfo: method from your glance interface controller and use it to provide contextual information about what the glance is doing. At launch time, your app can use that contextual data to display a different interface controller."

UIApplication.sharedApplication().beginIgnoringInteractionEvents() on Apple Watch

I can not use
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
in WatchKit Extension, I have error:
'sharedApplication()' is unavailable: Use view controller based solutions where appropriate instead.
Is there some alternative ?
The short answer is: No there isn't.
The long answer:
Please keep in mind that the extension is not executed on the watch but on your phone. So if you would call UIApplication.sharedApplication() it would return you the application of the extension on your phone, anyway! Everything you do inside your extension is stuff that manipulates the extension on your phone. The only exception from this are the WatchKit methods. And even they are basically calls that are converted into instructions that are send over bluetooth to tell the watch what to do. At no time you can write code that executes on the watch!
You have no control what so ever about what the watch does with the instructions you send to it. You are basically acting as a server talking to a client and you have no control over the client. You should send as little instructions as possible and once you send them, your task is done, the rest is up to the watch.
That being said, you should carefully plan your UI in a way that you do not need any calls that manipulate the event delivery. You should focus on simple 'if user taps x I do y' interaction.
Another thing to keep in mind is, that your extension can not communicate with your main iOS app. You can create a shared app group between your iOS app and your watch extension to share data between them, however you can not directly communicate with your app. If you want to use parts of your apps logic, extract the module in question into a framework (this has become very easy with Xcode 6) and use the framework in both, your app and your extension.

Can I connect single MFI accessory with two apps at a time

I have two apps with same UISupportedExternalAccessoryProtocols. These are communicating with the MFI certified device. How I can know that the session is already created with other app on the same device. So I can alert to the user that you can't create multiple sessions with the same accessory at a time.
Please help to fix this.
Thanks in advance.
I personally consider AyBayBay's answer invalid, since you tagged your question with the "iOS tag" and NSDistributedNotificationCenter is not supported on this platform. (There is a low level api to do this, but I think it's in a private framework)
If you are pretending to target your app to iOS8+, the simplest way to do it is using iOS8's AppExtensions feature. I made some tests creating a session within my app, and sending info to the device from another app using extensions the way below.
some app -> app extension -> my app connected with ext.device -> ext.device
so I'm pretty sure you can check if the session is opened or not and return this info to the caller app.
FYI: To pass info to another app you can also use URLSchemes or UIPasteboard too, but both behaviours in a different way than AppExtensions and I think it not fits on your case. Maybe it helps someone else...
Well when one app connects and establishes a session with your MFI device, you can post a notification to NSDistributedNotificationCenter. Your other app can subscribe and listen for this message, upon recieving it and processing it in its runLoop (when it comes back into the foreground) you can have code that blocks it off from creating a session at the same time. You will have to experiment with this a bit but it can be done.

Resources