Default Sharing in iOS 7

IosIos7ShareSocial NetworkingAirdrop

Ios Problem Overview


I have seen this format (Image shown below) of share option in most of the iOS applications that support iOS 7. Is there a default code/framework available to implement this share option as it is shown in the image below?

Ios Solutions


Solution 1 - Ios

What you are looking for is the UIActivityViewController.

Since you asked a general question I can't do more than give you a link to the documentation

Solution 2 - Ios

In addition to the accepted answer, a small piece of example code

- (void)shareText:(NSString *)text andImage:(UIImage *)image andUrl:(NSURL *)url
    {
        NSMutableArray *sharingItems = [NSMutableArray new];
        if (text) {
            [sharingItems addObject:text];
        }
        if (image) {
            [sharingItems addObject:image];
        }
        if (url) {
            [sharingItems addObject:url];
        }
        UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
        [self presentViewController:activityController animated:YES completion:nil];
    }

Call shareText, leave the things that you don't want to share at nil.

[self shareText:@"Hello world" andImage:nil andUrl:nil];

Solution 3 - Ios

The Controller in the image you posted is the UIActivitiyViewController this is a link to the class documentation

Solution 4 - Ios

some good example code: https://stackoverflow.com/questions/13498459/how-to-display-the-default-ios-6-share-action-sheet-with-available-share-options

I know this question is particular to iOS 7, and the code example specifies iOS 6, but AFAICT they are very similar one might find the example code as helpful as I did.

Solution 5 - Ios

UIActivityViewController is what you are looking for.

You can specify either the items or the applications

UIActivityViewController *actCont = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

Solution 6 - Ios

Just use following code for Default Sharing. You can able to add more items into shareItems array as per your requirement.

NSMutableArray *shareItems = [[NSMutableArray alloc] initWithObjects: 
                                 @"Hello", 
                                 [UIImage imageNamed:@"your_image.png"], 
                                 @"http://google.com/", nil];
[self shareItemToOtherApp:shareItems];

Following method is for default sharing Text or Image into other Apps:-

-(void)shareItemToOtherApp:(NSMutableArray *)shareItems{
    UIActivityViewController *shareController = [[UIActivityViewController alloc]
                                                 initWithActivityItems: shareItems applicationActivities :nil];
    
    [shareController setValue:@"Sharing" forKey:@"subject"];
    shareController.excludedActivityTypes = @[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll];
    
    shareController.completionHandler = ^(NSString *activityType, BOOL completed)
    {
        //NSLog(@" activityType: %@", activityType);
        //NSLog(@" completed: %i", completed);
    };
    
    [self presentViewController: shareController animated: YES completion: nil];
}

If you want to make Custom Sharing sheet then use following code. For this, you have to import <Social/Social.h> framework.

-(void)shareOnFacebook:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController *faceSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        // NSLog(@"%@", messageField.text);//This returns the appropriate string
        [faceSheet setInitialText:@"Hellooooooo"];
        //The facebook VC appears, but initial text is not set to messageField.text
        [self presentViewController:faceSheet animated:YES completion:nil];
    }
    else
    {
        NSLog(@"Please first install Application and login in Facebook");
    }
}

-(void)shareOnTwitter:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:@"Hello"];
        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
    else{
        NSLog(@"Please first install Application and login in Twitter");
    }
}

Hope, this is what you're looking for. Any concern get back to me. :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSpidyView Question on Stackoverflow
Solution 1 - IosAbizernView Answer on Stackoverflow
Solution 2 - Iossjaak bakkerView Answer on Stackoverflow
Solution 3 - IosoiledCodeView Answer on Stackoverflow
Solution 4 - IosGraehamFView Answer on Stackoverflow
Solution 5 - IosSumit utrejaView Answer on Stackoverflow
Solution 6 - IosMeet DoshiView Answer on Stackoverflow