iOS share extension, I can't get the image data

I am sure this is trivial once someone kindly point me in the right direction so my apology for asking a silly question. However I have been searching for days I can't figure out what I am doing wrong.


Scenario: create a simple share extension that receive an image file


Problem: when I access the attachements, I get no data back.


What I have done:

1) added the same group to both app and app extension

2) made sure both have the group (1) in the entitlement

3) made sure both are using a certificate/app id with that group enabled

4) clean and rebuild several times to no avail.


The code:

- (void)didSelectPost {

/

for (NSExtensionItem *item in self.extensionContext.inputItems) {

for (NSItemProvider *itemProvider in item.attachments) {

if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {

[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {

if (error!=nil){

errorMsg = error.description;

}

photo = image;

}];

break;

}

}

}

NSLog(errorMsg);

.... and so on and so forth


I can place a break point and see that loadItemForTypeIdentifier get called but the photo object is always nil and so is the errorMsg. Help, please?

Answered by Solublepeter in 63703022

A common reason you may not see an image from a share extension is that the code in the block may not have finished as execution passes on to the next stage.


In my case I was trying to send the file in an email. I found my code worked with small images, like icons (that had loaded in time) but was blank with larger photos.


I posted the solution I used here, on Stack Overflow, if that is any help.
http://stackoverflow.com/questions/32456738/why-do-larger-images-not-appear-in-a-share-extension-or-action-extension-solve/32456739#32456739

I can place a break point and see that loadItemForTypeIdentifier get called but the photo object is always nil and so is the errorMsg.

The method's being called or not and the block being invoked or not are other things. Have you put a break point inside the completionHandler and checked the values of image and error?

Good point. No, I never hit this line if I put a breakpoint


photo = image;


Not sure what that means since the first IF statement tells me it can find the content type kUTTypeImage...


As I said, I am at a losss here. Thanks!

Well, if the method loadItemForTypeIdentifier:options:completionHandler: is actually called, the completionHanlder must be invoked even in case of errors.


Debuggin extension needs quite a procedure, have you done it all right?

Debug, Profile, and Test Your App Extension

I believe I am debugging correctly.

I can debug with or without a device, I can see my extension, I can send it an image, I can hit the breakpoints I mentioned but I never get the image data. I know it is a head scratcher, that is why I posted for help 😉

Without an actual code reprodusing the issue, I cannot say anything definate, so just a few.


- Your code seems to work, except that the completionHandler terminates with `photo = image;`


- Checking the value of errorMsg just after calling loadItemForTypeIdentifier:options:completionHandler: is meaningless.

At that point, the completion handler may very probably not invoked yet.


- Find a reliable way for debugging. As you know, breakpoint may not be functional except in a certain condition.

While I was developing my share extension, NSLog was a more reliable for debugging, though I'm not sure this applies to you.

Accepted Answer

A common reason you may not see an image from a share extension is that the code in the block may not have finished as execution passes on to the next stage.


In my case I was trying to send the file in an email. I found my code worked with small images, like icons (that had loaded in time) but was blank with larger photos.


I posted the solution I used here, on Stack Overflow, if that is any help.
http://stackoverflow.com/questions/32456738/why-do-larger-images-not-appear-in-a-share-extension-or-action-extension-solve/32456739#32456739

*facepalm* I should have though of it, it is indeed a race condition. I re-wrote the code inspired by your solution and sure enough after several hundred millis I get a call back and I get the bytes. THANK YOU!

Thank you! I did confirm that the async code for the image loading was not getting called (see below).

iOS share extension, I can't get the image data
 
 
Q