Cloudkit Storage from Watch

Hi,


I am a newby but have managed to write and my Apps on the Apple Store, however I am still learning and being amost 60, i find it hard.


Wondering if any body know if the Watckit will allow me to store data directly to iCloud via Cloudkit.


I have written an iOS app and WatchOs and they are working great, I can store and retrieve basic data from the IOS (iPhone), but would like to be able to at least retrieve the same icloud data that the phone is using from the cloud.


Can the watch app store and retrive the data from the same icloud database the phone is using?


Hope that makes sense.


Cheers.



Craig.

Accepted Reply

According to:


https://developer.apple.com/library/archive/documentation/General/Conceptual/WatchKitProgrammingGuide/SharingData.html

iCloud interactions. Starting with watchOS 3, the WatchKit extension can communicate directly with CloudKit and other iCloud technologies.

Your code is written in the Watchkit extension so the answer is 'yes'.


I too started iOS at 60, 10 years ago. It's a great way to enter retirement.


Replies

According to:


https://developer.apple.com/library/archive/documentation/General/Conceptual/WatchKitProgrammingGuide/SharingData.html

iCloud interactions. Starting with watchOS 3, the WatchKit extension can communicate directly with CloudKit and other iCloud technologies.

Your code is written in the Watchkit extension so the answer is 'yes'.


I too started iOS at 60, 10 years ago. It's a great way to enter retirement.


Thank you.


So I assume you are now in your 70's or near it... Well done.


I have no formal training and are self taught.


I am having trouble just starting on getting the data from icloud which the IOS App stores.


Do you know or any demos or sample code that would help me kick start things in wrriting the section in the Watchkit Extension ?


The iOS section is connected to a Private Database that I would like the Watch section of the app to be able to connect to and populate a Tableview on the watch.


Any ideas please !


Cheers.


Craig.

Yup, 70.


I know of no demos. I started with SAMS 24 hours to program iPhone and went from there.


I would not try to connect the watch directly with iCloud. I would just connect the watch to the iPhone and let the iPhone do the iCloud connections. Here is some code to connect watch and iPhone:


//on the watch:


-(void)activateASession{
    if(!wcsession){
        wcsession = [WCSession defaultSession];
        wcsession.delegate=self;
    }
    if([wcsession activationState]!=WCSessionActivationStateActivated)
        [wcsession activateSession];
}

-(void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(nullable NSError *)error{
    //   NSLog(@"activation did complete with state %i",activationState);
}

-(void)session:(WCSession *)session didReceiveApplicationContext:userInfo{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
        if([userInfo objectForKey:@"whateverOnPhone"])
            [defaults setObject:[userInfo objectForKey:@"whateverOnPhone"] forKey:@"whateverOnPhone"];    
//  unload other things in userInfo
    });
    
}








//on the Phone
-(void)activateASession{
    if ([WCSession isSupported]) {
        theSession = [WCSession defaultSession];
        theSession.delegate = self;
        if([theSession activationState]!=WCSessionActivationStateActivated){
            [theSession activateSession];
        }
    }
}

//NEED TO COMPLETE THESE.........
-(void)sessionDidDeactivate:(WCSession *)session{
    [self activateASession];
}

-(void)sessionDidBecomeInactive:(WCSession *)session{
    
}

-(void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(nullable NSError *)error{
    
}

-(void)sendInfo{
    if ([WCSession isSupported]){
        NSMutableDictionary *theInfo=[[NSMutableDictionary alloc] initWithCapacity:3];
        [theInfo setObject:@"this is any object you want to send" forKey:@"whateverOnPhone"];
        [theInfo setObject:@"some other object" forKey:@"some other key"];
        [session updateApplicationContext:theInfo error:nil];
    }
}

Hi,


Well done... you are way above what am..


Being a simple motorcycle mechanic, I do not have an IT background.


That code you gave me is way above what I can do. I would not know how to implement it.


I was hoping to simply use similar code that is on the IOS App and duplicate it on the Watch App.


For example, my IOS code.

import UIKit

import CloudKit


class ListViewController: UIViewController {


// Setup Database from iCloud

let database = CKContainer.default().privateCloudDatabase

var records = [CKRecord]()


then....


// Get Database from iCloud

@objc func queryDatabase() {

let query = CKQuery(recordType: "FuelBuddy", predicate: NSPredicate(value: true))

database.perform(query, inZoneWith: nil) { (records, error) in


let sortedRecords = records!.sorted(by: { $0.creationDate! > $1.creationDate! })

self.records = sortedRecords

DispatchQueue.main.async {

// Check and report if an error

if error != nil {

self.alert()


} else {

//self.records = records!

self.tableView.reloadData()

self.tableView.refreshControl?.endRefreshing()

}}}}}


But as soon as I add this code to the Watch App, it crashes.

// Setup Database from iCloud

let database = CKContainer.default().privateCloudDatabase

var records = [CKRecord]()


Man I am lost..

Jusyt cannot find any samples.


Cheers.


Craig.

Thanks I have managed to get my Watch app storing to the cloud.. Thanks again Craig