1

I am working on an app in which I have to send deviceToken and uuid to server in JSON format like: "regid":"x1y2z3","uuid":"1a2b3c" how can I store the NSStrings into NSData in this format and send it to server?

regid string is like x1y2z3 and uuidstring is like 1a2b3c.

my code :

 PushNotification* pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
[pushHandler didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];

NSString *deviceT = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
NSString *tkStr = [[NSString alloc]initWithData:deviceToken encoding:NSUTF8StringEncoding];
tkStr = [deviceT stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"Device Token = %@",tkStr);
//UUID
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidStr = ( NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
CFRelease(uuid);
NSString *finalUIDstr = [uuidStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"UUID = %@",finalUIDstr);
NSArray *keysArray = [NSArray arrayWithObjects:@"regid", @"uuid", nil];
NSArray *objectArray = [NSArray arrayWithObjects:tkStr,finalUIDstr, nil];
//Dictionary

NSDictionary *jsonDict = [NSDictionary dictionaryWithObjects:objectArray forKeys:keysArray];
Ankur Arya
  • 4,693
  • 5
  • 29
  • 50

2 Answers2

2

If you want to generate that JSON string, you can build your dictionary (this is a more concise way to do that in Xcode 4.5 rather than building those two arrays and then combining them into a dictionary):

NSDictionary *dictionary = @{@"regid":tkStr,@"uuid":finalUIDstr};

And then generate your JSON string:

NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary
                                               options:0
                                                 error:&error];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • the server will accept data in `["regid":"x1y2z3","uuid":"1a2b3c"]` format but when I did `NSLog` result is like `JSON DATA = <7b227265 67696422 3a223831 63666131 38343236 61366363 64333666 62633132 64376361 65663734 37633963 30333439 65316163 61333064 33633461 37326330 36636165 36613238 6231222c 22757569 64223a22 38433135 45363831 45434643 34363633 39333644 34334541 42343036 35444641 227d>` is this acceptable ? – Ankur Arya Dec 06 '12 at 17:24
  • Yeah, when you log a `NSData`, it shows you the codes. If you want to see the `NSString` representation of that data, look at `[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]`. – Rob Dec 06 '12 at 17:26
  • thanks it worked but I guess there is some issue at server side `Can not convert D2655594D8B84D7E9B73130D340D9A94` to long value' – Ankur Arya Dec 06 '12 at 17:40
  • It's probably a question of how you're posting that (and in what format the server needs it). See this accepted answer's portion about posting JSON data: http://stackoverflow.com/a/4466899/1271826. But I think we've constructed the JSON correctly here. – Rob Dec 06 '12 at 17:43
1

Convert the NSDictionary to Json (NSJSONSerialization or SBJson) and POST it to the server as Application/Json.

Kenrik
  • 11
  • 1