How do I pass In-App Purchase data to Watch?

91 views Asked by At

I have part of my Apple Watch app behind a paywall, but I am unable to extract information from the WCSession transferUserInfo session.

Also, how do I store that data on the Watch app as no NSUserDefaults are available on the Watch?

Here is my code, that does open the session, but does not pass data correctly:

ViewController

//This creates the dictionary that will be passed to the Watch
var WatchInAppPurchases = ["product1":Bool(), "product2":Bool(),]    
func dictionaryCreator() {
        if (UserDefaults.standard.value(forKey: "product1purchased") != nil) == true {
            WatchInAppPurchases["product1"] = true
        } else {
            WatchInAppPurchases["product1"] = false
        }
        if (UserDefaults.standard.value(forKey: "product2purchased") != nil) == true {
            WatchInAppPurchases["product2"] = true
        } else {
            WatchInAppPurchases["product2"] = false
        }
    }


//This runs both with the .purchased and .restored function.
    func syncronizeWatch() {
        if WCSession.isSupported() {
            let session = WCSession.default
            session.delegate = self
            session.activate()

            session.transferUserInfo(WatchInAppPurchases)
        }
    }

Interface controller:

   func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        NSLog("%@", "activationDidCompleteWith activationState:\(activationState) error:\(String(describing: error))")
    }
    
    var userinfo0 : Bool?
    var userinfo1 : Bool?
    
    
    func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
        let userinfodic = userInfo 
        userinfo0 = userinfodic["product"]!
        userinfo1 = userinfodic["product2"]!
    }

After this runs, how am I supped store this data correctly?

if userInfo0 {
//show paid content?
}
0

There are 0 answers