Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ios handle sharing a text file #107

Merged
merged 1 commit into from
Sep 20, 2024
Merged
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
29 changes: 18 additions & 11 deletions plugin/src/ios/ShareExtensionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ class ShareViewController: SLComposeServiceViewController {
for (index, attachment) in (contents).enumerated() {
if attachment.hasItemConformingToTypeIdentifier(imageContentType) {
handleImages(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(textContentType) {
handleText(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(videoContentType) {
handleVideos(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(fileURLType) {
handleFiles(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(urlContentType) {
handleUrl(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(videoContentType) {
handleVideos(content: content, attachment: attachment, index: index)
} else if attachment.hasItemConformingToTypeIdentifier(textContentType) {
handleText(content: content, attachment: attachment, index: index)
} else {
NSLog("[ERROR] content type not handle !\(String(describing: content))")
self.dismissWithError(message: "content type not handle \(String(describing: content)))")
}
}
}
Expand All @@ -52,8 +55,10 @@ class ShareViewController: SLComposeServiceViewController {

private func handleText (content: NSExtensionItem, attachment: NSItemProvider, index: Int) {
attachment.loadItem(forTypeIdentifier: textContentType, options: nil) { [weak self] data, error in

if error == nil, let item = data as? String, let this = self {
if (error != nil) {
NSLog("[ERROR] Cannot load text content !\(String(describing: error))")
self?.dismissWithError(message: "Cannot load text content \(String(describing: error))")
} else if let item = data as? String, let this = self {
this.sharedText.append(item)

// If this is the last item, save sharedText in userDefaults and redirect to host app
Expand All @@ -65,15 +70,18 @@ class ShareViewController: SLComposeServiceViewController {
}

} else {
self?.dismissWithError(message: "Cannot load text content")
NSLog("[ERROR] Cannot load text content !\(String(describing: content))")
self?.dismissWithError(message: "text content is empty \(String(describing: content)))")
}
}
}

private func handleUrl (content: NSExtensionItem, attachment: NSItemProvider, index: Int) {
attachment.loadItem(forTypeIdentifier: urlContentType, options: nil) { [weak self] data, error in

if error == nil, let item = data as? URL, let this = self {
if (error != nil) {
NSLog("[ERROR] Cannot load url content !\(String(describing: error))")
self?.dismissWithError(message: "Cannot load url content \(String(describing: error))")
} else if let item = data as? URL, let this = self {
this.sharedText.append(item.absoluteString)

// If this is the last item, save sharedText in userDefaults and redirect to host app
Expand All @@ -84,9 +92,8 @@ class ShareViewController: SLComposeServiceViewController {
this.redirectToHostApp(type: .weburl)
}
} else {
self?.dismissWithError(message: "Cannot load url content")
self?.dismissWithError(message: "url is empty")
}

}
}

Expand Down
Loading