52

我希望能够跟踪用户是否通过 facebook、twitter 等共享,但似乎无法知道选择了哪种方法。有没有?

4

4 回答 4

48

您可以在 setCompletionHandler 中使用活动类型

UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];

[activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) {
    if([activityType isEqualToString: UIActivityTypeMail]){
        NSLog(@"Mail");
    }
    if([activityType isEqualToString: UIActivityTypePostToFacebook]){
        NSLog(@"Facebook");
    }

}];
[self presentViewController:activityVC animated:TRUE completion:nil];

系统内置支持的内置活动类型。

NSString *const UIActivityTypePostToFacebook;

NSString *const UIActivityTypePostToTwitter;

NSString *const UIActivityTypePostToWeibo;

NSString *const UIActivityTypeMessage;

NSString *const UIActivityTypeMail;

NSString *const UIActivityTypePrint;

NSString *const UIActivityTypeCopyToPasteboard;

NSString *const UIActivityTypeAssignToContact;

NSString *const UIActivityTypeSaveToCameraRoll;

为 iOS 8 编辑

请注意,这将在 iOS 8 中生成编译器警告,您需要使用setCompletionWithItemsHandler方法而不是setCompletionHandler方法。

代替:

[activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) {

和:

[activityVC setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed,  NSArray *returnedItems, NSError *activityError) {
于 2013-01-05T02:28:30.847 回答
31

在 Swift 4 中:

activityViewController.completionWithItemsHandler = { activity, success, items, error in
        if !success{
            print("cancelled")
            return
        }

        if activity == .postToTwitter {
            print("twitter")
        }

        if activity == .mail {
            print("mail")
        }
    }
于 2015-11-28T02:11:05.617 回答
4

迅速 3

let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: [screenCapture], applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityType.print, UIActivityType.postToWeibo, UIActivityType.copyToPasteboard, UIActivityType.addToReadingList, UIActivityType.postToVimeo]

activityViewController.completionWithItemsHandler = { activity, success, items, error in
    if !success{
        print("cancelled")
        return
    }

    if activity == UIActivityType.postToFacebook {
        print("facebook")
    }

    if activity == UIActivityType.mail {
        print("mail")
    }

}

self.present(activityViewController, animated: true, completion: nil)
于 2017-08-09T21:41:15.630 回答
1

您可以使用此库https://github.com/rdougan/RDActivityViewController来识别已触及的活动,并仅通过实现其协议来按活动返回自定义数据。

像这样:

- (NSArray *)activityViewController:(NSArray *)activityViewController itemsForActivityType:(NSString *)activityType
{
    if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
        return @[@"Twitter text"];
    } else if ([activityType isEqualToString:UIActivityTypeMessage]) {
        return @[@"Message text"];
    } else if ([activityType isEqualToString:UIActivityTypeMail]) {
        return @[@"Mail text"];
    } else if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
        return @[@"Facebook text"];
    } else {
        return @[@"Default text"];
    }
}

我希望我对你有所帮助!

于 2013-04-11T12:52:28.137 回答