Advertisement
Guest User

Untitled

a guest
Jan 15th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void)loadAndOpenDocument:(NSDictionary *)documentData {
  2.     if(![[documentData objectForKey:@"attachment"] length]) {
  3.         return;
  4.     }
  5.    
  6.     [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  7.     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
  8.                    ^{
  9.                        // Async part
  10.                        NSDictionary *answer = [[WebService sharedManager] call:@"getInvoice" arguments:@[[documentData objectForKey:@"attachment"]]];
  11.                        
  12.                        dispatch_async(dispatch_get_main_queue(),
  13.                                       ^{
  14.                                           // Sync part (in main thread)
  15.                                           [MBProgressHUD hideHUDForView:self.view animated:YES];
  16.                                          
  17.                                           if([[AnswerHandler sharedManager] processAnswer:answer]) {
  18.                                               NSArray*pdfData = [answer valueForKeyPath:@"data.pdf"];
  19.                                              
  20.                                               NSMutableData *data = [[NSMutableData alloc] initWithCapacity:pdfData.count];
  21.                                               for (NSNumber *byteVal in pdfData) {
  22.                                                   Byte b = (Byte)(byteVal.intValue);
  23.                                                   [data appendBytes:&b length:1];
  24.                                               }
  25.                                              
  26.                                               if (pdfData) {
  27.                                                  
  28.                                                   NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  29.                                                   NSString *documentDirectory=[paths objectAtIndex:0];
  30.                                                  
  31.                                                   NSString*textToSet = [documentData objectForKey:@"attachment"]; // ????
  32.                                                  
  33.                                                   NSString * imgFileName = @"invoice.pdf";
  34.                                                  
  35.                                                   NSString *finalPath=[documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:imgFileName,textToSet]];
  36.                                                  
  37.                                                   NSLog(@"finalpath--%@",finalPath);
  38.                                                   if(pdfData)
  39.                                                       [data writeToFile:finalPath atomically:YES];
  40.                                                  
  41.                                                   DisplayInvoiceViewController *detailViewController = [[DisplayInvoiceViewController alloc] initWithNibName:@"DisplayInvoiceViewController" bundle:[NSBundle mainBundle]];
  42.                                                   detailViewController.pdfPath = finalPath;
  43.                                                   [self.navigationController pushViewController:detailViewController animated:YES];
  44.                                               }
  45.                                           }
  46.                                       });
  47.                    });
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement