I have implemented a client-server transferring from Windows desktop application to iPhone App. I transfer data in a different format but always as a string using NSStream polling mode:
if([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output) {
[_arrRisposta addObject:output];
_strRisposta = [NSMutableString stringWithString: output];
}
}
else {
break;
}
}
Everything works perfectly, but when I transfer a PDF file and save it, the file is not the same. When I load the file in UIWebView the number of pages is correct but there is no content. I save the file like this:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *dataFile = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"temp.%@", @"pdf"]];
if ([fileManager fileExistsAtPath:dataFile]) {
[fileManager removeItemAtPath:dataFile error:nil];
}
[_strContenuto writeToFile:dataFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSURL *fileUrl = [NSURL fileURLWithPath:dataFile];
[webModello loadRequest:[NSURLRequest requestWithURL:fileUrl]];
What’s wrong? I tried to change the encoding of the string but I have not solved. If I compare the transferred file with the original, there are some characters that do not match.
Could someone help me, please?
You should not use strings. Change your code to use NSData objects. PDF files are not strings, and there are all sorts of encoding issues you would have to worry about to get this working with strings (if it ever would).
I have not looked up the exact api usage for NSData, this is all from my head… so don’t go copy and pasting this!