Default Sharing in iOS 7

前端 未结 6 1170
悲哀的现实
悲哀的现实 2020-12-23 11:23

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

6条回答
  •  臣服心动
    2020-12-23 12:01

    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 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. :)

提交回复
热议问题