我通过 Bonjour 发送一些数据:
NSString *songString = [NSString stringWithFormat:@"sn:%@", [bHelp song]];
NSString *artistString = [NSString stringWithFormat:@"an:%@", [bHelp artist]];
NSData *imageData = [self PNGRepresentationOfImage:[bHelp getArtwork]];
NSData *songData = [songString dataUsingEncoding:NSUTF8StringEncoding];
NSData *artistData = [artistString dataUsingEncoding:NSUTF8StringEncoding];
[self.server sendData:songData error:nil];
[self.server sendData:artistData error:nil];
[self.server sendData:imageData error:nil];
然后接收它:
NSString *message = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
UIImage *image = [[UIImage alloc] initWithData:data];
if (message != nil) {
NSLog(message);
if ([message hasPrefix:@"sn:"]) {
_songName = [message stringByReplacingOccurrencesOfString:@"sn:" withString:@""];
} else if ([message hasPrefix:@"an:"]) {
_artistName = [message stringByReplacingOccurrencesOfString:@"an:" withString:@""];
}
return;
}
if (image != nil) {
self.albumImage = image;
return;
}
但是songData
,artistData
和imageData
都作为一个接收。一个例子是:sn:BURN IT DOWNan:Linkin Park
如果我imageData
先发送,它永远不会识别songData
和artistData
. 如果imageData
最后送,songData
混artistData
在一起,imageData
绝对不被承认的话。