0

这是我的活动之一,我使用 NSURL 连接点击了一个 url,然后解析这个数据并以字节码获取 JSON 数据这个数据,如
NSString *ptr= { 82, 111, 111, 116, 32, 50, 32, 48, 32, 82, 10, 47, 83, 105, 122, 101, 32, 51, 50, 10, 47, 73, 110, 102, 111, 32, 49, 32, 48, 32, 82, 10, 62, 62、10、115、116、97、114、116、120、114、101、102、10、49、52、51、52、50、55、10、37、37、69、79、70、10。 ........ETC }

我在 NSString 中得到了这个,之后

  NSMutableArray *bytes=[[NSMutableArray alloc]init];
  [bytes addObject:ptr];

  NSMutableData *data = [[NSMutableData alloc] initWithCapacity:bytes.count];
  NSLog(@"file %@",data);
  for (NSNumber *byteVal in bytes)
  {
    Byte b = (Byte)(byteVal.intValue);
    [data appendBytes:&b length:1];
  }

 NSArray *docDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *docDirectory = [docDirectories objectAtIndex:0];
 NSString *fileName = [docDirectory stringByAppendingPathComponent:@"abcd.pdf"];
 NSLog(@"file %@",fileName);

 [data writeToFile:fileName atomically:NO];

 NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

 NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"abcd.pdf"];

 NSURL *targetURL = [NSURL fileURLWithPath:filePath];
 NSURLRequest *requestFile = [NSURLRequest requestWithURL:targetURL];

 NSLog(@"request path %@",requestFile);

这是输出。它会创建 pdf,但我不明白如何打开和查看生成的 pdf 文件。
文件 /Users/rahulsharma/Library/Application Support/iPhone Simulator/6.1/Applications/CD29ED82-8039-411F-8BBA-2784E5445EDE/Documents/abcd.pdf

request path <NSURLRequestfile://localhost/Users/rahulsharma/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/CD29ED82-8039-411F-8BBA-2784E5445EDE/Documents/abcd.pdf>

请帮助我解决如何获取pdf的问题。

 NSString *responseText;
 NSMutableArray *byteArray;
viewdidload
{
  [self report];
 }
-(void)report
{
finished=FALSE;
NSString *case_id=[NSString stringWithFormat:@"8cc61590-42b0-4433-b8ee-25e40d5e6033"];

NSString *investigation_id=[NSString stringWithFormat:@"33a8b03f-4ad2-424a-8aa9-02a07d3937eb"];
NSString *test_id=[NSString stringWithFormat:@"e5a7e867-422c-4365-b920-87fdf5d82783"];


dictionary = [NSMutableDictionary dictionary];

[dictionary setObject:case_id forKey:@"CaseId"];
[dictionary setObject:investigation_id forKey:@"InvestigationId"];
[dictionary setObject:test_id forKey:@"TestId"];
// [dictionary setObject:Procedure forKey:@"proc"];
// [dictionnary setObject:count  forKey:@"count"];
NSLog(@"dict %@",dictionary);
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];
NSLog(@"json %@",jsonData);
NSURL *url1 = [NSURL URLWithString:@"http://192.168.1.202:81/LaboratoryModule/LISService.asmx/GetpatienttestReport"];
NSLog(@"url is %@",url1);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url1];
[request setHTTPMethod:@"POST"];

[request addValue: @"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:jsonData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if( theConnection )
{
    webData = [NSMutableData data] ;
    NSLog(@"%@",webData);

}
else
{
    NSLog(@"theConnection is NULL");
}
while(!finished)
{
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

}

 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

 [webData setLength: 0];
 NSLog(@"web is = %@",webData);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
NSLog(@"web is = %@",webData);

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");

}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

responseText = [[NSString alloc] initWithData:self.webData encoding:NSUTF8StringEncoding];
NSLog(@"val is %@",responseText);

SBJsonParser *par=[[SBJsonParser alloc]init];
NSString *filecontent=[[NSString alloc]initWithString:responseText];
NSDictionary *data=(NSDictionary*)[par objectWithString:filecontent error:nil];
NSLog(@" data is %@",data);
byteArray=[data objectForKey:@"d"];
NSLog(@" string is %@",byteArray);


NSLog(@"DONE. Received Bytes: %d", [webData length]);

finished=TRUE;       
}
4

1 回答 1

1

如果您想在 iPhone 中打开和查看 PDF 文件,您可以使用 aUIWebView打开和显示 PDF 文件。通过requestFile这个 -

[webView loadRequest:requestFile];

webView是 的变量名UIWebView

于 2013-07-15T07:05:28.853 回答