Search code examples
iosswiftfacebookwatchos-2wcsession

Creating Architecture for Sharing Photos with Apple Watch OS2


I'm trying to figure out the proper approach for sharing 10+ photos from an iOS app to an Apple Watch app using watchOS 2.

I want to transfer these images in the background so that the user doesn't have to open the iOS app in order to view the photos.

I've tried querying photos from Facebook and sending them to the watch via transferUserInfo() but the payload is too large:

FBSDKGraphRequest(graphPath: "me/photos?limit=2", parameters:["fields": "name, source"]).startWithCompletionHandler({ (connection, result, error) -> Void in
    if (error != nil){
        print(error.description)
    }
    else {
        var arr = [NSData]()

        for res in result["data"] as! NSArray {
            if let string = res["source"] as? String {
                if let url = NSURL(string: string) {
                    if let data = NSData(contentsOfURL: url){
                        arr.append(data)
                    }
                }
            }
        }
        print(arr)
        if arr.count > 0 {
            self.session.transferUserInfo(["image" : arr])
        }

    }
})

Any ideas how I should go about doing this?


Solution

  • The proper method is mentioned in the WCSession documentation:

    Use the transferFile:metadata: method to transfer files in the background. Use this method in cases where you want to send more than a dictionary of values. For example, use this method to send images or file-based documents.

    The images will be asynchronously delivered to the watch on a background thread. session:didReceiveFile: will be called when the watch successfully receives an image.

    Make sure to include (date) metadata with the image, and remove any existing images from the watch which are no longer a part of the ten most recent Facebook uploads.