diff --git a/Projects/App/ShareExtension/Sources/DotoriShareViewController.swift b/Projects/App/ShareExtension/Sources/DotoriShareViewController.swift index 37d675f0..dbfcb22f 100644 --- a/Projects/App/ShareExtension/Sources/DotoriShareViewController.swift +++ b/Projects/App/ShareExtension/Sources/DotoriShareViewController.swift @@ -106,9 +106,7 @@ private extension DotoriShareViewController { func bindAction() { cancelButton.tapPublisher .sink(with: self, receiveValue: { owner, _ in - owner.hideExtension { _ in - owner.extensionContext?.cancelRequest(withError: NSError(domain: "Share Canceled", code: 1)) - } + owner.cancelRequest("Share cancelled", code: 1) }) .store(in: &subscription) @@ -122,11 +120,7 @@ private extension DotoriShareViewController { owner.extensionContext?.completeRequest(returningItems: nil) } } catch { - owner.hideExtension { _ in - owner.extensionContext?.cancelRequest( - withError: NSError(domain: "Jwt Token is expired", code: 6) - ) - } + owner.cancelRequest("Jwt Token is expired", code: 6) } } }) @@ -142,34 +136,65 @@ private extension DotoriShareViewController { }, completion: completion) } + func cancelRequest(_ errorMessage: String, code: Int) { + self.hideExtension { _ in + let error = NSError(domain: errorMessage, code: code) + self.extensionContext?.cancelRequest(withError: error) + } + } + func bindExtensionInput() { guard let extensionInput = extensionContext?.inputItems as? [NSExtensionItem] else { - self.extensionContext?.cancelRequest(withError: NSError(domain: "Invalid Inputs", code: 2)) + self.cancelRequest("Invalid Inputs", code: 2) return } for input in extensionInput where input.attachments?.isEmpty == false { - let itemProviders = input.attachments ?? [] - - for itemProvider in itemProviders where itemProvider.hasItemConformingToTypeIdentifier("public.url") { - itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil) { [weak self] item, error in - guard let self, let url = item as? URL, error == nil else { - self?.hideExtension { _ in - self?.extensionContext?.cancelRequest( - withError: NSError(domain: "Invalid URL Input", code: 3) - ) - } - return - } - self.shareURL = url + for itemProvider in input.attachments ?? [] { + guard let typeIdentifier = itemProvider.registeredTypeIdentifiers.first else { + self.cancelRequest("Invalid URL Input", code: 3) + return + } - DispatchQueue.global(qos: .userInteractive).async { - self.bindYoutubeThumbnail(url: url) + itemProvider.loadItem(forTypeIdentifier: typeIdentifier) { [weak self] item, error in + guard error == nil else { + self?.cancelRequest("Invalid URL Input", code: 3) + return } + self?.handleLoadedItem(item, typeIdentifier: typeIdentifier) } } } } + func handleLoadedItem(_ item: NSSecureCoding?, typeIdentifier: String) { + switch typeIdentifier { + case UTType.url.identifier: + guard let url = item as? URL else { + self.cancelRequest("Invalid URL Input", code: 3) + return + } + self.bindInputURL(url: url) + + case UTType.plainText.identifier: + guard let urlString = item as? String, let url = URL(string: urlString) else { + self.cancelRequest("Invalid URL Input", code: 3) + return + } + self.bindInputURL(url: url) + + default: + self.cancelRequest("Invalid URL Input", code: 3) + } + } + + func bindInputURL(url: URL) { + self.shareURL = url + + DispatchQueue.global(qos: .userInteractive).async { + self.bindYoutubeThumbnail(url: url) + } + } + func bindYoutubeThumbnail(url: URL) { DispatchQueue.main.async { self.imageActivityIndicator.startAnimating() @@ -177,21 +202,13 @@ private extension DotoriShareViewController { let provider = LPMetadataProvider() provider.startFetchingMetadata(for: url) { [weak owner = self] metadata, error in guard let owner, let metadata, error == nil else { - owner?.hideExtension { _ in - owner?.extensionContext?.cancelRequest( - withError: NSError(domain: "Invalid Youtube Metadata", code: 4) - ) - } + owner?.cancelRequest("Invalid Youtube Metadata", code: 4) return } metadata.imageProvider?.loadObject(ofClass: UIImage.self) { image, error in guard let image = image as? UIImage, error == nil else { - owner.hideExtension { _ in - owner.extensionContext?.cancelRequest( - withError: NSError(domain: "Invalid Youtube Thumbnail Image", code: 5) - ) - } + owner.cancelRequest("Invalid Youtube Thumbnail Image", code: 5) return } diff --git a/Projects/App/ShareExtension/Support/Info.plist b/Projects/App/ShareExtension/Support/Info.plist index ef8626ed..24f8116f 100644 --- a/Projects/App/ShareExtension/Support/Info.plist +++ b/Projects/App/ShareExtension/Support/Info.plist @@ -28,6 +28,8 @@ NSExtensionActivationRule + NSExtensionActivationSupportsText + NSExtensionActivationDictionaryVersion 1 NSExtensionActivationSupportsWebURLWithMaxCount