UIActivityViewController为Twitter和Facebook发布不同的图像和文本

时间:2015-04-10 10:30:47

标签: ios objective-c facebook share uiactivityviewcontroller

在我的项目中,我使用UIActivityViewController进行Facebook,Twitter和电子邮件共享。我想分享Facebook,电子邮件和Twitter的单独文本和图像。我该怎么做?

-(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{

    if ( [activityType isEqualToString:UIActivityTypePostToTwitter] )
        return _shareText1;
    if ( [activityType isEqualToString:UIActivityTypePostToFacebook] )
        return _shareText2;
    if ( [activityType isEqualToString:UIActivityTypeMail] )
        return _shareText3;

    return nil;


}

我在UIActivityItemProvider的子类中使用了上面的代码,并传递了要从我的UIViewcontroller共享的文本。如何返回带有文本的图像?是否返回{{1如果是这样,有什么关键?请帮助。

1 个答案:

答案 0 :(得分:3)

您可以创建UIActivity Subclass并在UIActivityViewController中使用的最佳方式..

-(void)sharePressed:(id)sender
{  

 GooglePlus *gPlus = [[GooglePlus alloc]init];

 FacebookShare *fb =[[FacebookShare alloc]init];

NSString *textToShare = @"Text u want to share";

NSURL *myWebsite = [NSURL      URLWithString:@"http://www.mywebsite.com/"];

UIImage *image =[UIImage imageNamed:@"apple.png"];
}

NSArray *objectsToShare = @[textToShare, myWebsite,image];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:    


[NSArray arrayWithObjects:gPlus,fb,nil]];
activityVC.completionHandler = ^(NSString *activityType, BOOL completed)
{
    //  NSLog(@" activityType: %@", activityType);

    //  NSLog(@" completed: %i", completed);

};


NSArray *excludedActivities = @[UIActivityTypePostToTwitter,
                                UIActivityTypePostToWeibo,
                                UIActivityTypeMessage,
                                UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
                                UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
                                UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
                                UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo,UIActivityTypePostToFacebook];
activityVC.excludedActivityTypes = excludedActivities;
[self presentViewController:activityVC animated:YES completion:nil];


// Create the subclass the UIActivity Class using delegates 

-(NSString *)activityType
{


return @"GooglePlus";

//CustomActivity 
}


-(NSString *)activityTitle
{


 return @"GooglePlus ";

// use your custom ActivityTitle  
}

-(UIImage *)_activityImage
{


  // Note: These images need to have a transparent background and I recommend these sizes:
  // iPadShare@2x should be 126 px, iPadShare should be 53 px, iPhoneShare@2x should be 100
  // px, and iPhoneShare should be 50 px. I found these sizes to work for what I was making.


    return [UIImage imageNamed:@"G+-60x60.png"];

}

-(BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{

NSLog(@"%s", __FUNCTION__);

return YES;
}

-(void)prepareWithActivityItems:(NSArray *)activityItems
{

NSLog(@"%s",__FUNCTION__);
}

-(UIViewController *)activityViewCtroller
{

 NSLog(@"%s",__FUNCTION__);

return nil;
}

-(void)performActivity
{

  // share what stuff u want to share 

}