Reputation: 1643
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.
Upvotes: 3
Views: 10310
Reputation: 1362
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
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.
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
Upvotes: 5