Sharing image with share extension in ios8

Hi I am developing one social network application. In this I have to share an image using an extension for my app API. I am developing my app with Objective C not Swift . Can any body help me to solve this problem.

+3


source to share


1 answer


Creating a share extension in object C

  • An app extension must have a containing app - you can't just create an app extension to be downloaded from the store, create a regular app first to contain an app extension. For the sake of this demo, just create a new project with one view and leave it untouched. Go to File-> New-> Project and select Single View App in iOS -> Applications , name it "ExtendableApp".

  • Go to File-> New-> Target and select Extend Extension in iOS - > App Extensions , name it 'myShareExtension' this will add an extension target for your project.

  • The ShareViewController extension inherits from SLComposeServiceViewController, which already has a view with text fields, images and buttons "Cancel" and "Mail" and some other functions such as number of characters, configuration, content checking.

    If you want to create your own experience, just set your ShareViewController to inherit from UIViewController. After activating your extension, all normal viewDidLoad, viewDidAppear, etc. will be called.

At this point, after installing your application, you can already see 'myShareExtension' in the UIActivityViewController menu

Get generic 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 for demonstration only, extensions should be fast and lightweight, not block the UI thread when the image is loaded, in a real application you will do this in the background.

Specify when the extension is displayed

by default the extension will be displayed whenever the UIActivityViewController menu appears, to indicate in which scripts the extension should appear, you need to set the correct values ​​in the extension info.plist in the NSExtension, NSExtensionAttributes, NSExtensionActivationRule You can find the available keys here: Reference by the list of information properties

Note that the default behavior for your extension should appear whenever all is applied , which means that if you specify NSExtensionActivationSupportsImageWithMaxCount

and NSExtensionActivationSupportsMovieWithMaxCount

, your extension will only appear when the user is sharing an image and , not an image or movie. To write an extension that appears for one of several common data types, see 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

+5


source







All Articles