使用“社交”框架,当呈现显示所有常用社交媒体图标的模态 UIActivityViewController 时,有没有办法准确找出用户点击了哪个图标?意思是,如果他们选择 Twitter、Facebook、邮件、消息等?
我期待可能会在文档中看到此类的一些委托方法,但我什么也没看到。
有人有什么想法吗?
使用“社交”框架,当呈现显示所有常用社交媒体图标的模态 UIActivityViewController 时,有没有办法准确找出用户点击了哪个图标?意思是,如果他们选择 Twitter、Facebook、邮件、消息等?
我期待可能会在文档中看到此类的一些委托方法,但我什么也没看到。
有人有什么想法吗?
UIActivityViewController
有财产completionHandler
。_ 实现此处理程序以得到通知。
UIActivityViewController *avc = ...;
avc.completionHandler = ^(NSString *activityType, BOOL completed) {
if (completed) {
NSLog(@"The selected activity was %@", activityType);
}
};
斯威夫特 3
Swift 3 中的 typealiasUIActivityViewControllerCompletionWithItemsHandler
更改为
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
}
completionHandler
已弃用。使用completionWithItemsHandler
.
activityController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
NSLog(@"completionWithItemsHandler, activityType: %@, completed: %d, returnedItems: %@, activityError: %@", activityType, completed, returnedItems, activityError);
};
基于这个 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)")
}
}
以下代码段适用于我的情况:
[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.
}
}];