WatchKit didReceiveApplicationContext not being called - swift

I can't get didReceiveApplicationContext to be called. Any ideas?
InterfaceController:
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController, WCSessionDelegate {
#IBOutlet var colorLabel: WKInterfaceLabel!
private let session: WCSession? = WCSession.isSupported() ? WCSession.defaultSession() : nil
override init() {
super.init()
session?.delegate = self
session?.activateSession()
}
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
}
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]){
let colors : String = applicationContext["color"] as! String
colorLabel.setText(colors)
NSLog("session did receive application context")
}
}
I've been following along with this tutorial: http://www.kristinathai.com/watchos-2-how-to-communicate-between-devices-using-watch-connectivity/
No NSLog or setting of the colorLabel happens. No idea what I'm missing. Thanks!

This seems to be a typical 'development phase' problem!
The WCSession.defaultSession.applicationContext is buffered on the iOS device, so it is only transferred to the watch (extension) once if it doesn't change.
This lead to the strange finding, that watch extensions 'didReveiveApplicationContext:' seems not to be called, when WCSession.defaultSession.updateApplicationContext is called in the iOS app again. (Try to call WSSession.defaultSession.receivedApplicationContext in the extension to find, that the earlier transferred context is in fact available)!
In test situations, it is very helpful to add a 'changer' object to the context dictionary (like an UUID object, or - maybe even better - a NSDate.date). This will ensure, that the context has changed (compared to the buffered one) and gets transferred again (leading to a call to didReceiveApplicationContext) :-)
NSError* error = nil;
[WCSession.defaultSession updateApplicationContext:#{ #"yourKey" : #"your content",
#"forceTransfer" : NSDate.date }
error:&error];
And: Don't forget to remove this in the production version of your app, as - of course - this leads to unneeded data transfer between your app and your watch extension!
PS: The checked answer solves this problem by creating a new app. And flushing all buffers this way...

I had the same problem. In my case it helped to just close both simulators and then run the Watch scheme. This opens up both simulators again in connected state.
Hoping it helps!

I copied the above code into a new watch app and it works fine. The error must lay on the sending side. Are you certain the code in the iOS app is being called? I presume you are using Xcode and two simulators, one for the iOS app and one for the WatchApp.
The code on the iOS side is not run unless you open the app on the phone simulator. Where and how on the iOS side are you issuing the updateAppContext call?
In my test, this is all that I added to my ViewController.swift on the iOS side (This code will not be triggered until I start the iOS app on my iPhone.)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
do {
try session.updateApplicationContext( ["color" : "Red" ])
} catch _ {
}
}

For me, when doing some testing/debugging i tried to fire updateApplicationContext in AppDelegate. This caused the didReceiveApplicationContext not being called.
Moving this logic to a later point, like in a UIViewController made it work for me at least.

Check the session to make sure its isPaired and watchAppInstalled properties are both YES. It seems like updating the shared context while these are NO will not work.
I was having this issue. Made a change to not update the context when either condition was NO. Added an implementation of sessionWatchStateDidChange:, and if both conditions were YES, updated the context. It worked.
I suspect this in combination with another issue where the phone will not send the context if the data is not different causes the "never updating" issue. A workaround of passing a "uuid" did help but I suspect the above is a better fix.

In my case, I used the following code to send my application context:
do {
try session.updateApplicationContext(applicationContext)
} catch let error {
throw error
}
and neither didReceiveApplicationContext was called, nor an error was thrown.
My problem was that applicationContext contained a custom object, whereas only property list items are allowed.
The strange point is that no error was thrown.

Related

Swift Command Line Tool Not Receiving DistributedNotificationCenter Notifications

I am trying to create a very basic Swift command-line application that signals to another application using a WebSocket when the macOS UI changes to/from light/dark mode.
For some reason, the command-line tool is not receiving any notifications from DistributedNotificationCenter, in particular, AppleInterfaceThemeChangedNotification. However, running the exact same code in a Cocoa UI app on applicationDidFinishLaunching works perfectly fine.
I found an old Obj-C CLI project on Github that is meant to print out every notification, but that doesn't do anything either. It makes me suspect Apple perhaps changed something, but I cannot seem to find anything online about it. Are there certain Xcode project settings I need to set?
// main.swift
import Foundation
class DarkModeObserver {
func observe() {
print("Observing")
DistributedNotificationCenter.default.addObserver(
forName: Notification.Name("AppleInterfaceThemeChangedNotification"),
object: nil,
queue: nil,
using: self.interfaceModeChanged(notification:)
)
}
func interfaceModeChanged(notification: Notification) {
print("Notification", notification)
}
}
let observer = DarkModeObserver.init()
observer.observe()
RunLoop.main.run()
I managed to get iTunes notifications working, so it was just the theme change notifications that weren't working. Given this, I suspect Apple only sends the notifications to UI/NSApplication applications. As such, replacing the last 3 lines from above with the following works:
let app = NSApplication.shared
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
let observer = DarkModeObserver.init()
observer.observe()
}
}
let delegate = AppDelegate()
app.delegate = delegate
app.run()

How to make a Switch keep its state after switching viewControllers in swift 4?

So I have this mini school project where in the beginning I have an off switch, the idea is to turn it on and be able to move to other viewControllers but when i get back the switch is still on, the thing is that when opening the app after closing it, it must be turned off. Ive seen other similar questions but none ask on how to maintain the off state after closing the app.
override func viewDidLoad() {
super.viewDidLoad()
switchOutlet.isOn = UserDefaults.standard.bool(forKey: "isOnSwitch")
{
#IBAction func switchAction(_ sender: Any) {
let isOnSwitch = UserDefaults.standard.bool(forKey: "isOnSwitch")
if isOnSwitch == true {
UserDefaults.standard.set(false, forKey: "isOnSwitch")
} else { UserDefaults.standard.set(true, forKey: "isOnSwitch")}
This code only works to make sure the switch is On at all times, even when entering the app for the first time.
Your logic is right. However, viewDidLoad will be called only once when the view controller is loaded. If you go background and open again, viewDidLoad will not be called again.
There are some function in UIApplicationDelegate you can use, like applicationWillEnterForeground, applicationWillTerminate, applicationDidBecomeActive, applicationWillResignActive, applicationDidEnterBackground... choose one that fits your scenario.
Or you can just add observer listening to UIApplicationWillEnterForeground in your viewcontroller, and update your switch whenever it is called.

When to unregister KVO observation of Operation isFinished

In this simple code (Xcode 8.3), I create an Operation subclass instance, register for KVO observation of its isFinished property, and launch the operation by adding it to my queue:
class MyOperation : Operation {
override func main() {
print("starting")
print("finishing")
}
}
class ViewController: UIViewController {
let q = OperationQueue()
override func viewDidLoad() {
super.viewDidLoad()
let op = MyOperation()
op.addObserver(self, forKeyPath: #keyPath(MyOperation.isFinished), options: [], context: nil)
self.q.addOperation(op)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
print("Observed \(keyPath)")
if let op = object as? Operation {
op.removeObserver(self, forKeyPath: #keyPath(MyOperation.isFinished))
}
}
}
As you can see, I have an implementation of observeValue(forKeyPath..., of course, and my plan was to call removeObserver(forKeyPath... there.
The problem is that my app crashes with "MyOperation was deallocated while key value observers were still registered with it". We print "starting" and "finishing" but we never print "Observed"; the operation goes out of existence before I get my KVO notification.
This seems like a catch-22. If I can't remove the observer by observing isFinished, when am I supposed to do it? [I can work around this issue by adding to MyOperation my own KVO-observable property that I set at the end of main. But the notion that I should have to do this is very odd; isn't this exactly why isFinished is observable, so that I can do what I'm trying to here?]
After testing the exact same given code snippet on Xcode 8.2, it worked as it should, the console shows:
starting
finishing
Observed Optional("isFinished")
It seems that the reason of the issue is testing it on Xcode 8.3, probably it is a bug -or it might be a new behavior-. However, I would suggest to report it as a bug.
Apple changed #keyPath behavior in Swift 3.1 (source). Currently #keyPath(isFinished) returns "finished", it used to return "isFinished", and that was a bug. Here is the explanation as it can easily get confusing when using KVO and Operation class.
When you register an object for KVO notifications
textView.addObserver(self,
forKeyPath: #keyPath(UITextView.isEditable),
options: [.new, .old],
context: nil)
Foundation provides it (textView) with new setter implementation that calls willChangeValue(forKey:) and didChangeValue(forKey:) (this is done via isa-swizzling). The key that is passed to those methods is not getter (isEditable) not setter(setEditable:) but property name (editable).
#property(nonatomic,getter=isEditable) BOOL editable
This is why it is expected to receive editable from #keyPath(UITextView.isEditable).
Although Operation class has properties defined ad follows
#property (readonly, getter=isExecuting) BOOL executing;
#property (readonly, getter=isFinished) BOOL finished;
It expects to observe notifications for isFinished and isExecuting keys.
Upon completion or cancellation of its task, your concurrent operation object must generate KVO notifications for both the isExecuting and isFinished key paths to mark the final change of state for your operation
This forces us to use literal strings when posting those notifications.
IMHO this is a mistake made years ago which is really hard to recover from without breaking existing code.

Loading Data from Bundled Realm Database in Swift

I’m relatively new to Realm. My task is to bundle a RealmDB and make it writable. Thus far I have copied the bundled realm file into the project and implemented the following code in the app delegate. Above the "func application(application: UIApplication, didFinishLaunchingWithOptions” I used the following function:
func bundleURL(name: String) -> NSURL? {
return NSBundle.mainBundle().URLForResource("data", withExtension: "realm") }
And below didFinishLaunchingWithOptions, I used the following:
if let v0URL = bundleURL("data.realm") {
do {
try NSFileManager.defaultManager().removeItemAtURL(defaultURL)
try NSFileManager.defaultManager().copyItemAtURL(v0URL, toURL: defaultURL)
} catch {}
The issue is that I have to load the app twice to get the data to show up in a MapViewController, which is the first controller upon launch. In this case, I want map pins in the MapViewController to automatically appear upon build. I tried to implement a notification in the MapViewController using the following:
let results = try! Realm().objects(Spaces)
notificationToken = results.addNotificationBlock {[weak self](changes: RealmCollectionChange<Results<Sapces>>) in
self!.populateMap()
I also tried to implement a Database Manager:
func getDBItems() -> [Spaces] {
let dbItemsFromRealm = try! Realm().objects(Spaces)
var bathroom = [Spaces]()
if dbItemsFromRealm.count > 0 {
for dbItemsInRealm in dbItemsFromRealm {
let spaces = dbItemsInRealm as Spaces
space.append(space)
}
}
return space
}
}
However, I can’t get the pins to load upon launch. Any help would be much appreciated.
The behavior you describe is what I'd expect to see if you've already opened the Realm at the target path prior to copying the bundled Realm over to that location. You can confirm this by putting a breakpoint on the Realm initializer and on your code that calls removeItemAtURL and seeing which is hit first.

Pass saved Workout from Watch to iPhone

On Watch, I'm able to pass a saved workout from the WorkoutInterfaceController to the SummaryInterfaceController. But I was wondering how to pass the saved workout from the Watch to the iPhone (so I can display it in a Summary View Controller too).
Do you know? Or is there a better way I'm supposed to do this?
Here's what I use to pass the saved workout from WorkoutInterfaceController to the SummaryInterfaceController:
private func saveWorkout() {
// Create and save a workout sample
let configuration = workoutSession!.workoutConfiguration
let isIndoor = (configuration.locationType == .indoor) as NSNumber
print("locationType: \(configuration)")
let workout = HKWorkout(activityType: configuration.activityType,
start: workoutStartDate!,
end: workoutEndDate!,
workoutEvents: workoutEvents,
totalEnergyBurned: totalEnergyBurned,
totalDistance: totalDistance,
metadata: [HKMetadataKeyIndoorWorkout:isIndoor]);
healthStore.save(workout) { success, _ in
if success {
self.addSamples(toWorkout: workout)
}
}
WKInterfaceController.reloadRootControllers(withNames: ["SummaryInterfaceController"], contexts: [workout])
}
private func addSamples(toWorkout workout: HKWorkout) {
// Create energy and distance samples
let totalEnergyBurnedSample = HKQuantitySample(type: HKQuantityType.activeEnergyBurned(),
quantity: totalEnergyBurned,
start: workoutStartDate!,
end: workoutEndDate!)
// Add samples to workout
healthStore.add([totalEnergyBurnedSample], to: workout) { (success: Bool, error: Error?) in
if success {
// Samples have been added
}
}
}
Let me know if any questions or information needed, thanks!
As a part of my research and development,I discovered how the pairing of the iPhone and the Apple Watch has the potential to be useful.
In this case, tapping on a button on the Watch app will send text on the iPhone.
To make a simple demo of this functionality, place a button on the WatchKit interface and a label on the iOS app’s storyboard.
Now, hook up the button to the WatchKit Interface Controller as an IBAction in order to respond to button tap events. Also hook up the Label to the UI View Controller as an IBOutlet.
In the Interface Controller, we make up a string variable to send to the label and in the button’s IBAction method, make a dictionary that includes the string variable you made. This dictionary is what is passed to the iPhone app.
class InterfaceController: WKInterfaceController {
Var str: String = "Hello Phone"
#IBAction func button() {
let dict: Dictionary = ["message": str]
}
Use the following method to send the dictionary to the iPhone.
WKInterfaceController.openParentApplication(dict, reply: {(reply, error) -> void in
print("Reply receive from iPhone app")
})
In the AppDelegate of the iOS app, add the following application method. This is what will handle the previous methods communication from the Watch. Also we can use a notification to notify a view controller that data has been received and to pass it along.
func application(application: UIApplication, handleWatchkitExtensionRequest userInfo: [NSObject : AnyObject]?, reply:(([NSObject : AnyObject]!) {
NSNotificationCenter.defaultCenter().postNotificationName("WatchKitReq", object: userInfo)
}
Finally in the view controller, make a listener for the notification that will update the label’s text to the string that was sent with the data.
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector("handleWatchKitNotification:"), name: "WatchKitReq", object: nil)
}
func handleWatchKitNotification(notification: NSNotification) {
if let userInfo = notification.object as? [String : String] {
label.text = userInfo["message"]
}
}
Hope this will help you to understand. For more concerns you can look on this,
Delegate Method
To do this you have to create an App Group which is essentially a space which both apps can use. It was brought in with the exetension framework in iOS8 so apps can communicate with their Today widgets, or custom keyboards, and amongst other applications.
ADD CAPABILITIES
The first thing we have to do is add the app group capability to both our iPhone and Watch Watch Extension targets.
To do this open up the project settings (blue icon at the top of the list of files) and select the iPhone target. You will need to select the “capabilities” tab at the top of the page and scroll down to turn on app groups.
This requires a connected developer profile, and will take a while to enable. You’ll need to do the same steps to switch on app groups for the watch kit extension also.
Next you need to ensure that the app group string is an identifier string you want and that makes sense for your app, it must start with the word group or it complains. You can also add multiple groups if you wish. Whatever you pick they must be enabled with a blue tick (again this might take a while) and are exactly the same for both the iPhone and Watch extension targets!
To use App Groups, it’s not that different or difficult to use than NSUserDefaults:
var userDefaults = NSUserDefaults(suiteName: "group.com.example.My-App")
userDefaults.setObject(true, forKey: "isDarkModeEnabled")
userDefaults.synchronize()
The only differences here are how NSUserDefaults is instantiated and calling synchronize at the end. You feed it the container ID to the constructor parameter called “suiteName”, then call “synchronize()”, and your data flies to the cloud for other apps and devices to consume.
Taking It to the Next Level
You can take this one step further by creating a class for your app and abstract the underlying storage for your properties. Here’s an example:
public class ConfigurationModel: NSObject {
public static let storageKey = "group.com.example.My-App"
public let userStorage = NSUserDefaults(suiteName: storageKey)
public var isDarkModeEnabled: Bool {
 get {
// Get setting from storage or default
if userStorage?.objectForKey("isDarkModeEnabled") == nil {
userStorage?.setObject(false, forKey: "isDarkModeEnabled")
userStorage?.synchronize()
}
return userStorage?.objectForKey("isDarkModeEnabled")
}
set {
// Set new value in storage
userStorage?.setObject(newValue, forKey: "isDarkModeEnabled")
userStorage?.synchronize()
}
}
At the top of the class, I am declaring my group container ID and creating the NSUserDefault object out of it. Then my properties for the class have getters and setters to store the data to the App Group. If the key doesn’t exist, it creates it with a default value and synchronizes it. Using the class from this point forward is simple:
var configModel = ConfigurationModel()
configModel.isDarkModeEnabled = true
This property is stored in the cloud! Everything is abstracted away for you. You don’t have to be bothered about storing and synchronizing it into the App Group. It’s all done automatically for you in the getters and setters!
Hope, this will help you to understand how you can share data between the iPhone and Apple Watch app.