Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #4034: Revert #3863 text share extension logic. (#4037)
Browse files Browse the repository at this point in the history
  • Loading branch information
iccub committed Aug 12, 2021
1 parent 6f53d93 commit 168a09e
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions BraveShareTo/ShareToBraveViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ class ShareToBraveViewController: SLComposeServiceViewController {

init?(item: NSSecureCoding) {
if let text = item as? String {
if let url = URL(string: text)?.absoluteString {
urlOrQuery = url
type = .url
} else {
urlOrQuery = text
type = .query
}
urlOrQuery = text
type = .query
} else if let url = (item as? URL)?.absoluteString.firstURL?.absoluteString {
urlOrQuery = url
type = .url
} else {
return nil
}
Expand Down Expand Up @@ -64,14 +62,14 @@ class ShareToBraveViewController: SLComposeServiceViewController {

// Look for the first URL the host application is sharing.
// If there isn't a URL grab the first text item
guard let provider = attachments.first(where: { $0.isText }) else {
guard let provider = attachments.first(where: { $0.isUrl }) ?? attachments.first(where: { $0.isText }) else {
// If no item was processed. Cancel the share action to prevent the extension from locking the host application
// due to the hidden ViewController.
cancel()
return []
}

provider.loadItem(forTypeIdentifier: String(kUTTypeText), options: nil) { item, error in
provider.loadItem(of: provider.isUrl ? kUTTypeURL : kUTTypeText) { item, error in
guard let item = item, let schemeUrl = Scheme(item: item)?.schemeUrl else {
self.cancel()
return
Expand Down Expand Up @@ -116,6 +114,14 @@ extension NSItemProvider {
var isText: Bool {
return hasItemConformingToTypeIdentifier(String(kUTTypeText))
}

var isUrl: Bool {
return hasItemConformingToTypeIdentifier(String(kUTTypeURL))
}

func loadItem(of type: CFString, completion: CompletionHandler?) {
loadItem(forTypeIdentifier: String(type), options: nil, completionHandler: completion)
}
}

extension NSObject {
Expand Down

0 comments on commit 168a09e

Please sign in to comment.