To transfer files between your iPhone and your Apple Watch, you must use Watch Connectivity using WCSession to properly process the message. You can send UIImage as NSData using the didReceiveMessageData delegation didReceiveMessageData for WCSessionDelegate .
The first thing you need to know is to convert UIImage to NSData and vice versa. You can use the following code for this:
If PNG images
let image = UIImage(named: "nameOfYourImage.jpg") let data = UIImagePNGRepresentation(image)
If jpg images
let image = UIImage(named: "nameOfYourImage.jpg") let data = UIImageJPEGRepresentation(image, 1.0)
Then you can use WCSession to send the message as follows:
ViewController.swift
class ViewController: UIViewController, WCSessionDelegate { override func viewDidLoad() { super.viewDidLoad()
InterfaceController.swift
import WatchKit import Foundation import WatchConnectivity class InterfaceController: WKInterfaceController, WCSessionDelegate { override func awakeWithContext(context: AnyObject?) { super.awakeWithContext(context)
In the above code, when you open the ViewController it sends a UIImage , the above example is for educational purposes only, you should handle it in a more proper way in relation to the complexity of your project.
Hope this helps you.
Victor sigler
source share