Search code examples
iosswiftuialertcontrollerslcomposeviewcontrollershare-extension

Display an UIAlertController from my share extension ShareViewController


I'm currently developing my first share extension in swift 3. This is actually a very simple extension which share a text or an url directly from Safari (by selecting the text or the url from the web page content)

I'd like to display an UIAlertController in didSelectPost() of the ShareViewController (SLComposeServiceViewController) but this is not actually working. The alert will display only for few nanoseconds right after the user has tapped the post button. FYI, here is the code I'm using for this :

func displayUIAlertController(title: String, message: String) {

    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
    self.present(alert, animated: true, completion: nil)

}

and then in didSelectPost()

displayUIAlertController(title: "title", message: "test")

Is there a way to display an UIAlertController from a share extension ? Thanks.

Edit. I guess I got it, but my only concern now is to be certain that this will be accepted by Apple when they'll valid my app for the appstore (this will be my first app, I don't even have a paid developer account yet)

Here is my solution:

func displayUIAlertController(title: String, message: String) {

    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction!) -> () in
        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
    }))

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

So now self.extensionContext!.completeRequest() is called after the user has clicked on the alert's button instead of at the end of didSelectPost(). It works like a charm, but I am still wondering if this is a "safe" solution or not ? If so, I guess my solution could help the other related questions.

Hopefully someone who'll let me know.


Solution

  • I edited the initial post with an answer which works. Without any confirmation from you guys, I guess my solution is the best one but let me know if there's something different that can be done or if this solution might be a problem during Apple validation.

    Here is my solution:

    func displayUIAlertController(title: String, message: String) {
    
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction!) -> () in
        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
    }))
    
    self.present(alert, animated: true, completion: nil)
    }
    

    So now self.extensionContext!.completeRequest() is called after the user has clicked on the alert's button instead of at the end of didSelectPost(). It works like a charm, but I am still wondering if this is a "safe" solution or not ? If so, I guess my solution could help the other related questions.