Forum Xamarin.iOS
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

NSExtensionItem to byte[]

kenneth.leekenneth.lee USMember ✭✭✭

Hi everyone,

I'm trying to write a share Extension that can take any file and upload it. I did this on my main app by uploading a byte[] and would like to replicate it in my Share extension. Reading the documentation did little help and I'm still unclear as to what I need to go to get the byte[] of the input.

Any help would be greatly appreciated
Thanks

Posts

  • kenneth.leekenneth.lee USMember ✭✭✭

    I think i got it i got to use loaditem and get the NSData on completionHandler and call GetArray on the NSData.

    The swift code looks something like

            // Request document data
            [itemProvider loadItemForTypeIdentifier:MyDocumentUTI options:nil
                completionHandler:^(NSData *data, NSError *error) {
                if (data) self.contents = data;
            }]; 
    
  • kenneth.leekenneth.lee USMember ✭✭✭

    So far i got this

            foreach (NSItemProvider itemProvider in this.ExtensionContext.InputItems[0].Attachments)
            {
                if (itemProvider.HasItemConformingTo("public.jpeg"))
                {
                    itemProvider.LoadItem("public.jpeg", null, (item, error) =>
                    {
                        if (item != null)
                        {
                            uploadData = (NSData.FromUrl((NSUrl)item).ToArray());
                            filename = "filename";
                        }
                        /*
                        UIImage sharedImage = null;
                        if (item is NSUrl)
                        {
                            sharedImage = UIImage.LoadFromData(NSData.FromUrl((NSUrl)item));
                            uploadData.Add(NSData.FromUrl((NSUrl)item).ToArray());
                        }
    
                        if (item is UIImage)
                        {
                            sharedImage = (UIImage)item;
                            uploadData.Add(NSData.FromObject());
                        }
                        */
                    });
                }
            }
    

    However since LoadItem does a callback it doesn't set uploadData when i need it and I need to figure a way to wait for the callback.

    I've tried a while loop to wait for uploadData to be set but it seems to wait indefinitely and would be a pretty bad solution if somehow it doesn't set uploadData and blocks the user.

Sign In or Register to comment.