确定使用了哪个共享扩展

时间:2014-09-23 15:54:56

标签: ios cocoa-touch ios8 uiactivityviewcontroller

由于iOS 8中已弃用completionHandler UIActivityViewController,是否有任何方法可以确定用户使用的共享扩展/活动?

3 个答案:

答案 0 :(得分:14)

您只需使用新处理程序 UIActivityViewControllerCompletionWithItemsHandler

typedef void (^UIActivityViewControllerCompletionWithItemsHandler)(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError);

这样称呼:

[yourActivityVC setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError){

}];

此外,如果您想知道选择了哪个项目,您只需要确保您的活动项目符合UIActivityItemSource协议;

UIActivityItemSource Protocol Documentation

答案 1 :(得分:1)

setCompletionWithItemsHandler仅限iOS8。如果您需要支持iOS6-7,那么:

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

}];

答案 2 :(得分:1)

这就是我们在Swift中使用的:

    ...

    // Configure UIActivityViewController
    let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivityTypeAirDrop,
        UIActivityTypeAddToReadingList,
        UIActivityTypeAssignToContact,
        UIActivityTypePrint,
        UIActivityTypeCopyToPasteboard]

    // Show UIActivityViewController
    presentViewController(activityViewController, animated: true, completion: nil)

    // Define completion handler
    activityViewController.completionWithItemsHandler = doneSharingHandler

    ...

func doneSharingHandler(activityType: String?, completed: Bool, returnedItems: [AnyObject]?, error: NSError?) {
    // Return if cancelled
    if (!completed) {
        return
    }

    // If here, log which activity occurred
    println("Shared video activity: \(activityType)")
}