开发者

How can I see components of NSData?

开发者 https://www.devze.com 2023-03-16 02:35 出处:网络
I am trying to write a PDF file from NSData I got from an http request and then display the pdf.When I call [response description], I get <30>, so I am at least getting something back.However, when

I am trying to write a PDF file from NSData I got from an http request and then display the pdf. When I call [response description], I get <30>, so I am at least getting something back. However, when I run this code, an error comes up: "failed to find PDF header: `%PDF' not found."

Is there some way to see what my NSData is if it is not a pdf? Am I going about this incorrectly? Thanks!

 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

if (response) {
    NSString *description = [response description]; 
    NSLog(@"description: %@", description);  //this prints: <30>
} else {
    NSLog(@"response is nil");
}

NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                    NSUserDomainMask, YES); 

NSString *filename = @"Test1.pdf"; 

NSString *dirPath = [dirs objectAtIndex:0]; 

NSError *error = nil;
NSString *path=[dirPath stringByAppendingPathComponent:filename];
[response writeToFile:path atomically:YES];
NSLog(@"Write returned error: %@", [开发者_如何学Cerror localizedDescription]);

NSURL *pdfUrl = [NSURL fileURLWithPath: path];
[webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号