title: Using UIDocumentInteractionController to display presentPreviewAnimated: via UIWebView
body: <p>I'd like to intercept the clicks from any of the <a href="http://developer.apple.com/iphone/library/qa/qa2008/qa1630.html" rel="nofollow noreferrer">supported file types</a> of the UIDocumentInteractionController and display them in a modal view (provided by <code>presentPreviewAnimated:</code> method of UIDocumentInteractionController).  This modal view also provides the "Open In..." functionality to open any of these documents in other compatible applications on the device.  The code below will intercept these clicks, detect if the URL string has any of the appropriate suffixes, load this data, write it to the directory, read it again and finally use the data with the <code>presentPreviewAnimated:</code> method.  I have to write the data to the file system first because the UIDocumentInteractionController requires local data and cannot use the data directly from the URL (as far as I've read).</p>
  
  <pre><code>- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
   if (navigationType == UIWebViewNavigationTypeLinkClicked) {
    ILog(@"URL loading for NavigationTypeLinkClicked: %@", request.URL.absoluteString);
  
    if ([request.URL.absoluteString hasSuffix:@"pdf"] || [request.URL.absoluteString hasSuffix:@"doc"] || [request.URL.absoluteString hasSuffix:@"xls"] || [request.URL.absoluteString hasSuffix:@"ppt"] || [request.URL.absoluteString hasSuffix:@"rtf"] || [request.URL.absoluteString hasSuffix:@"key"] || [request.URL.absoluteString hasSuffix:@"numbers"] || [request.URL.absoluteString hasSuffix:@"pages"]) {
     if (NSClassFromString(@"UIDocumentInteractionController")) {
                  NSArray *parts = [request.URL.absoluteString componentsSeparatedByString:@"/"];
                  self.previewDocumentFileName = [parts lastObject];
                  NSLog(@"The file name is %@", previewDocumentFileName);
  
      // Get file online
                  NSData *fileOnline = [[NSData alloc] initWithContentsOfURL:request.URL];
  
      // Write file to the Documents directory
                  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                  NSString *documentsDirectory = [paths objectAtIndex:0];
                  if (!documentsDirectory) {NSLog(@"Documents directory not found!");}
                  NSString *appFile = [documentsDirectory stringByAppendingPathComponent:previewDocumentFileName];
                  [fileOnline writeToFile:appFile atomically:YES];
      NSLog(@"Resource file '%@' has been written to the Documents directory from online", previewDocumentFileName);
      [fileOnline release];
  
      // Get file again from Documents directory
                  NSURL *fileURL = [NSURL fileURLWithPath:appFile];
  
      UIDocumentInteractionController* docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
  
      docController.delegate = self;
      [docController retain];
  
      BOOL result = [docController presentPreviewAnimated:YES];
  
      if (!result) {
       [docController release];
      }
      return NO;
     }
    }
   }
  
   // Do lots of other URL-detecting stuff here
  }
  </code></pre>
  
  <p>I also implement the following delegate methods to use the appropriate view and to delete the file after the view has been dismissed (<code>previewDocumentFileName</code> is a class variable):</p>
  
  <pre><code>#pragma mark -
  #pragma mark Document Interaction Controller Delegate Methods
  
  - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
   return self;
  }
  
  - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller {
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *appFile = [documentsDirectory stringByAppendingPathComponent:previewDocumentFileName];
   NSFileManager *fileManager = [NSFileManager defaultManager];
   [fileManager removeItemAtPath:appFile error:NULL];
   //[controller release]; // Release here was causing crashes
  }
  </code></pre>
  
  <p>Please note that the Apple Documentation made specific mention of the fact that the document controller needed to be retained manually, so I did that.  I also needs to be released, so I do that if the controller wasn't used (top method) and attempted to release it after it was used (delegate method), but that release was causing crashes, so I removed it and things seem to be working fine from that standpoint.</p>
  
  <p>With the background out of the way, my question has to do with the fact that while this code seems to work in the iPhone simulator (the online document is displayed correctly in the <code>presentPreviewAnimated:</code> view, although without the 'Open In...' dialogue that can't be used in the sim), it is not showing the <code>presentPreviewMethod:</code> on the iPad sim or my actual iPhone or iPad devices.  I believe the method is being called correctly, but the <code>presentPreviewAnimated:</code> method is returning <code>NO</code>, which means it was not able to display the document preview.</p>
  
  <p>Why would this be?  Am I not saving the file properly (and thus it is not being detected as a *pdf or *doc, etc)?  What else could I be doing wrong?  There is very little documentation or example code out there besides the Apple Documentation, so I hope that at the very least this code will be helpful to someone, although I would like to get it working 100% first.  Thanks in advance.</p>