UIActivityViewController completion handler returns success when tweet has failed
Asked Answered
D

4

37

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

Drunken answered 27/1, 2015 at 11:15 Comment(0)
M
43

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)
Meliamelic answered 14/9, 2017 at 7:26 Comment(3)
completed always returns true even though press cancelTwoup
This worked for me, "completed" returns false even if user cancels within another app (Gmail for example).Hungnam
Lets say i tapped on whats app but after open whats app, i am not sharing it and press "cancel". Then how can i know sharing is not successful.Terisateriyaki
E
13

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

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


}
Eada answered 3/1, 2016 at 21:41 Comment(2)
Lets say i tapped on whats app but after open whats app, i am not sharing it and press "cancel". Then how can i know sharing is not successful.Terisateriyaki
@MiteshDobareeya I am facing the same issue. how you overcome it?Dianadiandra
E
8

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.

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 excludedActivityTypes and UIActivityTypePostToTwitter. So, instead of showing an error for a duplicate tweet, you prevent the action sequence even from happening.

Easley answered 13/2, 2015 at 16:2 Comment(0)
I
-5

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!

Idiom answered 17/2, 2015 at 20:28 Comment(4)
What does this work to achieve though? Surely by removing the completion handler i'm further way from being able to check / change error messagesDrunken
I suggest testing this, and then, if it is still not working, removing it. This seemed to work for me. @DrunkenIdiom
I was using the function with the completion handler to begin with. The functionality all still works with / without the completion handlerDrunken
This has nothing to do with what was asked in the question.Hungnam

© 2022 - 2024 — McMap. All rights reserved.