For watchOS 2
and Xcode 7.3
, I am trying to send an image I have in the iPhone side to the watch side. In the phone side, I have this:
func sendImage() {
if WCSession.isSupported() {
let session = WCSession.defaultSession()
if session.watchAppInstalled {
do {
let image = UIImage(named: "myPic")
let imgData = NSKeyedArchiver.archivedDataWithRootObject(image!)
let dictionary = ["img": imgData]
try session.updateApplicationContext(dictionary)
} catch {
print("ERROR: \(error)")
}
}
}
}
Then, in the WatchKit Extension
side (ExtensionDelegate
file), I have:
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
let initialController = WKExtension.sharedExtension().rootInterfaceController as! InterfaceController
initialController.showImage(applicationContext["img"] as! NSData)
}
and in InterfaceController
:
func showImage(imageData: NSData) {
let image = UIImage(data: imageData, scale: 1.0)
self.myImage.setImage(image!)
}
where myImage
is a WKInterfaceImage
outlet. When showImage
method is called, imageData
is not nil
, but image
is when self.myImage.setImage(image!)
called. What am I doing wrong?