Reputation: 944
I have two view controllers in my watch extension.Whenever I call
[[WCSession defaultSession] sendMessage:applicationData replyHandler:^(NSDictionary *reply) {}
I get response only for the first view controller, and error in my second viewcontroller
Error Domain=WCErrorDomain Code=7011 "Message reply failed."
UserInfo={NSUnderlyingError=0x79f1f100 {Error Domain=WCErrorDomain Code=7010 "Payload contains unsupported type."
UserInfo={NSLocalizedRecoverySuggestion=Only pass valid types., NSLocalizedDescription=Payload contains unsupported type.}}, NSLocalizedDescription=Message reply failed.}
WCSession is initiated in both app and watch extension.Any suggestions?
Upvotes: 6
Views: 4008
Reputation: 2055
In my case what happened before iOS 10 and XCode 8.0, in my watchkit app code i used:
let infoDictionary = ["request" : "word_detail", "word": self.word, "type": self.type]
and it works grest. When I tested the same code in WatchKit app 2.0 simulator by XCode 8.0, I found watchkit connectivity error showing with unsupported format message.
After lots of debugging I found the solution for it:
let infoDictionary:NSDictionary = ["request" : "word_detail", "word": self.word, "type": self.type]
I had to put just NSDictionary as a typecast and the whole thing works without problem.
Upvotes: 0
Reputation: 91891
In WCSessionDelegate's - session:didReceiveMessage:replyHandler:
method, the replyHandler
argument is defined as [String : AnyObject]
. The AnyObject
portion is misleading. It can only contain a property list data type: NSData
, NSString
, NSArray
, NSDictionary
, NSDate
, and NSNumber
. (Under these circumstances, it makes sense why AnyObject
was chosen, since those 6 data types do not inherit from a common subclass besides NSObject
.)
Usually people mention that NSCoding and NSKeyedArchiver can resolve the issue, but I haven't seen more examples/explanations beyond that.
The thing to note is that the replyHandler
dictionary doesn't care about serialization. You could use NSKeyedArchiver, JSON, your own custom encoding, etc. So long as the dictionary only contains those 6 data types replyHandler will be happy. Otherwise you'll see the Payload contains unsupported type.
error.
For this reason, you can never call the reply handler like so: replyHandler(["response": myCustomObject)
, even if myCustomObject
implements the NSCoding
protocol perfectly.
Summary of encoding choices:
If you do use NSCoding, this is what it'll look like:
iPhone App:
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
let data = NSKeyedArchiver.archivedDataWithRootObject(myCustomObject)
replyHandler(["response": data])
}
Watch App:
WCSession.defaultSession().sendMessage([],
replyHandler: {
response -> Void in
let myCustomObject = NSKeyedUnarchiver.unarchiveObjectWithData(response["response"])
}, errorHandler: nil
)
Note that if you want to recover from a crash when unarchiving objects, you'll need to use the new iOS 9 API, unarchiveTopLevelObjectWithData, which throws an error if there was a problem.
Note: Your custom object must inherit from NSObject
, otherwise you'll get the following error when archiving:
*** NSForwarding: warning: object ... of class 'Foo' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[Foo replacementObjectForKeyedArchiver:]
Upvotes: 13
Reputation: 56
Using NSKeyedArchiver/NSKeyedUnarchiver you can serialize any object that implements NSCoding.
To archive:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject: entries];
Where entries is an array of objects that implement NSCoding.
To unarchive:
NSArray *entries = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSCoding is a protocol with two methods you have to implement like this.
-(id) initWithCoder: (NSCoder *)decoder {
if(self = [super init]){
self.yourpoperty = [decoder decodeObjectForKey:@"PROPERTY_KEY"];
}
return self;
}
- (void) encodeWithCoder: (NSCoder *)encoder {
[encoder encodeObject:self.yourpoperty forKey:@"PROPERTY_KEY"];
}
Upvotes: 2
Reputation: 514
I saw this error when accidentally transporting over 400 data objects to the watch. Limiting to 20 objects fixed the error.
Upvotes: 0
Reputation: 944
I solved it by sending JSON string in dictionary format directly to the call back method from the iPhone app Appdelegate
- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message replyHandler:(void(^)(NSDictionary<NSString *, id> *replyMessage))replyHandler {
instead of converting the JSON dictionary to normal dictionary. And converting this JSON dictionary to normal dictionary in the watch viewcontroller call back methods
[[WCSession defaultSession] sendMessage:applicationData
replyHandler:^(NSDictionary *reply) {}
Since I was calling this methods from two different viewcontrollers in watch,sending normal dictionary from the iPhone app to watch works fine the first time but for some reasons I was getting the errors in the question if I send the dictionary from the iPhone app to watch for the second view controller of watch.
Upvotes: 1
Reputation: 20257
"Payload contains unsupported type" likely means you are sending a custom object in your message dictionary. You will need to serialize this data to contain only the supported types (NSNumber, NSDate, NSString, NSData, NSArray, and NSDictionary).
I have a github project that automatically serializes your custom objects into safe ones for watchkit transfer. You can check it out here.
Upvotes: 7