Hi i am developing one social network app. In that I required to share the image using extension to my app API. I am developing my app by objective C not Swift. Can any body help me to solve this problem.
-
Can you say how you getting the image? – Yohan Apr 23 '15 at 09:29
1 Answers
Creating a Share Extension in objective C
App extension must have a containing app - you can't just create an app extension to be downloaded from the store, first create a regular app to contain the app extension. For the sake of this demonstration just create a new single view project and leave it untouched. Go to File->New->Project and select Single view application under iOS -> Applications call it 'ExtendableApp'.
Go to File->New->Target and select Share Extension under iOS -> Application Extensions call it 'myShareExtension' this will add the share Extension target to your project.
The extension ShareViewController inherit from SLComposeServiceViewController which already has a View with a Textbox, imageview and 'Cancel' and 'Post' buttons and some other features like character count, configuration, content validation.
If you want to create your custom experience simply set your ShareViewController to inherit from UIViewController, Once your extension is activated all the regular viewDidLoad, viewDidAppear, etc will be called.
At this point after installing your containing app you will already by able to see 'myShareExtension' in UIActivityViewController menu
Get the shared UIImage
In your ShareViewController.mm in viewDidAppear use the following to get the image
-(void)viewDidAppear:(BOOL)animated
{
for (NSItemProvider* itemProvider in ((NSExtensionItem*)self.extensionContext.inputItems[0]).attachments )
{
if([itemProvider hasItemConformingToTypeIdentifier:@"public.image"])
{
[itemProvider loadItemForTypeIdentifier:@"public.image" options:nil completionHandler:
^(id<NSSecureCoding> item, NSError *error)
{
UIImage *sharedImage = nil;
if([(NSObject*)item isKindOfClass:[NSURL class]])
{
sharedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:(NSURL*)item]];
}
if([(NSObject*)item isKindOfClass:[UIImage class]])
{
sharedImage = (UIImage*)item;
}
}];
}
}
}
Note - This code is only for demonstration, extensions should be quick and lightweight and not block the UI thread while loading an image, in real application you would do this in the background.
Specify when the extension shows up
by default the extension will now show up whenever the UIActivityViewController menu appears, to specify in which scenarios the extension should appear you need to set the proper values in the extension info.plist under NSExtension, NSExtensionAttributes, NSExtensionActivationRule You can find a decumentation of the available keys here: Information Property List Key Reference
Note that the default behavior is for your extension to appear whenever all of the keys apply, that means that if you specify NSExtensionActivationSupportsImageWithMaxCount
and NSExtensionActivationSupportsMovieWithMaxCount
your extension will appear only when the user is sharing both image And movie not image or movie.
To write an extension which appears for either one of a few shared data types look here
http://bryan.io/post/97658826431/what-we-learned-building-the-tumblr-ios-share-extension
Declaring Supported Data Types for a Share or Action Extension
-
1I got the image and all. Now I need to share the image in background. Can you provide any code for this? – TamilKing Nov 18 '14 at 10:53
-
I don't understand, from you question it seems you are developing a share extension so why would you want to share an image that was already shared with you ? And regardless of context the only way to use a share extension is through UIActivityViewController and it requires presenting this controller so it cant be done in the background. – MichaelB Nov 19 '14 at 05:55
-
I add all the thing and also sharing.. Some time it not sharing.. I need to call two api for upload the image. I hope did mistake in handling in background session. – TamilKing Nov 19 '14 at 11:37
-
Hi @TamilKing, I am doing the same thing in my project but I need to encrypt the image data into base64 format before I upload the image. Could you please let me know if you did encryption while uploading? – Md Rais Jul 04 '17 at 10:02
-
Is this at all possible with Swift? My itemProvider callback gets an `NSURL`. When attempting to copy the file from that url to my apps documents folder I get an exception that the file does not exist. – Chris Feb 25 '19 at 04:54