简体   繁体   中英

How to detect which activity has been selected and how to check if message has been sent?

I've got code for simple sharing some messages from app.

UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
                                                    initWithActivityItems:@[message]
                                                    applicationActivities:nil];

activityViewController.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityViewController animated:YES completion:nil];

Questions:

  1. It is possible to detect which sharing option has been selected?
  2. It is possible to detect if message was shared or canceled before?

Thank you in advance.

Here is a sample of how to use UIActivityViewControllerCompletionHandler :

UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
                                                    initWithActivityItems:@[@"abc", someImage]
                                                    applicationActivities:nil];

[activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed) {
    NSLog(@"%@, %d", activityType, completed);
}];

activityViewController.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityViewController animated:YES completion:nil];

您需要实现UIActivityViewControllerCompletionHandler ,您可以在其中查看用户执行的操作或是否已取消。

In iOS8, the completionHandler property is deprecated. Use the completionWithItemsHandler property instead:

activityViewController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
NSLog(@"completed    = '%@'", (completed) ? @"YES" : @"NO");
NSLog(@"activityType = '%@'", activityType);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM