D
D
dgolubeff2021-07-07 14:14:27
iOS
dgolubeff, 2021-07-07 14:14:27

Why does an ios application freeze after calling share extension?

Hey!
I am new to IOS development. I am doing my first project and I want to implement a small application that will allow me to save my favorite links from the browser. I'm using SwiftUI for the main app + Share Extension component to pass a link to the main app from the browser. The sequence of actions looks like this: I find an interesting site, click the share button and select my application icon. Then my application opens and there I see a link. The problem is that after that the browser freezes completely and you have to close it and reopen it, this does not always happen, in about 70% of cases. Moreover, if I connect the phone to the poppy and run the application in debug mode, then nothing hangs.
share extension code:

@objc(ShareExtensionViewController)
class ShareViewController: UIViewController {

    private static let URL_STORAGE_KEY = "url"
    private static let APP_URL = "MyApp://"
    private static let APP_GROUP_NAME = "group.ru.myapp"

    override func viewDidLoad() {
        super.viewDidLoad()
        // set link to UserDefaults
        handleShared()
        // open main app from extension
        openMainApp()
    }

    @objc
    @discardableResult
    func openURL(_ url: URL) -> Bool {
        var responder: UIResponder? = self
        while responder != nil {
            if let application = responder as? UIApplication {
                return application.perform(#selector(openURL(_:)), with: url) != nil
            }
            responder = responder?.next
        }
        return false
    }

    private func openMainApp() {
        DispatchQueue.global(qos: .background).async {
            self.extensionContext?.completeRequest(returningItems: nil, completionHandler: { _ in
                let url = URL(string: ShareViewController.APP_URL)!
                self.openURL(url)
            })
        }
    }

    private func handleShared() {
        let attachments = (extensionContext?.inputItems.first as? NSExtensionItem)?.attachments ?? []
        let contentType = kUTTypeURL as String
        for provider in attachments {
            if provider.hasItemConformingToTypeIdentifier(contentType) {
                provider.loadItem(forTypeIdentifier: contentType, options: nil) {
                    [unowned self] (data, error) in
                    guard error == nil else {
                        return
                    }
                    let userDefaults = UserDefaults(suiteName: ShareViewController.APP_GROUP_NAME)!
                    userDefaults.set(data, forKey: ShareViewController.URL_STORAGE_KEY)
                }
            }
        }
    }
}

Fragment of the main application that reads the link from UserDefaults:
static func tryGetLink() -> String? {
    let userDefaults = UserDefaults(suiteName: DataStorage.APP_GROUP)
    return userDefaults?.object(forKey: URL_KEY) as? String
}

What could be the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question