From cea37c1746cad587a41b567887bf41a9a9b6ba5f Mon Sep 17 00:00:00 2001 From: Anselme Chorein Date: Fri, 20 Sep 2024 16:29:59 +0200 Subject: [PATCH] fix: ios handle sharing a text file (#107) --- .../ios/ShareExtensionViewController.swift | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/plugin/src/ios/ShareExtensionViewController.swift b/plugin/src/ios/ShareExtensionViewController.swift index 221d3c9..bf6d709 100644 --- a/plugin/src/ios/ShareExtensionViewController.swift +++ b/plugin/src/ios/ShareExtensionViewController.swift @@ -36,14 +36,17 @@ class ShareViewController: SLComposeServiceViewController { for (index, attachment) in (contents).enumerated() { if attachment.hasItemConformingToTypeIdentifier(imageContentType) { handleImages(content: content, attachment: attachment, index: index) - } else if attachment.hasItemConformingToTypeIdentifier(textContentType) { - handleText(content: content, attachment: attachment, index: index) + } else if attachment.hasItemConformingToTypeIdentifier(videoContentType) { + handleVideos(content: content, attachment: attachment, index: index) } else if attachment.hasItemConformingToTypeIdentifier(fileURLType) { handleFiles(content: content, attachment: attachment, index: index) } else if attachment.hasItemConformingToTypeIdentifier(urlContentType) { handleUrl(content: content, attachment: attachment, index: index) - } else if attachment.hasItemConformingToTypeIdentifier(videoContentType) { - handleVideos(content: content, attachment: attachment, index: index) + } else if attachment.hasItemConformingToTypeIdentifier(textContentType) { + handleText(content: content, attachment: attachment, index: index) + } else { + NSLog("[ERROR] content type not handle !\(String(describing: content))") + self.dismissWithError(message: "content type not handle \(String(describing: content)))") } } } @@ -52,8 +55,10 @@ class ShareViewController: SLComposeServiceViewController { private func handleText (content: NSExtensionItem, attachment: NSItemProvider, index: Int) { attachment.loadItem(forTypeIdentifier: textContentType, options: nil) { [weak self] data, error in - - if error == nil, let item = data as? String, let this = self { + if (error != nil) { + NSLog("[ERROR] Cannot load text content !\(String(describing: error))") + self?.dismissWithError(message: "Cannot load text content \(String(describing: error))") + } else if let item = data as? String, let this = self { this.sharedText.append(item) // If this is the last item, save sharedText in userDefaults and redirect to host app @@ -65,15 +70,18 @@ class ShareViewController: SLComposeServiceViewController { } } else { - self?.dismissWithError(message: "Cannot load text content") + NSLog("[ERROR] Cannot load text content !\(String(describing: content))") + self?.dismissWithError(message: "text content is empty \(String(describing: content)))") } } } private func handleUrl (content: NSExtensionItem, attachment: NSItemProvider, index: Int) { attachment.loadItem(forTypeIdentifier: urlContentType, options: nil) { [weak self] data, error in - - if error == nil, let item = data as? URL, let this = self { + if (error != nil) { + NSLog("[ERROR] Cannot load url content !\(String(describing: error))") + self?.dismissWithError(message: "Cannot load url content \(String(describing: error))") + } else if let item = data as? URL, let this = self { this.sharedText.append(item.absoluteString) // If this is the last item, save sharedText in userDefaults and redirect to host app @@ -84,9 +92,8 @@ class ShareViewController: SLComposeServiceViewController { this.redirectToHostApp(type: .weburl) } } else { - self?.dismissWithError(message: "Cannot load url content") + self?.dismissWithError(message: "url is empty") } - } }