swiftui – Why does my iOS Share Extension mishandle plaintext content shared from Google Maps while correctly parsing URLs from Safari?


Hello I’m currently working on an iOS Share Extension that allows users to share content from various apps to my application. The Share Extension is designed to handle both URLs and plaintext content. However, I’m encountering a peculiar issue when attempting to share content from Google Maps.

When sharing content from Google Maps, the Share Extension consistently detects the content as plaintext, rather than a URL. While this behavior is unexpected, the real challenge arises when attempting to parse the shared content. Despite correctly identifying the content as plaintext, the Share Extension consistently fails to extract any meaningful data from it, resulting in an empty display within my app.

Interestingly, when sharing content from Safari, the Share Extension correctly identifies and parses the URL, displaying the content as expected. This inconsistency in behavior between Google Maps and Safari has left me puzzled.

I’ve included the relevant portion of my Share Extension code below for reference:

    // ShareViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()
    guard let extensionItem = extensionContext?.inputItems.first as? NSExtensionItem,
          let itemProvider = extensionItem.attachments?.first else {
        close()
        return
    }
    
    let textDataType = UTType.url.identifier
    let plainTextDataType = UTType.plainText.identifier
    
    // Handling plaintext content
    if itemProvider.hasItemConformingToTypeIdentifier(plainTextDataType) {
        itemProvider.loadItem(forTypeIdentifier: plainTextDataType, options: nil) { (providedText, error) in
            if let _ = error {
                self.close()
                return
            }
            if let localText = providedText as? String {
                // Parsing plaintext content
                DispatchQueue.main.async {
                    let contentView = UIHostingController(rootView: ShareExtensionView(text: localText))
                    self.addChild(contentView)
                    self.view.addSubview(contentView.view)
                    contentView.view.translatesAutoresizingMaskIntoConstraints = false
                    // Setting up constraints...
                }
            } else {
                self.close()
                return
            }
        }
    }
    // Handling URL content
    else if itemProvider.hasItemConformingToTypeIdentifier(textDataType) {
        itemProvider.loadItem(forTypeIdentifier: textDataType, options: nil) { (providedText, error) in
            if let _ = error {
                self.close()
                return
            }
            if let localText = providedText as? NSURL, let text = localText.absoluteString  {
                // Parsing URL content
                DispatchQueue.main.async {
                    let contentView = UIHostingController(rootView: ShareExtensionView(text: text))
                    self.addChild(contentView)
                    self.view.addSubview(contentView.view)
                    contentView.view.translatesAutoresizingMaskIntoConstraints = false
                    // Setting up constraints...
                }
            } else {
                self.close()
                return
            }
        }
    }
    // Close if no valid content is found
    else {
        close()
        return
    }
    // Adding observer to close the extension
    NotificationCenter.default.addObserver(forName: NSNotification.Name("close"), object: nil, queue: nil) { _ in
        DispatchQueue.main.async {
            self.close()
        }
    }
}

func close() {
    self.extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
}

Any insights or suggestions on why Google Maps content isn’t being parsed correctly by my Share Extension would be greatly appreciated. Thank you!

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img