HomeiOS Developmentios - Swift Share Extension crashes for Google Information sharing

ios – Swift Share Extension crashes for Google Information sharing


I’ve a Swift share extension that accepts URLs and textual content objects (code under). It really works as anticipated for a lot of apps (Mail, Safari, NetNewsWire and so on.).

When I attempt to share content material from inside Google Information the extension crashes. The view would not absolutely render, solely the semi-transparent background reveals up. It stays caught in that state and I’ve to kill Google Information to have the ability to use it once more.

I’ve tried including Logger statements, which do not present up wherever. I’ve tried debugging the extension operating on my bodily machine utilizing XCode unsuccessfully. Sadly, I am unable to set up Google Information on the iOS simulator.

The extension prints a single error, even within the success case (e.g. sharing from Safari):

-[_EXSinkLoadOperator loadItemForTypeIdentifier:completionHandler:expectedValueClass:options:] nil expectedValueClass permitting {(
    _EXItemProviderSandboxedResource,
    NSData,
    ...
)}

This is my view controller:

//...

class ShareViewController: UIViewController {    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        guard
            let extensionItem = extensionContext?.inputItems.first as? NSExtensionItem,
            let itemProvider = extensionItem.attachments?.first
        else {
            self.shut()
            return
        }
        
        if itemProvider.hasItemConformingToTypeIdentifier(UTType.url.identifier) {
            itemProvider.loadItem(forTypeIdentifier: UTType.url.identifier, choices: nil) { urlCoded, error in
                if let error {
                    self.shut()
                    return
                }
                
                if let url = urlCoded as? URL {
                    DispatchQueue.foremost.async {
                        self.renderView(url)
                    }
                } else {
                    self.shut()
                    return
                }
            }
        } else if itemProvider.hasItemConformingToTypeIdentifier(UTType.textual content.identifier) {
            itemProvider.loadItem(forTypeIdentifier: UTType.textual content.identifier, choices: nil) { providedText, error in
                if let error {
                    self.shut()
                    return
                }
                
                guard
                    let textual content = providedText as? String,
                    let url = self.extractURL(textual content)
                else {
                    self.shut()
                    return
                }
                
                DispatchQueue.foremost.async {
                    self.renderView(url)
                }
            }
        } else {
            self.shut()
            return
        }
    }
    
    personal func renderView(_ url: URL) {
        self.saveLink(url: url)
        
        // host the SwiftU view
        let contentView = UIHostingController(rootView: ShareView(url: url))
        self.addChild(contentView)
        self.view.addSubview(contentView.view)
        contentView.view.backgroundColor = .clear
        self.view.backgroundColor = .clear
        
        // arrange constraints
        contentView.view.translatesAutoresizingMaskIntoConstraints = false
        contentView.view.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        contentView.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        contentView.view.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
        contentView.view.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
        
        Timer.scheduledTimer(withTimeInterval: 1, repeats: false, block: { _ in self.shut() })
    }
    
    personal func saveLink(url: URL) {
        // writes to a .plist file that is shared with the principle app
    }
    
    personal func shut() {
        self.extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
    }
    
    personal func extractURL(_ textual content: String) -> URL? {
        do {
            let detector = attempt NSDataDetector(sorts: NSTextCheckingResult.CheckingType.hyperlink.rawValue)
            let matches = detector.matches(
                in: textual content,
                choices: [],
                vary: NSRange(location: 0, size: textual content.utf16.rely)
            )
            
            if let firstMatch = matches.first, let vary = Vary(firstMatch.vary, in: textual content) {
                let url = String(textual content[range])
                return URL(string: url)
            }
        } catch {
            print("Unable to extract URL from textual content: (error)")
        }
        
        return nil
    }
}

Not sharing the view code, it is a easy SwiftUI view that reveals sheet after which disappears once more. This is are the activation guidelines for the extension:

<key>NSExtensionActivationRule</key>
<dict>
    <key>NSExtensionActivationSupportsText</key>
    <true/>
    <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
    <integer>1</integer>
</dict>

The problem is on iOS 17.2 and upwards.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments