弄清楚在UIActivityViewController中点击了哪个图标

时间:2013-05-22 00:53:37

标签: ios objective-c uiactivityviewcontroller

使用“社交”框架,在展示显示所有常用社交媒体图标的模式UIActivityViewController时,有没有办法找出用户点击的确切图标?意思是,如果他们选择Twitter,Facebook,Mail,Message等?

我希望在Docs中可能会看到这个类的一些委托方法,但我什么都没看到。

有人有任何想法吗?

5 个答案:

答案 0 :(得分:17)

UIActivityViewController具有completionHandler属性。实现此处理程序以进行通知。

UIActivityViewController *avc = ...;
avc.completionHandler = ^(NSString *activityType, BOOL completed) {
    if (completed) {
        NSLog(@"The selected activity was %@", activityType);
    }
};

答案 1 :(得分:4)

completionHandler已弃用。使用completionWithItemsHandler

activityController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
    NSLog(@"completionWithItemsHandler, activityType: %@, completed: %d, returnedItems: %@, activityError: %@", activityType, completed, returnedItems, activityError);
};

答案 2 :(得分:3)

根据这个SO回答:https://stackoverflow.com/a/34581940/2177085

这还包括通过WhatsApp或Gmail检查用户是否共享。您可以打印activityType以添加其他类型。

let activityViewController = UIActivityViewController(activityItems: [finalMsg as NSString], applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)

activityViewController.completionWithItemsHandler = {(activityType, completed:Bool, returnedItems:[AnyObject]?, error: NSError?) in

// Return if cancelled
if (!completed) {
    print("user clicked cancel")
    return
}

if activityType == UIActivityTypeMail {
    print("share throgh mail")
}
else if activityType == UIActivityTypeMessage {
    print("share trhought Message IOS")
}
else if activityType == UIActivityTypePostToTwitter {
    print("posted to twitter")
}
else if activityType == UIActivityTypePostToFacebook {
    print("posted to facebook")
}
else if activityType == UIActivityTypeCopyToPasteboard {
    print("copied to clipboard")
}
else if activityType! == "net.whatsapp.WhatsApp.ShareExtension" {
    print("activity type is whatsapp")
}
else if activityType! == "com.google.Gmail.ShareExtension" {
    print("activity type is Gmail")
}
else {
    // You can add this activity type after getting the value from console for other apps.
    print("activity type is: \(activityType)")
}
}

答案 3 :(得分:2)

以下代码段在我的案例中有效:

[activityController setCompletionHandler:^(NSString *act, BOOL done)
{

   NSLog(@"act type %@",act);
   NSString *ServiceMsg = nil;
   if ( [act isEqualToString:UIActivityTypeMail] )
     ServiceMsg = @"Mail sent";

   if ( [act isEqualToString:UIActivityTypePostToTwitter] )
     ServiceMsg = @"Post on twitter, ok!";

   if ( [act isEqualToString:UIActivityTypePostToFacebook] ) 
     ServiceMsg = @"Post on facebook, ok!";

   if ( done )
   {
      UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:ServiceMsg message:@"" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
      [Alert show];
      [Alert release];
   }
   else
   {
      // didn't succeed. 
   }
}];

答案 4 :(得分:2)

Swift 3

Swift 3中的typealias UIActivityViewControllerCompletionWithItemsHandler更改为

public typealias UIActivityViewControllerCompletionWithItemsHandler = (UIActivityType?, Bool, [Any]?, Error?) -> Swift.Void

您可以像这样使用completionWithItemsHandler。

activityViewController.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems:[Any]?, error: Error?) in
    //Do whatever you want
}