Skip to content

Commit

Permalink
Merge pull request #182 from Team-Ampersand/181-shareextension-not-work
Browse files Browse the repository at this point in the history
🔀 :: [#181] ShareExtension 버그 해결
  • Loading branch information
baekteun authored Aug 16, 2023
2 parents cf105c4 + cc4afa5 commit 6b87aa6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 34 deletions.
85 changes: 51 additions & 34 deletions Projects/App/ShareExtension/Sources/DotoriShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}
}
})
Expand All @@ -142,56 +136,79 @@ 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()
}
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
}

Expand Down
2 changes: 2 additions & 0 deletions Projects/App/ShareExtension/Support/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsText</key>
<true/>
<key>NSExtensionActivationDictionaryVersion</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
Expand Down

0 comments on commit 6b87aa6

Please sign in to comment.