Przeglądaj źródła

append shared urls to text field of share extension

cyberta 5 lat temu
rodzic
commit
5e9fe02f85

+ 11 - 0
DcShare/Controller/ShareViewController.swift

@@ -163,6 +163,17 @@ extension ShareViewController: SendingControllerDelegate {
 }
 
 extension ShareViewController: ShareAttachmentDelegate {
+    func onUrlShared(url: URL) {
+        DispatchQueue.main.async {
+            if var contentText = self.contentText, !contentText.isEmpty {
+                contentText.append("\n\(url.absoluteString)")
+                self.textView.text = contentText
+            } else {
+                self.textView.text = "\(url.absoluteString)"
+            }
+        }
+    }
+
     func onAttachmentChanged() {
         DispatchQueue.main.async {
             self.validateContent()

+ 18 - 0
DcShare/Helper/ShareAttachment.swift

@@ -7,6 +7,7 @@ import QuickLookThumbnailing
 protocol ShareAttachmentDelegate: class {
     func onAttachmentChanged()
     func onThumbnailChanged()
+    func onUrlShared(url: URL)
 }
 
 class ShareAttachment {
@@ -56,6 +57,8 @@ class ShareAttachment {
                 createAudioMsg(attachment)
             } else if attachment.hasItemConformingToTypeIdentifier(kUTTypeFileURL as String) {
                 createFileMsg(attachment)
+            } else if attachment.hasItemConformingToTypeIdentifier(kUTTypeURL as String) {
+                addSharedUrl(attachment)
             }
         }
     }
@@ -171,4 +174,19 @@ class ShareAttachment {
         }
     }
 
+    func addSharedUrl(_ item: NSItemProvider) {
+        if let delegate = self.delegate {
+            item.loadItem(forTypeIdentifier: kUTTypeURL as String, options: nil) { data, error in
+                switch data {
+                case let url as URL:
+                    delegate.onUrlShared(url: url)
+                default:
+                    self.dcContext.logger?.debug("Unexpected data: \(type(of: data))")
+                }
+                if error != nil {
+                    self.dcContext.logger?.error(error?.localizedDescription ?? "Could not share URL.")
+                }
+            }
+        }
+    }
 }