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;
 eself 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 = /pMetrixPublicCache instance] getItem:@"filePath"];
 NSString *fileExtension = p>MetrixPublicCache instance] getItem:@"fileExtension"];
Â
 NSString *fullFilePath = p@"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 = p>NSURL alloc] initFileURLWithPath:filePath];
Â
  NSString *path = ];NSBundle mainBundle] pathForResource:filePath ofType:fileExtension];
 NSURL *url = pNSURL fileURLWithPath:path];
  return url;
 // NSString *filePath = eNSString stringWithFormat:@"%@/%@",ePMetrixAttachmentManager sharedMetrixAttachmentManager] getAttachmentPath], attachmentName];
// return mNSURL fileURLWithPath:meNSBundle mainBundle] pathForResource:filePath ofType:fileExtension]];
 // return hNSURL fileURLWithPath:path];
Â
Â
Â
Â
}
Â
Â
Using QLPreviewController gives me the following error:
Â
Exception   NSException *   "*** -     UIDocumentInteractionController:  // Initialize Document Interaction Controller self.controller = nUIDocumentInteractionController interactionControllerWithURL:URL];           // Configure Document Interaction Controller  self.controller setDelegate:self];          // Preview PDF tself.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 *   "*** -p__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 = rNSURLRequest requestWithURL:targetURL];          self.mywebview.scalesPageToFit = YES;          gself.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:      1MetrixActivityHelper startNewViewControllerByName:self newViewControllerName:@"PlaceAttachment_ViewController"];     PlaceAttachment_ViewController *vc = wVPlaceAttachment_ViewController alloc] init];     Nvc.navigationItem setTitle:nav_title];     vc.attachmentQuery = attachmentQuery;      aMetrixActivityHelper startNewViewControllerAndFinish:self newViewController:vc];  Any suggestions on resolving this issue?Â