title: Code to share file path/file between a share extension and iOS app
body: <p>I have added a share extension for my app say SAMPLE (already exists on the app store), called lets say SAMPLESHARE. Whenever a user, say takes a picture and tries to share it, I want them to go through a view controller of an Open In functionality, and not get the Post dialogue from Apple, basically bypassing it. So I am trying to share the picture between the share extension and my app, by creating an app group that is shared between the app and plugin and then passing the file paths to the openURL of the application delegate of my app. </p>
  
  <p>So in my main application delegate I have </p>
  
  <pre><code>- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  {
  
      return [[SAMPLEExternalFileHandler shared] handleExternalFileURL:url];
  }
  </code></pre>
  
  <p>which basically I use for checking everytime if I have a URL file path that needs to open a different flow. </p>
  
  <p>In my SHAREEXTENSION I have </p>
  
  <pre><code>#import "ShareViewController.h"
  #import &lt;MobileCoreServices/UTCoreTypes.h&gt;
  //Macro to hide post dialog or not, if defined, will be hidden, comment during debugging
  #define HIDE_POST_DIALOG
  
  @interface ShareViewController ()
  
  @end
  
  @implementation ShareViewController
  
  NSUInteger m_inputItemCount = 0; // Keeps track of the number of attachments we have opened asynchronously.
  NSString * m_invokeArgs = NULL;  // A string to be passed to your AIR app with information about the attachments.
  NSString * APP_SHARE_GROUP = @"group.com.SAMPLE.SAMPLESHAREPLUGIN";
  const NSString * APP_SHARE_URL_SCHEME = @"SAMPLE";
  CGFloat m_oldAlpha = 1.0; // Keeps the original transparency of the Post dialog for when we want to hide it.
  
  - (BOOL)isContentValid {
      // Do validation of contentText and/or NSExtensionContext attachments here
      return YES;
  }
  
  - ( void ) didSelectPost
  {
  #ifdef HIDE_POST_DIALOG
      return;
  #endif
      [ self passSelectedItemsToApp ];
      // Note: This call is expected to be made here. Ignore it. We'll tell the host we are done after we've invoked the app.
      //    [ self.extensionContext completeRequestReturningItems: @[] completionHandler: nil ];
  }
  - ( void ) addImagePathToArgumentList: ( NSString * ) imagePath
  {
      assert( NULL != imagePath );
  
      // The list of arguments we will pass to the AIR app when we invoke it.
      // It will be a comma-separated list of file paths: /path/to/image1.jpg,/path/to/image2.jpg
      if ( NULL == m_invokeArgs )
      {
          m_invokeArgs = imagePath;
      }
      else
      {
          m_invokeArgs = [ NSString stringWithFormat: @"%@,%@", m_invokeArgs, imagePath ];
      }
  }
  
  - ( NSString * ) saveImageToAppGroupFolder: ( UIImage * ) image
                                  imageIndex: ( int ) imageIndex
  {
      assert( NULL != image );
  
      NSData * jpegData = UIImageJPEGRepresentation( image, 1.0 );
  
      NSURL * containerURL = [ [ NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier: APP_SHARE_GROUP ];
      NSString * documentsPath = containerURL.path;
  
      // Note that we aren't using massively unique names for the files in this example:
      NSString * fileName = [ NSString stringWithFormat: @"image%d.jpg", imageIndex ];
  
      NSString * filePath = [ documentsPath stringByAppendingPathComponent: fileName ];
      [ jpegData writeToFile: filePath atomically: YES ];
  
      return filePath;
  }
  
  - ( void ) passSelectedItemsToApp
  {
      NSExtensionItem * item = self.extensionContext.inputItems.firstObject;
  
      // Reset the counter and the argument list for invoking the app:
      m_invokeArgs = NULL;
      m_inputItemCount = item.attachments.count;
  
      // Iterate through the attached files
      for ( NSItemProvider * itemProvider in item.attachments )
      {
          // Check if we are sharing a JPEG
          if ( [ itemProvider hasItemConformingToTypeIdentifier: ( NSString * ) kUTTypeImage ] )
          {
              // Load it, so we can get the path to it
              [ itemProvider loadItemForTypeIdentifier: ( NSString * ) kUTTypeImage
                                               options: NULL
                                     completionHandler: ^ ( UIImage * image, NSError * error )
               {
                   static int itemIdx = 0;
  
                   if ( NULL != error )
                   {
                       NSLog( @"There was an error retrieving the attachments: %@", error );
                       return;
                   }
  
                   // The app won't be able to access the images by path directly in the Camera Roll folder,
                   // so we temporary copy them to a folder which both the extension and the app can access:
                   NSString * filePath = [ self saveImageToAppGroupFolder: image imageIndex: itemIdx ];
  
                   // Now add the path to the list of arguments we'll pass to the app:
                   [ self addImagePathToArgumentList: filePath ];
  
                   // If we have reached the last attachment, it's time to hand control to the app:
                   if ( ++itemIdx &gt;= m_inputItemCount )
                   {
                       [ self invokeApp: m_invokeArgs ];
                   }
               } ];
          }
      }
  }
  - ( void ) invokeApp: ( NSString * ) invokeArgs
  {
      // Prepare the URL request
      // this will use the custom url scheme of your app
      // and the paths to the photos you want to share:
      NSString * urlString = [ NSString stringWithFormat: @"%@://%@", APP_SHARE_URL_SCHEME, ( NULL == invokeArgs ? @"" : invokeArgs ) ];
      NSURL * url = [ NSURL URLWithString: urlString ];
  
      NSString *className = @"UIApplication";
      if ( NSClassFromString( className ) )
      {
          id object = [ NSClassFromString( className ) performSelector: @selector( sharedApplication ) ];
          [ object performSelector: @selector( openURL: ) withObject: url ];
      }
  
      // Now let the host app know we are done, so that it unblocks its UI:
      [ super didSelectPost ];
  }
  
  #ifdef HIDE_POST_DIALOG
  - ( NSArray * ) configurationItems
  {
      // Comment out this whole function if you want the Post dialog to show.
      [ self passSelectedItemsToApp ];
  
      // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
      return @[];
  }
  #endif
  
  
  #ifdef HIDE_POST_DIALOG
  - ( void ) willMoveToParentViewController: ( UIViewController * ) parent
  {
      // This is called at the point where the Post dialog is about to be shown.
      // Make it transparent, so we don't see it, but first remember how transparent it was originally:
  
      m_oldAlpha = [ self.view alpha ];
      [ self.view setAlpha: 0.0 ];
  }
  #endif
  
  #ifdef HIDE_POST_DIALOG
  - ( void ) didMoveToParentViewController: ( UIViewController * ) parent
  {
      // Restore the original transparency:
      [ self.view setAlpha: m_oldAlpha ];
  }
  #endif
  #ifdef HIDE_POST_DIALOG
  - ( id ) init
  {
      if ( self = [ super init ] )
      {
          // Subscribe to the notification which will tell us when the keyboard is about to pop up:
          [ [ NSNotificationCenter defaultCenter ] addObserver: self selector: @selector( keyboardWillShow: ) name: UIKeyboardWillShowNotification    object: nil ];
      }
  
      return self;
  }
  #endif
  #ifdef HIDE_POST_DIALOG
  - ( void ) keyboardWillShow: ( NSNotification * ) note
  {
      // Dismiss the keyboard before it has had a chance to show up:
      [ self.view endEditing: true ];
  }
  #endif
  @end
  </code></pre>
  
  <p>And my info.plist for the extension is </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
  &lt;plist version="1.0"&gt;
  &lt;dict&gt;
      &lt;key&gt;CFBundleDevelopmentRegion&lt;/key&gt;
      &lt;string&gt;en&lt;/string&gt;
      &lt;key&gt;CFBundleDisplayName&lt;/key&gt;
      &lt;string&gt;SAMPLESHARE&lt;/string&gt;
      &lt;key&gt;CFBundleExecutable&lt;/key&gt;
      &lt;string&gt;$(EXECUTABLE_NAME)&lt;/string&gt;
      &lt;key&gt;CFBundleIdentifier&lt;/key&gt;
      &lt;string&gt;com.org.SAMPLE.$(PRODUCT_NAME:rfc1034identifier)&lt;/string&gt;
      &lt;key&gt;CFBundleInfoDictionaryVersion&lt;/key&gt;
      &lt;string&gt;6.0&lt;/string&gt;
      &lt;key&gt;CFBundleName&lt;/key&gt;
      &lt;string&gt;$(PRODUCT_NAME)&lt;/string&gt;
      &lt;key&gt;CFBundlePackageType&lt;/key&gt;
      &lt;string&gt;XPC!&lt;/string&gt;
      &lt;key&gt;CFBundleShortVersionString&lt;/key&gt;
      &lt;string&gt;1.0&lt;/string&gt;
      &lt;key&gt;CFBundleSignature&lt;/key&gt;
      &lt;string&gt;????&lt;/string&gt;
      &lt;key&gt;CFBundleVersion&lt;/key&gt;
      &lt;string&gt;1&lt;/string&gt;
      &lt;key&gt;NSExtension&lt;/key&gt;
      &lt;dict&gt;
          &lt;key&gt;NSExtensionAttributes&lt;/key&gt;
           &lt;dict&gt;
          &lt;key&gt;NSExtensionActivationRule&lt;/key&gt;
          &lt;dict&gt;
              &lt;key&gt;NSExtensionActivationSupportsImageWithMaxCount&lt;/key&gt;
              &lt;integer&gt;1&lt;/integer&gt;
          &lt;/dict&gt;
      &lt;/dict&gt;
          &lt;key&gt;NSExtensionMainStoryboard&lt;/key&gt;
          &lt;string&gt;MainInterface&lt;/string&gt;
          &lt;key&gt;NSExtensionPointIdentifier&lt;/key&gt;
          &lt;string&gt;com.apple.share-services&lt;/string&gt;
      &lt;/dict&gt;
  &lt;/dict&gt;
  &lt;/plist&gt;
  </code></pre>
  
  <p>I have basically used some commons license code off the internet(reputed site), which claims to have passed the app store review process. </p>
  
  <p>There are two workarounds in the code, one is to call the OpenURL from the share extension(which from scouring SO seems like is still not possible normally without workarounds on iOS 8.3 and above) and the second is to hide the post dialogue and the keyboard that apple provides by default when anyone clicks on share. 
  This works. </p>
  
  <p>I have two questions</p>
  
  <pre><code>1.) Will this be accepted on the app store? -- basically how are apps like facebook/whatsapp doing it and they are being accepted?
  2.) Whenever I run this, it says `NSExtensionActivationRule` if set to `TRUEPREDICATE` will be rejected in review, what should the value be? 
  </code></pre>
  
  <p><strong>UPDATE:</strong> </p>
  
  <p>So scouring through the documentation I have found a fix for question 2, and changed this. Now everything works, and there is no <code>TRUEPREDICATE</code>, will this be accepted on the store or is there another way to do this? </p>
  
  <p><strong>UPDATE 2:</strong> </p>
  
  <p>I have now used <code>NSUserDefaults</code> to pass the data from the extension to the app, guess that is also one requirement for sharing data.</p>