Gurjit Singh
Gurjit Singh

Reputation: 1913

Stop Present Presenting view controller when move to another screen

There is a share button in my application in a view controller of product details page. By pressing share button, an API called then after response, i am model presenting UIActivityViewController to share the URLs and other things. The problem is that in some stage api getting time to fetch share data, in that mean time user can move back to previous view controller or move forward to another view controller.

My question is how can I stop presenting UIActivityViewController if i am not in that screen when its going to present? I may be on next screen in navigation or move back.

Another issue I am facing if UIActivityViewController get present on another screen with respect to above case, then dismissing that making my whole app interaction disabled in iOS 13 because an UIDimming type view appears in main UIWindow which is not getting dismiss when dismissing UIActivityViewController.

Here is the code:

@IBAction private func toolbarShareStonesBtnPressed(_ sender: UIBarButtonItem) {
    guard !isSharingInProgress else { return }
    Utils.showLoading(forTargetVC: self)
    isSharingInProgress = true
    viewModel.getSelectedStonesShareLinks { [weak self] (errorMsg, shareURLs) in
        guard let `self` = self else { return }
        Utils.hideLoading(forTargetVC: self)
        self.isSharingInProgress = false
        if let errorMsg = errorMsg {
            Utils.showAlert(withMessage: errorMsg)
        } else if let shareURLs = shareURLs {
            let activityViewController = UIActivityViewController(activityItems: shareURLs , applicationActivities: nil)
            activityViewController.popoverPresentationController?.sourceView = self.view
            if let popoverController = activityViewController.popoverPresentationController {
                popoverController.barButtonItem = sender
                popoverController.permittedArrowDirections = .any
            }
            self.present(activityViewController, animated: true, completion: nil)
        }
    }
}

Upvotes: 0

Views: 677

Answers (1)

matt
matt

Reputation: 536028

I’d say you are doing this backward. When the button is tapped, present the activity view controller immediately. For your activity item, use a UIActivityItemProvider.

https://developer.apple.com/documentation/uikit/uiactivityitemprovider

That is its whole purpose, to act as a conduit when your activity item needs some time to fetch the data. It is an Operation so now you can network asynchronously in the background. Meanwhile the activity view is up, and prevents navigation unless the user gives up and cancels it.

Upvotes: 2

Related Questions