Search code examples
iosobjective-cwatchkitwatchos-2watchconnectivity

Send an NSObject by watchConnectivity


I am working on a WatchOS 2 version of our app and stuck on the fact that i can only send a string with the function "updateApplicationContext:(NSDictionary *)".

I hoped I could make an NSObject with some variables to send over as object in this NSDictionary. After a few days I still didn't find a solutions for this problem. Is there anyway to send a NSObject over the function?

If there is no way to send an NSObject is it possible to send something like a struct?

My code is a s follows:

-(void)updateWatchData
{
    //Objective-C
    if ([WCSession isSupported]) {
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];

        if(session.isPaired) {
            //Objective-C
            DLog("Sending watch data");
            NSMutableArray *kalenderData = [[NSMutableArray alloc] init];

            for (int i = 0; i < self.dataContainerViews.count; i++) {
                DataContainerView *container = self.dataContainerViews[i];

                if(container.listType == ListGPKalender){
                    for(int j = 0; j < container.tableArray.count; j++){
                        GrandPrix *gp = container.tableArray[j];
                        WatchGrandPrix *watchGp = [[WatchGrandPrix alloc] init];

                        watchGp.gpnaam = gp.gpnaam;

                        [kalenderData addObject:watchGp];
                    }
                }
            }

            NSArray *keys = [NSArray arrayWithObjects:@"kalenderData", nil];
            NSArray *objects = [NSArray arrayWithObjects:kalenderData, nil];
            NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

            NSError *anyError;
            if([[WCSession defaultSession] updateApplicationContext:dictionary error:&anyError]){
                DLog("Data Send!");
            }else{
                DLog("Failed to send data with error: %@", anyError);
            }
        }
    }
}

And the NSObject is just:

@interface WatchGrandPrix : NSObject

@property(nonatomic,strong) NSString *gpnaam;

@end

The error i got was

Failed to send data with error: Error Domain=WCErrorDomain Code=7010 "Payload contains unsupported type." UserInfo={NSLocalizedDescription=Payload contains unsupported type., NSLocalizedRecoverySuggestion=Only pass valid types.}

Thanks!


Solution

  • You could convert the object to a property list representation (basically instead of an array of your model object you have an array of dictionaries, where each dictionary is a mapping of values where the key is the name of the variable and the value is a plist compatible type representing the value).

    Another option, which will be less bandwidth efficient, is to make your object NSSecureCoding compliant and use something like NSKeyedArchiver to convert it to NSData and send an array of NSData's.