1

I am trying to add Instagram in "Share To" functionality in my app. I have seen the Instagram's iPhone hooks documents. I have created custom UIActivty which works fine but my question is, is there a way to add "Import with Instagram" functionality as it can be seen in iOS's Photos app iOS Photo App:

enter image description here

In my app for some reason, it does not show that "Import with Instagram". my app Share view :

enter image description here

I do not want to share only with Instagram so no ".igo"

EDIT: All of this is specifically for iOS versions < 10. For some reasons Instagram Share Extension works fine (for my app) in devices with iOS >= 10.

EDIT: I am trying to share image and video with ".jpeg" and ".mov" formats respectively

I have seen/read that Instagram added share extension in release 8.2, so technically all the apps should show "Instagram" in share tray, i.e. it can be seen in Google Photos app.

public void NativeShareImage(UIView sourceView, CGRect sourceRect,
                             UIImage image, string shareCaption, string emailSubject)
{
  string filename = Path.Combine(FileSystemUtils.GetTemporaryDataPath(), "Image.jpg");

  NSError err = null;
  using(var imgData = image.AsJPEG(JpgImageQuality))
  {
    if(imgData.Save(filename, false, out err))
    {
      Logger.Information("Image saved before native share as {FileName}", filename);
    }
    else
    {
      Logger.Error("Image NOT saved before native share as to path {FileName}. {Error}", filename, err.Description);
      return;
    }
  }

  // this are the items that needs to be shared
  // Instagram ignores the caption, that is known
  var activityItems = new List<NSObject>
  {
    new NSString(shareCaption),
    new NSUrl(new Uri(filename).AbsoluteUri)
  };

  // Here i add the custom UIActivity for Instagram
  UIActivity[] applicationActivities = 
  {
    new InstagramActivity(image, sourceRect, sourceView),
  }

  var activityViewController = new UIActivityViewController(activityItems.ToArray(), applicationActivities);
  activityViewController.SetValueForKey(new NSString(emailSubject), new NSString("subject"));

  activityViewController.CompletionWithItemsHandler = (activityType, completed, returnedItems, error) => 
  {
    UserSharedTo(activityType, completed);
  };

  // Hide some of the less used activity types so that Instagram shows up in the list. Otherwise it's pushed off the activity view
  // and the user has to scroll to see it.
  activityViewController.ExcludedActivityTypes = new[] { UIActivityType.AssignToContact, UIActivityType.CopyToPasteboard, UIActivityType.Print };

  if(UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
  {
    PresentViewController(activityViewController, true, null);
  }
  else
  {
    activityViewController.ModalPresentationStyle = UIModalPresentationStyle.Popover;
    PresentViewController(activityViewController, true, null);

    // Get the popover presentation controller and configure it.
    UIPopoverPresentationController presentationController = activityViewController.PopoverPresentationController;
    presentationController.PermittedArrowDirections = UIPopoverArrowDirection.Down;
    presentationController.SourceRect = sourceRect;
    presentationController.SourceView = sourceView;
  }
}



// when opening custom activity use ".igo" to only show instagram
  public class InstagramActivity : UIActivity
  {

    public InstagramActivity(UIImage imageToShare, CGRect frame, UIView view, string shareCaption = "")
    {
      _ImageToShare = imageToShare;
      _Frame = frame;
      _View = view;
    }

    public override UIImage Image { get { return UIImage.FromBundle("Instagram"); } }

    public override string Title { get { return "Instagram"; } }

    public override NSString Type { get { return new NSString("PostToInstagram"); } }

    public string Caption { get; set; }


    public override bool CanPerform(NSObject[] activityItems)
    {
      return UIApplication.SharedApplication.CanOpenUrl(NSUrl.FromString("instagram://app"));
    }

    public override void Prepare(NSObject[] activityItems)
    {
    }

    public override void Perform()
    {
      string filename = Path.Combine(FileSystemUtils.GetTemporaryDataPath(), "Image.igo");

      NSError err = null;
      using(var imgData = _ImageToShare.AsJPEG(JpgImageQuality))
      {
        if(imgData.Save(filename, false, out err))
        {
          Logger.Information("Instagram image saved as {FileName}", filename);
        }
        else
        {
          Logger.Error("Instagram image NOT saved as to path {FileName}. {Error}", filename, err.Description);
          Finished(false);
          return;
        }
      }

      var url = NSUrl.FromFilename(filename);
      _DocumentController = UIDocumentInteractionController.FromUrl(url);
      _DocumentController.DidEndSendingToApplication += (o, e) => Finished(true);
      _DocumentController.Uti = "com.instagram.exclusivegram";
      if(!string.IsNullOrEmpty(ShareCaption))
      {
        _DocumentController.Annotation = NSDictionary.FromObjectAndKey(new NSString(ShareCaption), new NSString("InstagramCaption"));
      }
      _DocumentController.PresentOpenInMenu(_Frame, _View, true);
    }

    UIImage _ImageToShare;
    CGRect _Frame;
    UIView _View;
    UIDocumentInteractionController _DocumentController;
  }
vava044
  • 51
  • 8
  • Post the code you are using to share your image. Sharing to Instagram should be the same as sharing to any other image service. – Patrick Tescher Oct 06 '16 at 00:09
  • @PatrickTescher I have added the code. It's in C#, I am using Xamarin. Also, I think the problem is with adding the text as well as media during the share (`activityItems array`). If I remove the caption everything works fine. Again iOS<10 issue only. – vava044 Oct 06 '16 at 16:14
  • Try to use this third party share activity for **instagram** [link](https://github.com/AquaSupport/AQSInstagramActivity) – Jayachandra A Aug 03 '17 at 14:00
  • @JayachandraA last commit 3 years ago) – Mike Glukhov Sep 21 '17 at 12:56

0 Answers0