Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 458
Swinny89 Avatar asked Jan 27 '15 11:01

Swinny89


People also ask

What is a uiactivityviewcontroller?

A standard view controller that connects the application to a number of standard services such as social networks, SMS, etc. The UIActivityViewController allows the application user to easily share data between the current application and services. A number of services such as social networks, email, and SMS are provided by the OS.

What happens when UIViewController is overridden?

When overridden, returns the UIViewController that determines whether the status bar is hidden or unhidden. When overridden, returns the UIViewController that determines the style of the status bar. Invoked to determine if this object implements the specified protocol. Performs a copy of the underlying Objective-C object.

What is beginappearancetransition in UIViewController?

With BeginAppearanceTransition (Boolean, Boolean), tells child UIViewController s that their child views have just appeared or disappeared. The array of child UIViewController objects that should be searched to determine if they are the unwind segue destination.

How do I restore the state of a child UIViewController?

With DecodeRestorableState (NSCoder), allows custom state restoration. With BeginAppearanceTransition (Boolean, Boolean), tells child UIViewController s that their child views have just appeared or disappeared. The array of child UIViewController objects that should be searched to determine if they are the unwind segue destination.


2 Answers

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) 
like image 97
Soumen Avatar answered Oct 04 '22 01:10

Soumen


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   } 
like image 37
Adam Studenic Avatar answered Oct 04 '22 00:10

Adam Studenic