0


これは私のアクティビティの 1 つで、NSURL 接続を使用して URL をヒットし、このデータを解析して、 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. ........など}

これを 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ファイルを開いて表示したい場合は、を使用してUIWebViewPDFファイルを開いて表示できます。これをこれに渡すrequestFile-

[webView loadRequest:requestFile];

webViewの変数名は次のとおりですUIWebView

于 2013-07-15T07:05:28.853 に答える