title: iOS Obj-C: How to open a local file with its default application?
body: <p>I've searched &amp; tried anything I've found on stackoverflow on this, but can't go on with this problem.</p>
  
  <p>I'm creating a GPS application that I want to interact with another one (which is specialized in aeronautic navigation) named SkyDemon.</p>
  
  <p>On this last one, you can export a route, and receive it by email. It then contains an attachement file named MyRoute.flightplan (it is basically an xml file with several information on it) that can be open directly from the "mail" app for example and when touching the app, it opens correctly the route on it.
  <a href="https://i.stack.imgur.com/rcvw8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rcvw8.png" alt="The email received, with the attached file, that can be directly opened by touching it"></a>
  <a href="https://i.stack.imgur.com/51CFp.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/51CFp.jpg" alt="The route opened on SkyDemon App"></a></p>
  
  <p>I tried to reproduce the way "mail" does that by creating a local xml file with the good extension :</p>
  
  <pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  
  NSString *filename = [NSString stringWithFormat:@"%@/directions.flightplan",documentsDirectory];
  
  NSString *content = @"&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;&lt;DivelementsFlightPlanner&gt;&lt;PrimaryRoute Start=\"N484459.10 E0020640.25\" Level=\"3000\" PlannedFuel=\"120.000000\"&gt;&lt;RhumbLineRoute To=\"N483837.45 E0014947.70\" Level=\"MSL\" LevelChange=\"B\" /&gt;&lt;ReferencedAirfields /&gt;&lt;/PrimaryRoute&gt;&lt;/DivelementsFlightPlanner&gt;";
  [content writeToFile:filename atomically:NO encoding:NSStringEncodingDetectionAllowLossyKey error:nil];
  </code></pre>
  
  <p>Then I call UIDocumentInteractionController to get the same controller as for "mail" to be opened :</p>
  
  <pre><code>UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filename]];
  documentController.delegate = self;
  [documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
  </code></pre>
  
  <p>The application answers to UIDocumentInteractionControllerDelegate with this :</p>
  
  <pre><code>- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
      return  self;
  }
  
  - (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
      NSLog(@"&gt;&gt;&gt;&gt;&gt;&gt; Start sending to %@", application);
  }
  
  - (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {
      NSLog(@"&gt;&gt;&gt;&gt;&gt;&gt; Done sending.");
  }
  </code></pre>
  
  <p>When I run my application I get the good controller, I can choose the application "SkyDemon" and then I've got the following Assertion Failure :</p>
  
  <blockquote>
    <p>2017-08-17 17:16:49.709766+0200 MyApp[22530:4191257] *** Assertion failure in -[_UIDocumentInteractionControllerOpenWithAppActivity performActivity], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.9.1/UIDocumentInteractionController.m:420</p>
    
    <p>2017-08-17 17:16:49.712475+0200 MyApp[22530:4191257] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIDocumentInteractionController has gone away prematurely!'</p>
    
    <p>*** First throw call stack:
    (0x18accefe0 0x189730538 0x18acceeb4 0x18b767720 0x19150b170 0x1917b81d8 0x19114df80 0x191151770 0x190f256f4 0x190f254e4 0x190f24f98 0x190f24b1c 0x190e39338 0x190e39154 0x18dfea0d4 0x10020da10 0x100212b78 0x18ac7d0c8 0x18ac7ace4 0x18abaada4 0x18c615074 0x190e65f74 0x100102878 0x189bb959c)
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb) </p>
  </blockquote>
  
  <p>Has anybody an idea?</p>
  
  <p>Thanks for your help :)</p>