更新
我想为APNs消息创建一个JSON兼容的字典,看起来像这样:
{
"aps" : {
"alert": "new push"
}
}
实际上我可以这样写:
{
aps = {
alert = "new push";
};
}
这是我的实际代码:
NSString *content = [NSString stringWithFormat:@"new push."];
self.dict1 = @{ @"alert": content} ;
NSMutableDictionary *newPush = [NSMutableDictionary dictionary];
[newPush setObject:self.dict1 forKey:@"aps"];
有人可以解释我,我做错了什么?如何将其转换为正确的格式?
更新
NSDictionary *dict = @{ @"aps" : @{ @"alert" : content } };
//create the json
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
options:NSJSONWritingPrettyPrinted
error:&error];
NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
我从控制台得到了这个建议:There was an error sending the data to the origin. Be sure you didn't try to send non-object or non-JSON data.
你可以直接做
NSDictionary *dict = @{
@"aps" : @{ @"alert" : @"new push" }
};
(与您在前两个代码示例中编写的方式非常相似)。
然后使用NSJSONSerialization
创建格式化的JSON
使用NSJSONSerialization将Dictionary或NSArray转换为json对象
...
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:newPush
options:NSJSONWritingPrettyPrinted
error:&error];
...