Forum Xamarin.Forms

How can I share an image to instagram in iOS?

I only open instagram, but I want to share the photo of the uri

path = http://pic3.zhimg.com/8161ba9638273e0fb1a0201789d22d8e_m.jpg

public string ObtainURL(string path)
        {
            var imageURL = FromUrl(path);
            var returnMessage = "";

            try
            {
                NSString documentDirectory = (new NSString(Environment.GetFolderPath(Environment.SpecialFolder.Personal)));
                //string saveImagePath = documentDirectory.AppendPathComponent(new NSString(@"Image.igo"));
                string saveImagePath = documentDirectory.AppendPathComponent(new NSString(@"Image.ig"));
                NSData imageData = imageURL.AsPNG();
                imageData.Save(saveImagePath, true);
                NSUrl imageURLfinal = NSUrl.FromFilename(saveImagePath);
                NSUrl instagramURL = NSUrl.FromString("instagram://app");

                if (UIApplication.SharedApplication.OpenUrl(instagramURL)) //check for App is install or not
                {
                    var DocumentController = UIDocumentInteractionController.FromUrl(imageURLfinal);
                    var dicDel = new UIDocumentInteractionControllerDelegateClass();
                    DocumentController.Delegate = dicDel;
                    DocumentController.Uti = "com.instagram.photo";
                    //DocumentController.Delegate = new IUIDocumentInteractionControllerDelegate(this);
                    //DocumentController.Uti = "com.instagram.exlusivegram";

                    CGRect rect = new CGRect(0, 0, 612, 612);

                    DocumentController.PresentOpenInMenu(new RectangleF(1, 1, 1, 1), View, true);

                    DocumentController.PresentOpenInMenu(rect, this.View, true);

                    InvokeOnMainThread(delegate {
                        DocumentController.PresentOpenInMenu(new RectangleF(1, 1, 1, 1), View, true);
                        DocumentController.PresentOpenInMenu(rect, this.View, true);
                    });

                    returnMessage = "NSURL: " + imageURLfinal;
                }
                else
                {
                    returnMessage = "Instagram Not Found";
                }
            }
            catch (Exception ex)
            {
                returnMessage = ex.Message;
            }

            //return imageURL.ToString()+" NSURL: "+ imageURLfinal;
            return returnMessage;
        }

--

public static UIImage FromUrl(string uri)
        {
            var imageName = uri.Substring(uri.LastIndexOf('/') + 1);

            if (File.Exists(Path.Combine(NativeImagesPath, imageName)))
                return UIImage.FromFile(Path.Combine(NativeImagesPath, imageName));

            if (File.Exists(Path.Combine(ImagesCachePath, imageName)))
                return UIImage.FromFile(Path.Combine(ImagesCachePath, imageName));

            if (Items.ContainsKey(uri))
                return Items[uri];

            using (var url = new NSUrl(uri))
            using (var data = NSData.FromUrl(url))
            {
                var image = UIImage.LoadFromData(data);

                if (!NSFileManager.DefaultManager.FileExists(ImagesCachePath))
                    NSFileManager.DefaultManager.CreateDirectory(ImagesCachePath, false, null);

                var result = NSFileManager.DefaultManager.CreateFile(Path.Combine(ImagesCachePath, imageName), data, new NSFileAttributes());
                if (!result)
                    Items[uri] = image;

                return image;
            }
        }

--

public class UIDocumentInteractionControllerDelegateClass : UIDocumentInteractionControllerDelegate
        {

            UIViewController viewC;

            public UIDocumentInteractionControllerDelegateClass(UIViewController controller)
            {
                viewC = controller;
            }

            public UIDocumentInteractionControllerDelegateClass()
            {
            }

            public override UIViewController ViewControllerForPreview(UIDocumentInteractionController controller)
            {
                return viewC;
            }

            public override UIView ViewForPreview(UIDocumentInteractionController controller)
            {
                return viewC.View;
            }


            public override CGRect RectangleForPreview(UIDocumentInteractionController controller)
            {
                return viewC.View.Frame;
            }          

        }

Best Answer

  • danielparedes97danielparedes97 ✭✭✭
    edited August 2019 Accepted Answer

    I got to share with this code

    var viewer = UIDocumentInteractionController.FromUrl(NSUrl.FromFilename(saveImagePath));
    viewer.Url = imageURLsaved;
    viewer.Uti = "com.instagram.photo";
    var controller = GetVisibleViewController();
    viewer.PresentOpenInMenu(controller.View.Frame, controller.View, true);
    

    I change the image to share, for another with https

    private UIViewController GetVisibleViewController(UIViewController controller = null)
            {
                controller = controller ?? UIApplication.SharedApplication.KeyWindow.RootViewController;
    
                if (controller.PresentedViewController == null)
                    return controller;
    
                if (controller.PresentedViewController is UINavigationController)
                {
                    return ((UINavigationController)controller.PresentedViewController).VisibleViewController;
                }
    
                if (controller.PresentedViewController is UITabBarController)
                {
                    return ((UITabBarController)controller.PresentedViewController).SelectedViewController;
                }
    
    
    
                return GetVisibleViewController(controller.PresentedViewController);
            }
    
    • I save the image as "Image.ig" and I've tried to change the uti to "com.instagram.exclusivegram" and the image extension for "Image.igo"

    The only problem is that I get all the options to share, and I just want only instagram option

    How can I do that?

    Can someone help me?

Answers

  • batmacibatmaci DEMember ✭✭✭✭✭

    not possible. instagram doesnt have an api end point for that. you can only open instagram app and insert through the app.

  • ColeXColeX Member, Xamarin Team Xamurai

    Use FFImageLoading to load the image from internet and then share to to instagram.

  • danielparedes97danielparedes97 Member ✭✭✭
    edited August 2019

    @ColeX I'm trying to use FFImageLoading, but my UIImageView always is null

    UIImageView imaview = null;
    ImageService.Instance.LoadUrl(path).Into(imaview);
    

    And then I do...

    NSData imageData = imaview.Image.AsPNG();
    imageData.Save(saveImagePath, true);
    

    But nothing... :'(

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭

    Your question belongs to iOS forums, not Forms.

  • ColeXColeX Member, Xamarin Team Xamurai
    edited August 2019

    After iOS 9 , for security apple only allows Https request , change your image url to a Https url , or do something to make your application support Http request ,refer https://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http

  • danielparedes97danielparedes97 Member ✭✭✭
    edited August 2019 Accepted Answer

    I got to share with this code

    var viewer = UIDocumentInteractionController.FromUrl(NSUrl.FromFilename(saveImagePath));
    viewer.Url = imageURLsaved;
    viewer.Uti = "com.instagram.photo";
    var controller = GetVisibleViewController();
    viewer.PresentOpenInMenu(controller.View.Frame, controller.View, true);
    

    I change the image to share, for another with https

    private UIViewController GetVisibleViewController(UIViewController controller = null)
            {
                controller = controller ?? UIApplication.SharedApplication.KeyWindow.RootViewController;
    
                if (controller.PresentedViewController == null)
                    return controller;
    
                if (controller.PresentedViewController is UINavigationController)
                {
                    return ((UINavigationController)controller.PresentedViewController).VisibleViewController;
                }
    
                if (controller.PresentedViewController is UITabBarController)
                {
                    return ((UITabBarController)controller.PresentedViewController).SelectedViewController;
                }
    
    
    
                return GetVisibleViewController(controller.PresentedViewController);
            }
    
    • I save the image as "Image.ig" and I've tried to change the uti to "com.instagram.exclusivegram" and the image extension for "Image.igo"

    The only problem is that I get all the options to share, and I just want only instagram option

    How can I do that?

    Can someone help me?

  • ColeXColeX Member, Xamarin Team Xamurai

    The only problem is that I get all the options to share, and I just want only instagram option

    Just save you image file in format ".igo" file type extension , it should be able to show Instagram only in share menu .

    Refer https://stackoverflow.com/a/26833432/8187800

  • danielparedes97danielparedes97 Member ✭✭✭
    edited August 2019

    @ColeX

    If I use the extension class "ig" and the type "com.instagram.photo":

    • There are many more options to share (Gmail, Telegram, Drive, Drive copy...) and about Instagram, there are two options (Instagram and Instagram copy)

    If I specify the extension class "igo" and the type "com.instagram.exclusivegram":

    • There are fewer options to share (Gmail, Telegram, Drive copy...) and about Instagram, there is only one option (Instagram copy)

    Instagram copy is my correct option, but I want to disappear the rest of the applications of the list to share (Gamil, Telegram...)

    How Can I do that?

  • ashiishtashiisht INMember ✭✭

    Hi @ColeX & @danielparedes97

    I am also facing the same issue, i want to show only the instagram app to select and if i am going with the ".igo" extension then it not showing instagram app in the options list.

    Did you found any work around on this.

    Thank you.

Sign In or Register to comment.