iosswiftxcodeshareshare-extension

Share extension ios swift


I have created a share extension with some buttons in it and inherited that extension with UIViewcontroller. when I create @IBAction of those UIButtons it never gets called on clicking the button.

import UIKit
import Social

class ShareViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    
//    override func isContentValid() -> Bool {
//        // Do validation of contentText and/or NSExtensionContext attachments here
//        return true
//    }
//
//    override func didSelectPost() {
//        // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
//    
//        // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
//        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
//    }
//
//    override func configurationItems() -> [Any]! {
//        // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
//        return []
//    }

    @IBAction func btnBackClicked(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }
    @IBAction func btnCancleClicked(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }
    @IBAction func btnSaveClicked(_ sender: Any) {
        
    }
}

Solution

  • I think your button actions are being called, just that calling self.dismiss(animated: true, completion: nil) will not actually dismiss your share extension.

    With reference to this answer, you should be using:

    self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)

    OR

    self.extensionContext!.cancelRequest(withError:NSError(domain: Bundle.main.bundleIdentifier!, code: 0))