Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,19 @@ class iBoxFactory: ProjectFactory {
"NSExtension": [
"NSExtensionAttributes": [
"NSExtensionActivationRule": [
"NSExtensionActivationSupportsWebPageWithMaxCount" : 1,
"NSExtensionActivationSupportsWebURLWithMaxCount" : 1
"NSExtensionActivationSupportsWebPageWithMaxCount": 1,
"NSExtensionActivationSupportsWebURLWithMaxCount": 1,
"SUBQUERY": [
"extensionItems": [
"SUBQUERY": [
"attachments": [
"ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO 'public.data'": "TRUE"
],
"@count": 1
]
],
"@count": 1
]
]
],
"NSExtensionPointIdentifier": "com.apple.share-services",
Expand Down
35 changes: 34 additions & 1 deletion ShareExtension/Sources/ShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,26 @@ class CustomShareViewController: UIViewController {
}

func extractSharedURL() {
guard let extensionItem = extensionContext?.inputItems.first as? NSExtensionItem else { return }
guard let extensionItem = extensionContext?.inputItems.first as? NSExtensionItem else {
print("No extension items found.")
return
}

if let item = extensionContext?.inputItems.first as? NSExtensionItem {
for attachment in item.attachments ?? [] {
if attachment.hasItemConformingToTypeIdentifier("public.plain-text") {
attachment.loadItem(forTypeIdentifier: "public.plain-text", options: nil) { (data, error) in
DispatchQueue.main.async {
if let text = data as? String {
self.extractURL(fromText: text)
} else {
print("Error loading text: \(String(describing: error))")
}
}
}
}
}
}

for attachment in extensionItem.attachments ?? [] {
if attachment.hasItemConformingToTypeIdentifier(UTType.url.identifier) {
Expand All @@ -86,10 +105,24 @@ class CustomShareViewController: UIViewController {
}
}
break
} else {
print("Attachment does not conform to URL type.")
}
}
}

private func extractURL(fromText text: String) {
let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let matches = detector?.matches(in: text, options: [], range: NSRange(location: 0, length: text.utf8.count))

if let firstMatch = matches?.first, let range = Range(firstMatch.range, in: text), let url = URL(string: String(text[range])) {
print("Extracted URL: \(url)")
self.dataURL = url.absoluteString
} else {
print("No URL found in text")
}
}

// MARK: IBAction

@IBAction func cancel() {
Expand Down