AppleWatch - "attempting to insert null" when calling WKInterfaceDevice addCachedImage
Calling WKInterfaceDevice addCachedImage (_: name :) to send an image from my iPhone app to the Apple Watch (where the extension can report an image to show it). The exception is the following:
2015-06-09 20:47:57.079 TimeInterval[20195:5186462] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[3]'
Various google and StackOverflow searches show that this has to do with using a shortcut to create an NSDictionary that does not allow traversal nil
. However, my code doesn't make a dictionary at all. Also, when the debugger breaks (breakpoint on exceptions), I check that the UIImage and NSString name I am passing is not exactly null.
Has anyone else seen this? Any idea why this is happening or how to fix it? Has anyone really used addCachedImage successfully? (Considering how the new AppleWatch is, who knows!)
My piece of code, if it helps:
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
if let info = userInfo {
NSLog("watch kit request: %@", info)
if let element = info["element"] as? String {
//Request to render an element
if element == "timer" {
let timerView = NPProgressLabel(size: CGSizeMake(48, 48))
timerView.text = info["value"] as! String
timerView.progressPercent = (info["progress"] as! NSNumber).floatValue
timerView.render()
let device = WKInterfaceDevice.currentDevice()
var success = false
if let image = timerView.currentImage {
success = device.addCachedImage(image, name: timerView.currentImageName()) // <------- crashing here ----------
} else {
NSLog("no image");
}
if !success {
NSLog("failed")
} else {
NSLog("addCachedImage success")
}
reply(["imageName": timerView.currentImageName()])
} else {
reply(["error": "Unknown element"])
}
return
}
}
reply(["error": "Bad request"])
}
source to share
The exact error I am getting may be Apple's error, but I think the answer to my question is that WKInterfaceDevice addCachedImage should not be called from an iPhone application, but rather from the WatchKit Extension. Between iPhone app and WatchKit Extension, I need to use a shared container to save, then load the image, then the extension can call addCachedImage.
source to share