Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

ios - UIActivityViewController completion handler returns success when tweet has failed

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!

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use completion handler like this For 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...