Sam321pbs
Sam321pbs

Reputation: 301

UIActivityViewControllerCompletionWithItemsHandler having error with new update swift

I was working on a basic app a few weeks ago and everything was working fine but now I am having an issue with UIActivityViewControllerCompletionWithItemsHandler

I am new to swift so I am having trouble. I am getting a red line ctivityViewController.completionWithItemsHandler =

Not sure how I am supposed to type it so the image can save. Any help is appreciated!

enter image description here

Here is my code:

@IBAction func shareMeme(_ sender: AnyObject) {
    let img: UIImage = generateMemedImage()
    let shareItems:Array = [img]

    let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)

    self.save()
    print("Saved")

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

        self.save()
        print("Saved")

    }


    self.present(activityViewController, animated: true, completion: nil)
}

Upvotes: 0

Views: 1501

Answers (1)

rmaddy
rmaddy

Reputation: 318874

The API changed in Swift 3. Your code needs to be:

activityViewController.completionWithItemsHandler = { (activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) -> Void in
    if completed == true {
        self.save()
        print("Saved")
    }
}

Many, many APIs have changed in Swift 3. Whenever you get an error like this, check the API documentation to see the proper method name and arguments (and their types).

Upvotes: 5

Related Questions