如何处理 iOS 中的 UIActivityController 类型的点击?

How to handle click of types of UIActivityController in iOS?

在我的应用程序中想要处理 UIActivityTypePostToFacebook 的点击。 那么如何实现这个点击事件呢。 请检查我的代码: #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

     NSString *aText = @"Google";
     UIImage *aPhoto = [UIImage imageNamed:@"3.png"];
     NSURL *website = [NSURL URLWithString:@"http://www.google.com/"];



     NSArray *objectsToShare = @[aText, aPhoto, website];

     NSArray * applicationActivities = nil;

     UIActivityViewController * activityViewController = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:applicationActivities];

     activityViewController.excludedActivityTypes = @[
                                                      UIActivityTypePostToWeibo,
                                                      UIActivityTypeAssignToContact,
                                                      UIActivityTypeAirDrop,
                                                      UIActivityTypeAddToReadingList,
                                                      UIActivityTypeCopyToPasteboard,
                                                      UIActivityTypeSaveToCameraRoll,
                                                      UIActivityTypePrint,
                                                      UIActivityTypePostToFlickr,
                                                      UIActivityTypePostToTencentWeibo,

                                                      // Whatever you want to exclude
                                                      ];

     if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){

         activityViewController.popoverPresentationController.sourceView =
        self.view;
     }
     if(isiPhone)
     {
        [self presentViewController:activityViewController animated:YES completion:nil];

     }
     else{
         UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
         [popup presentPopoverFromRect:CGRectMake(0, screenheight, 768, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
     }
activityViewController.excludedActivityTypes = @[
                                                  UIActivityTypePostToWeibo,
                                                  UIActivityTypeAssignToContact,
                                                  UIActivityTypeAirDrop,
                                                  UIActivityTypeAddToReadingList,
                                                  UIActivityTypeCopyToPasteboard,
                                                  UIActivityTypeSaveToCameraRoll,
                                                  UIActivityTypePrint,
                                                  UIActivityTypePostToFlickr,
                                                  UIActivityTypePostToTencentWeibo,


UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, 

                                                  // Whatever you want to exclude
                                                  ];  


  if(isiPhone)
 {
    [self presentViewController:activityViewController animated:YES completion:nil];

 }
 else{
     UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
     [popup presentPopoverFromRect:CGRectMake(0, screenheight, 768, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
 }


[activityViewController setCompletionHandler:^(NSString *act, BOOL done)
 {
 NSString *ServiceMsg = nil;
 if ( [act isEqualToString:UIActivityTypeMail] )           ServiceMsg = @"Mail sended!";
 if ( [act isEqualToString:UIActivityTypePostToTwitter] )  ServiceMsg = @"Post on twitter, ok!";
 if ( [act isEqualToString:UIActivityTypePostToFacebook] ) ServiceMsg = @"Post on facebook, ok!";
 if ( [act isEqualToString:UIActivityTypeMessage] )        ServiceMsg = @"SMS sended!";
 if ( done )
 {
   UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:ServiceMsg message:@"" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
   [Alert show];

 }
 }];

选择-2

如果以上选择效果不佳,请尝试选择 2,在您的 .h 文件中添加 UIActivityItemSource

然后在您的 .m 文件中调用以下方法

 - (id) activityViewController:(UIActivityViewController *)activityViewController
      itemForActivityType:(NSString *)activityType
{
 if ( [activityType isEqualToString:UIActivityTypePostToTwitter] )
return @"This is a #twitter post!";
if ( [activityType isEqualToString:UIActivityTypePostToFacebook] )
return @"This is a facebook post!";
 if ( [activityType isEqualToString:UIActivityTypeMessage] )
return @"SMS message text";
 if ( [activityType isEqualToString:UIActivityTypeMail] )
return @"Email text here!";
 if ( [activityType isEqualToString:@"it.albertopasca.myApp"] )
return @"OpenMyapp custom text";
 return nil;
 }