Skip to main content
Question

iOS: Objective-C - having trouble previewing attachment files (QLPreviewController, UIDocumentInteractionController, and UIWebView.)


Forum|alt.badge.img+4
  • Do Gooder (Employee)
  • 3 replies

I am trying to preview an attachment (word, doc, or pdf) in a view controller. When a place attachment is selected, PlaceAttachment_ViewController should be called and display/preview the selected place attachment. This is working on iPhone 11 and 13 but not iPhone 7 or 8, running iOS 15.5.

 

I have tried using the following approaches to preview an attachment file: QLPreviewController, UIDocumentInteractionController, and UIWebView.

 

Here is sample code that I used for each of those:

 

QLPreviewController:

 

QLPreviewController* ql = [QLPreviewController new];

ql.dataSource = self;

ql.delegate = self;

                

// [self.navigationController pushViewController:ql animated:TRUE];

ql.currentPreviewItemIndex = 0;

 [self presentViewController:ql animated:NO completion:nil];

 

- (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id <QLPreviewItem>)item {

 

  return YES;

}

 

- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController 😉 controller {

 

  return 1;

}

 

- (id <QLPreviewItem>) previewController: (QLPreviewController 😉 controller previewItemAtIndex: (NSInteger) index {

  NSString *filePath = [[MetrixPublicCache instance] getItem:@"filePath"];

  NSString *fileExtension = [[MetrixPublicCache instance] getItem:@"fileExtension"];

 

  NSString *fullFilePath = [@"file://" stringByAppendingString: filePath];

 

  NSString* test = @"/Users/sybaus/Library/Developer/CoreSimulator/Devices/ACFA5AE2-EB97-4F35-920A-C52132308569/data/Containers/Data/Application/D928D9C0-F2DF-4353-B3F2-2DA63E9FC067/Documents/attachments/20833%20Chattanooga,%20TN%201%20Feb%202016%20-%20106609-02,%20RevB%20Acoustical%20Measurements.xls";

 

  // NSURL *testURL = [[NSURL alloc] initFileURLWithPath:filePath];

 

   NSString *path = [[NSBundle mainBundle] pathForResource:filePath ofType:fileExtension];

  NSURL *url = [NSURL fileURLWithPath:path];

   return url;

  // NSString *filePath = [NSString stringWithFormat:@"%@/%@",[[MetrixAttachmentManager sharedMetrixAttachmentManager] getAttachmentPath], attachmentName];

// return [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:filePath ofType:fileExtension]];

  // return [NSURL fileURLWithPath:path];

 

 

 

 

}

 

 

Using QLPreviewController gives me the following error:

 

Exception    NSException *    "*** -[NSURL initFileURLWithPath:]: nil string parameter"    0x000060000365eb80

 

 

 

 

UIDocumentInteractionController:

 

// Initialize Document Interaction Controller

self.controller = [UIDocumentInteractionController interactionControllerWithURL:URL];

                

 // Configure Document Interaction Controller

 [self.controller setDelegate:self];

                

// Preview PDF

[self.controller presentPreviewAnimated:YES];

                

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {

    return self;

}

 

- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {

    NSLog(@"Starting to send this puppy to %@", application);

}

 

- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {

    NSLog(@"We're done sending the document.");

}

 

 

This gives me the following error:

 

Exception    NSException *    "*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[2]"    0x000060000204e2b0

 

UIWebView:

    NSURL *targetURL = [NSURL URLWithString: urlTextEscaped]; // only returns url if I pass in urlTextEscaped

                 NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];

                 self.mywebview.scalesPageToFit = YES;

                 [self.mywebview loadRequest:request];

 

This will result in a blank screen being loaded instead of the attached file. See screenshot below:

 

 

I am calling the following methods to display PlaceAttachment_ViewController:

 

        [MetrixActivityHelper startNewViewControllerByName:self newViewControllerName:@"PlaceAttachment_ViewController"];

        PlaceAttachment_ViewController *vc = [[PlaceAttachment_ViewController alloc] init];

        [vc.navigationItem setTitle:nav_title];

        vc.attachmentQuery = attachmentQuery;

        [MetrixActivityHelper startNewViewControllerAndFinish:self newViewController:vc];

 

Any suggestions on resolving this issue? 

0 replies

Be the first to reply!

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings