UIActivityViewController - 检测发送与取消时的错误

时间:2013-08-03 04:21:58

标签: iphone ios objective-c

目前我正在使用以下代码来实例化UIActivityViewController:

NSArray *itemsToShare = [[NSArray alloc] initWithObjects:bodyMessage, nil];
        UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
        activityVC.excludedActivityTypes = [[NSArray alloc] initWithObjects: UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypePostToWeibo,UIActivityTypePostToFacebook,UIActivityTypePostToTwitter, nil];
        [activityVC setValue:subject forKey:@"subject"];
        [self presentViewController:activityVC animated:YES completion:nil];


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

             NSString *ServiceType;
             if ( [activityType isEqualToString:UIActivityTypeMail] )           ServiceType = @"Mail.";
             if ( [activityType isEqualToString:UIActivityTypeMessage] )  ServiceType = @"Messenger.";

             NSMutableString *shareWithActivityResultMessage;
             NSString *alertTitle;
             if (done)
             {
                 alertTitle = @"Success!";

                 shareWithActivityResultMessage = [NSMutableString stringWithString:successMessage];
                 [shareWithActivityResultMessage appendString:ServiceType];
             }
             else
             {
                 // didn't succeed.
                 alertTitle = @"Error";
                 shareWithActivityResultMessage = [NSMutableString stringWithString:errorMessage];
             }

当我取消撰写时,会触发ActivityVC done = false,从而触发错误消息。有没有办法检测取消而不是发送消息时的实际错误?

1 个答案:

答案 0 :(得分:2)

您可以在iOS 8中使用新的UIActivityViewControllerCompletionWithItemsHandler类型:

activityVC.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError){
    ...
}

只需检查activityError属性

即可