ios UIActivityViewController 完成处理程序在推文失败时返回成功

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28169192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:32:10  来源:igfitidea点击:

UIActivityViewController completion handler returns success when tweet has failed

iosswifttwitteruiactivityviewcontroller

提问by Swinny89

I'm using a UIActivityViewController to display a share sheet so users can share my app. I'm currently testing tweets and i'm getting some unexpected results. On tweeting for the first time, all goes well. On the second time, i'm getting a duplicate tweet error message, which is expected. The problem is that the completionWithItemsHandler is returning success: Bool as true!

我正在使用 UIActivityViewController 来显示共享表,以便用户可以共享我的应用程序。我目前正在测试推文,并且得到了一些意想不到的结果。第一次发推文,一切顺利。第二次,我收到重复的推文错误消息,这是意料之中的。问题是 completionWithItemsHandler 返回 success: Bool as true!

I want to be able to display my own personalised message to the user rather than the massive one that is returned currently.

我希望能够向用户显示我自己的个性化消息,而不是当前返回的大量消息。

Here is my code:

这是我的代码:

@IBAction func ShareButtonTapped(sender: AnyObject) {
    let textToShare = "I'm using Buzz!  The new way to send emoji's, with sound, it's annoying, funny and amazing"
    var url = NSURL(string: "-Image url masked out-")
    var data = NSData(contentsOfURL: url!)
    let image = UIImage(data: data!)
    if let myWebsite = NSURL(string: "-redirect masked out-")
    {
        let objectsToShare = [textToShare, myWebsite]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        activityVC.completionWithItemsHandler = {
            (activity, success, items, error) in
            println("Activity: \(activity) Success: \(success) Items: \(items) Error: \(error)")
        }
        self.presentViewController(activityVC, animated: true, completion: { () -> Void in

        })
    }
}

Here is my log:

这是我的日志:

2015-01-27 11:10:58.021 Buzz[3239:813859] LaunchServices: invalidationHandler called
2015-01-27 11:10:58.052 Buzz[3239:813860] LaunchServices: invalidationHandler called Activity: com.apple.UIKit.activity.PostToTwitter Success: true Items: nil Error: nil
2015-01-27 11:11:04.134 Buzz[3239:813859] LaunchServices: invalidationHandler called
2015-01-27 11:11:09.182 Buzz[3239:813859] plugin com.apple.share.Twitter.post invalidated

2015-01-27 11:10:58.021 Buzz[3239:813859] LaunchServices:invalidationHandler 调用
2015-01-27 11:10:58.052 Buzz[3239:813860] LaunchServices:invalidation.comactivity.apple.UI:Activity PostToTwitter 成功:true 项目:nil 错误:nil
2015-01-27 11:11:04.134 Buzz[3239:813859] LaunchServices: invalidationHandler 调用
2015-01-27 11:11:09.182 Buzz[33com.apple plugin]8 .share.Twitter.post 失效

回答by Soumen

Use completion handler like this For SWIFT 3 AND 4, iOS 10 AND 11:

像这样使用完成处理程序对于SWIFT 3 AND 4, iOS 10 AND 11

activityVC.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in
    if !completed {
        // User canceled 
        return
    }
    // User completed activity
}

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

回答by Adam Studenic

SWIFT 2.0 iOS 8.0 >, you should use completion handler like this:

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

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

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

     // Return if cancelled
     if (!completed) {
         return
     }

     //activity complete
     //some code here


}

回答by Teemu Kurppa

I don't think you can affect the feedback flow of the UIActivityViewController as it is high-level, easy-to-use component that is not tailored for fine-grained customization.

我认为您不会影响 UIActivityViewController 的反馈流,因为它是高级、易于使用的组件,不是为细粒度定制而量身定制的。

What you can do, though, is to save the state that user has tweeted this exact message after the first tweet and then disable Twitter from UIActivityController using excludedActivityTypesand UIActivityTypePostToTwitter. So, instead of showing an error for a duplicate tweet, you prevent the action sequence even from happening.

但是,您可以做的是保存用户在第一条推文之后发布了这条确切消息的状态,然后使用excludedActivityTypes和 从 UIActivityController 禁用 Twitter UIActivityTypePostToTwitter。因此,您不仅不会为重复的推文显示错误,还可以防止操作序列发生。

回答by tdh

I suggest replacing

我建议更换

self.presentViewController(activityVC, animated: true, completion: { () -> Void in })

with

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

This worked for me. Hope it helps!

这对我有用。希望能帮助到你!