I am writing an app, that is connecting to EchoServer with web socket, and i am sending there a difference information pack(NSDictionary with(BOOL,String) in different format (JSON,XML,Binary).
there is an action when i am sending this pack
- (IBAction)sendSocket:(id)sender
{
NSString *enteredText = [[NSString alloc] initWithString:enterTextField.text];
BOOL switchSelected = switchControl.on;
NSDictionary *dictionaryToSend = [[NSDictionary alloc] initWithObjectsAndKeys:enteredText, @"text", [NSNumber numberWithBool:switchSelected], @"switch", nil];
NSMutableData *dataObject = [[NSMutableData alloc] init];
if (segmentControl.selectedSegmentIndex == 0)
{
dataObject = [[NSJSONSerialization dataWithJSONObject:dictionaryToSend options:NSJSONWritingPrettyPrinted error:nil] mutableCopy];
}
else if (segmentControl.selectedSegmentIndex == 1)
{
NSError *error;
dataObject = [[NSPropertyListSerialization dataWithPropertyList:dictionaryToSend format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]mutableCopy];
NSLog(@"%@",dataObject);
}
else if (segmentControl.selectedSegmentIndex == 1)
{
NSError *error;
dataObject = [[NSPropertyListSerialization dataWithPropertyList:dictionaryToSend format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error]mutableCopy];
}
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(sendClickedWithData:)])
{
[self.delegate sendClickedWithData:dataObject];
}
}
delegate method
- (void)sendClickedWithData:(NSData *)data
{
[_webSocket send:data];
}
all functions with web socket are right. but after I am sent, I receive the same data pack, and I need to decode this in main ViewController(action is done in ViewController2)
It is easy with JSON
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message
{
NSMutableDictionary *decodedData = [[NSMutableDictionary alloc] init];
decodedData = [NSJSONSerialization JSONObjectWithData:message
options:kNilOptions
error:nil];
but I stuck to do the same to XML and Binary! Can you help me improve this method for this 2 types?
No comments:
Post a Comment