Search code examples
swifticloud

CKSubscriptions and production container


Hi I'm trying to create a CKSubscription, I use this code:

func setupCKSubscriptions(){

    if NSUserDefaults.standardUserDefaults().boolForKey("sub") == false{

    let subscription = CKSubscription(recordType: "Quadri", predicate: NSPredicate(value: true), options: .FiresOnRecordCreation)

    let notificationInfo = CKNotificationInfo()
    notificationInfo.alertLocalizationKey = NSLocalizedString("NEW_Q", comment: "")
    notificationInfo.shouldBadge = true

    subscription.notificationInfo = notificationInfo

    CKContainer.defaultContainer().publicCloudDatabase.saveSubscription(subscription) { (subscription, errore) -> Void in

        if errore == nil{

            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "sub")
            NSUserDefaults.standardUserDefaults().synchronize()
            let alert = UIAlertController(title: "Ok", message: "", preferredStyle: .Alert)
            self.presentViewController(alert, animated: true, completion: nil)

        }else{

            print(errore?.localizedDescription)
            let alert = UIAlertController(title: "Errore", message: errore?.localizedDescription, preferredStyle: .Alert)
            self.presentViewController(alert, animated: true, completion: nil)
        }
    }
    }else{

        let alert = UIAlertController(title: "Errore", message: "", preferredStyle: .Alert)
        self.presentViewController(alert, animated: true, completion: nil)
    }
}

The problem is that this code only works on the simulator, when I run the app on a real device I get this error:

attempting to create a subscription in a production container.

How can I fix this?


Solution

  • I had exactly the same problem and the solution is easy, BUT NOT VERY INTUITIVE. In development mode let the app create the subscription(s) you need. Deploy database to production. Test your app in production and you'll see it can now create the subscription you have made. Hope it works