如果用户按下取消,UIActivityViewController完成处理程序仍会调用操作

时间:2014-02-28 00:42:06

标签: ios uiactivityviewcontroller completionhandler

在我的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];



}];

感谢您的帮助!

8 个答案:

答案 0 :(得分:19)

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

更新: 就像adruzh说的那样,在iOS8上有一个新的completionHandler,Apple在文档中忘了提及:

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

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

答案 1 :(得分:13)

这就是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];
}];

答案 2 :(得分:3)

对于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)")
}

答案 3 :(得分: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
                }
            }
        }
    }

答案 4 :(得分: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
}

答案 5 :(得分: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];
    }
}];

答案 6 :(得分: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

答案 7 :(得分:1)

Swift 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