9

在我的 UIActivityViewController 中,我使用完成处理程序来执行“成功共享”通知。它可以工作,但我唯一的问题是,如果用户按下取消,它仍然会显示通知。

这是我的完成处理程序代码,

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


    CWStatusBarNotification *notification = [CWStatusBarNotification new];
    [notification displayNotificationWithMessage:@"✓ Successfully Shared Centre!"
                                          forDuration:3.0f];

    notification.notificationLabelBackgroundColor = [UIColor colorWithRed:38.0f/255.0f green:81.0f/255.0f blue:123.0f/255.0f alpha:1.0f];
    notification.notificationLabelTextColor = [UIColor whiteColor];



}];

谢谢您的帮助!

4

8 回答 8

19

注意:completionHandler 属性在 iOS8 中已弃用,因此无法再知道共享操作的结果。 https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/1622010-completionhandler

更新:就像 adruzh 所说,在 iOS8 上,Apple 忘记在文档中提到一个新的 completionHandler:

[activityController setCompletionWithItemsHandler:
    ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
}];

https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/1622022-completionwithitemshandler

于 2014-08-14T09:54:23.110 回答
12

这就是completed论证的目的:

[controller setCompletionHandler:^(NSString *activityType, BOOL completed) {
    if (!completed) return;

    CWStatusBarNotification *notification = [CWStatusBarNotification new];
    [notification displayNotificationWithMessage:@"✓ Successfully Shared Centre!"
                                     forDuration:3.0f];

    notification.notificationLabelBackgroundColor = [UIColor colorWithRed:38.0f/255.0f green:81.0f/255.0f blue:123.0f/255.0f alpha:1.0f];
    notification.notificationLabelTextColor = [UIColor whiteColor];
}];
于 2014-02-28T00:51:43.353 回答
4

对于 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)")
}
于 2015-07-05T09:35:13.700 回答
3

Swift 5 - 下面的函数涵盖了大部分 UIActivityViewController 属性。它对我有用,并认为它可能对你们也有帮助。

func performShareAction() {
        let itemsToShare : [Any] = ["Hello World"]
        let activityView = UIActivityViewController(activityItems: itemsToShare, applicationActivities: nil)
        
        // Apps that you want to exclude sharing the items
        let excludedActivityTypes : [UIActivity.ActivityType] = [
            .addToReadingList,
            .assignToContact,
            .copyToPasteboard,
            .mail,
            .markupAsPDF,
            .message,
            .openInIBooks,
            .postToFacebook,
            .postToFlickr,
            .postToTencentWeibo,
            .postToTwitter,
            .postToVimeo,
            .postToWeibo,
            .print,
            .saveToCameraRoll
        ]

        activityView.excludedActivityTypes = excludedActivityTypes
        self.present(activityView, animated: true, completion: nil)
        
        activityView.completionWithItemsHandler = { activityType, completed, items, error in
            
            // Event Cancelled
            if !completed {
                print("Content Sharing was cancelled.")
                return
            }
            
            // Content Shared on particular activity
            print("Shared on activity type: \(String(describing: activityType?.rawValue))")
            
            // Detect app on which the items are shared
            if let type = activityType {
                switch type {
                case .addToReadingList: print("Added To Reading List"); break
                case .airDrop: print("AirDropped to Other Device"); break
                case .assignToContact: print("Assigned To Contact"); break
                case .copyToPasteboard: print("Copied To Pasteboard"); break
                case .mail: print("Mailed"); break
                case .markupAsPDF: print("Marked-Up As PDF"); break
                case .message: print("Messaged"); break
                case .openInIBooks: print("Opened In iBooks"); break
                case .postToFacebook: print("Posted To Facebook"); break
                case .postToFlickr: print("Posted To Flickr"); break
                case .postToTencentWeibo: print("Posted To Tencent Weibo"); break
                case .postToTwitter: print("Posted To Twitter"); break
                case .postToVimeo: print("Posted To Vimeo"); break
                case .postToWeibo: print("Posted To Weibo"); break
                case .print: print("Printed"); break
                case .saveToCameraRoll: print("Saved To Camera Roll"); break
                default: print("Shared with new app"); break
                }
            }
        }
    }
于 2020-08-07T07:09:51.690 回答
2

对于那里的 Swifties,以下是您如何在 Swift 中编写代码以及一些共享服务检测:

activityViewController.completionHandler = {(activityType, completed:Bool) in
    if !completed {
        //cancelled
        return
    }

    //shared successfully

    //below is how you would detect for different sharing services
    var activity:String = "other"
    if activityType == UIActivityTypePostToTwitter {
        activity = "twitter"
    }
    if activityType == UIActivityTypeMail {
        activity = "mail"
    }
    //more code here if you like
}
于 2015-01-09T23:46:46.943 回答
1

completed参数将是NO用户取消。

[controller setCompletionHandler:^(NSString *activityType, BOOL completed) {
    if (completed) {
        CWStatusBarNotification *notification = [CWStatusBarNotification new];
        [notification displayNotificationWithMessage:@"✓ Successfully Shared Centre!"
                                          forDuration:3.0f];

        notification.notificationLabelBackgroundColor = [UIColor colorWithRed:38.0f/255.0f green:81.0f/255.0f blue:123.0f/255.0f alpha:1.0f];
        notification.notificationLabelTextColor = [UIColor whiteColor];
    }
}];
于 2014-02-28T00:50:23.387 回答
1

SWIFT 2.0, iOS 8.0 >,你应该像这样使用完成处理程序:

self.presentViewController(activityVC, animated: true, completion: nil)

activityVC.completionWithItemsHandler = {(activityType, completed:Bool, returnedItems:[AnyObject]?, error: NSError?) in
     //do some action
}

在这里查看我的答案:https ://stackoverflow.com/a/34581940/1109892

于 2016-01-03T21:44:57.857 回答
1

斯威夫特 3

   func completionHandler(activityType: UIActivityType?, shared: Bool, items: [Any]?, error: Error?) {
        if (shared) {
            print("Cool user shared some stuff")
        }
        else {
            print("Bad user canceled sharing :(")
        }
    }

    activityController.completionWithItemsHandler = completionHandler
于 2017-01-23T09:33:02.997 回答