mirror of
https://gitlab.linphone.org/BC/public/linphone-iphone.git
synced 2026-01-17 02:58:07 +00:00
Fix Share extension
This commit is contained in:
parent
106e628e41
commit
06b76b2935
1 changed files with 70 additions and 50 deletions
|
|
@ -19,10 +19,12 @@
|
||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
import Social
|
import Social
|
||||||
|
import UniformTypeIdentifiers
|
||||||
|
|
||||||
class ShareViewController: SLComposeServiceViewController {
|
class ShareViewController: SLComposeServiceViewController {
|
||||||
|
|
||||||
var remainingSlots = 12
|
var remainingSlots = 12
|
||||||
|
var fileURLs: [URL] = []
|
||||||
|
|
||||||
override func isContentValid() -> Bool {
|
override func isContentValid() -> Bool {
|
||||||
return true
|
return true
|
||||||
|
|
@ -36,49 +38,83 @@ class ShareViewController: SLComposeServiceViewController {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
private func handleSharedFiles() {
|
private func handleSharedFiles() {
|
||||||
guard let extensionItems = self.extensionContext?.inputItems as? [NSExtensionItem] else { return }
|
// Get all items shared to the extension
|
||||||
|
guard let extensionItems = self.extensionContext?.inputItems as? [NSExtensionItem] else { return }
|
||||||
|
|
||||||
var fileURLs: [URL] = []
|
let dispatchGroup = DispatchGroup()
|
||||||
let dispatchGroup = DispatchGroup()
|
|
||||||
|
|
||||||
for item in extensionItems {
|
for item in extensionItems {
|
||||||
if let attachments = item.attachments {
|
if let attachments = item.attachments {
|
||||||
for provider in attachments {
|
for provider in attachments {
|
||||||
guard remainingSlots > 0 else { break }
|
guard remainingSlots > 0 else { break }
|
||||||
if provider.hasItemConformingToTypeIdentifier("public.item") {
|
remainingSlots -= 1
|
||||||
remainingSlots -= 1
|
dispatchGroup.enter()
|
||||||
dispatchGroup.enter()
|
|
||||||
provider.loadItem(forTypeIdentifier: "public.item", options: nil) { item, error in
|
if provider.hasItemConformingToTypeIdentifier(UTType.image.identifier) {
|
||||||
if let url = item as? URL, let saved = self.copyFileToSharedContainer(from: url) {
|
// Image / screenshot
|
||||||
fileURLs.append(saved)
|
provider.loadItem(forTypeIdentifier: UTType.image.identifier, options: nil) { item, error in
|
||||||
dispatchGroup.leave()
|
defer { dispatchGroup.leave() }
|
||||||
} else if let image = item as? UIImage,
|
self.handleImageItem(item)
|
||||||
let data = image.pngData(),
|
|
||||||
let saved = self.saveDataToSharedContainer(data: data) {
|
|
||||||
fileURLs.append(saved)
|
|
||||||
dispatchGroup.leave()
|
|
||||||
} else if let data = item as? Data,
|
|
||||||
let saved = self.saveDataToSharedContainer(data: data) {
|
|
||||||
fileURLs.append(saved)
|
|
||||||
dispatchGroup.leave()
|
|
||||||
} else {
|
|
||||||
dispatchGroup.leave()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else if provider.hasItemConformingToTypeIdentifier("public.item") {
|
||||||
|
// Other files (PDF, video, document, etc.)
|
||||||
|
provider.loadFileRepresentation(forTypeIdentifier: "public.item") { url, error in
|
||||||
|
defer { dispatchGroup.leave() }
|
||||||
|
if let url = url, let saved = self.copyFileToSharedContainer(from: url) {
|
||||||
|
self.fileURLs.append(saved)
|
||||||
|
print("File copied to App Group: \(saved.path)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Unsupported type, just skip
|
||||||
|
dispatchGroup.leave()
|
||||||
|
print("Unsupported file type encountered.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatchGroup.notify(queue: .main) {
|
// Once all providers are processed, open parent app if we have files
|
||||||
if !fileURLs.isEmpty {
|
dispatchGroup.notify(queue: .main) {
|
||||||
self.openParentApp(with: fileURLs)
|
if !self.fileURLs.isEmpty {
|
||||||
} else {
|
self.openParentApp(with: self.fileURLs)
|
||||||
self.extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
|
} else {
|
||||||
}
|
self.extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
|
||||||
}
|
print("No valid files to send.")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func handleImageItem(_ item: NSSecureCoding?) {
|
||||||
|
guard let item = item else { return }
|
||||||
|
|
||||||
|
if let image = item as? UIImage {
|
||||||
|
// Create a temporary file for the image
|
||||||
|
let tempURL = FileManager.default.temporaryDirectory
|
||||||
|
.appendingPathComponent(UUID().uuidString + ".png")
|
||||||
|
|
||||||
|
if let data = image.pngData() {
|
||||||
|
do {
|
||||||
|
try data.write(to: tempURL)
|
||||||
|
if let saved = self.copyFileToSharedContainer(from: tempURL) {
|
||||||
|
fileURLs.append(saved)
|
||||||
|
print("Image copied to App Group: \(saved.path)")
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
print("Error writing UIImage -> file: \(error)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if let url = item as? URL {
|
||||||
|
// If iOS returned a URL
|
||||||
|
if let saved = self.copyFileToSharedContainer(from: url) {
|
||||||
|
fileURLs.append(saved)
|
||||||
|
print("Image file copied from URL: \(saved.path)")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
print("Unsupported image type: \(type(of: item))")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func openParentApp(with fileURLs: [URL]) {
|
func openParentApp(with fileURLs: [URL]) {
|
||||||
let urlStrings = fileURLs.map { $0.path }
|
let urlStrings = fileURLs.map { $0.path }
|
||||||
|
|
@ -123,21 +159,5 @@ class ShareViewController: SLComposeServiceViewController {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveDataToSharedContainer(data: Data, suggestedName: String = "screenshot") -> URL? {
|
|
||||||
guard let sharedContainerURL = FileManager.default.containerURL(
|
|
||||||
forSecurityApplicationGroupIdentifier: "group.org.linphone.phone.linphoneExtension"
|
|
||||||
) else { return nil }
|
|
||||||
|
|
||||||
let randomInt = Int.random(in: 1000...9999)
|
|
||||||
let destinationURL = sharedContainerURL.appendingPathComponent("\(suggestedName)_\(randomInt).png")
|
|
||||||
|
|
||||||
do {
|
|
||||||
try data.write(to: destinationURL)
|
|
||||||
return destinationURL
|
|
||||||
} catch {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue