mirror of
https://gitlab.linphone.org/BC/public/linphone-iphone.git
synced 2026-01-17 02:58:07 +00:00
Fix Share extension with unsaved screenshot
This commit is contained in:
parent
750c27001f
commit
3ce27ad496
1 changed files with 56 additions and 33 deletions
|
|
@ -49,11 +49,13 @@ class ShareViewController: SLComposeServiceViewController {
|
|||
if provider.hasItemConformingToTypeIdentifier("public.item") {
|
||||
remainingSlots -= 1
|
||||
dispatchGroup.enter()
|
||||
provider.loadFileRepresentation(forTypeIdentifier: "public.item") { urlFile, error in
|
||||
if let url = urlFile {
|
||||
if let urlSaved = self.copyFileToSharedContainer(from: url) {
|
||||
fileURLs.append(urlSaved)
|
||||
}
|
||||
provider.loadItem(forTypeIdentifier: "public.item", options: nil) { item, error in
|
||||
if let url = item as? URL, let saved = self.copyFileToSharedContainer(from: url) {
|
||||
fileURLs.append(saved)
|
||||
} else if let image = item as? UIImage,
|
||||
let data = image.pngData(),
|
||||
let saved = self.saveDataToSharedContainer(data: data) {
|
||||
fileURLs.append(saved)
|
||||
}
|
||||
dispatchGroup.leave()
|
||||
}
|
||||
|
|
@ -91,19 +93,40 @@ class ShareViewController: SLComposeServiceViewController {
|
|||
}
|
||||
|
||||
func copyFileToSharedContainer(from url: URL) -> URL? {
|
||||
let fileManager = FileManager.default
|
||||
guard let sharedContainerURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.org.linphone.phone.linphoneExtension") else {
|
||||
return nil
|
||||
}
|
||||
guard let sharedContainerURL = FileManager.default.containerURL(
|
||||
forSecurityApplicationGroupIdentifier: "group.org.linphone.phone.linphoneExtension"
|
||||
) else { return nil }
|
||||
|
||||
let destinationURL = sharedContainerURL.appendingPathComponent(url.lastPathComponent)
|
||||
|
||||
do {
|
||||
if fileManager.fileExists(atPath: destinationURL.path) {
|
||||
try fileManager.removeItem(at: destinationURL)
|
||||
if FileManager.default.fileExists(atPath: destinationURL.path) {
|
||||
try FileManager.default.removeItem(at: destinationURL)
|
||||
}
|
||||
try FileManager.default.copyItem(at: url, to: destinationURL)
|
||||
|
||||
let attrs = try FileManager.default.attributesOfItem(atPath: destinationURL.path)
|
||||
if let size = attrs[.size] as? NSNumber, size.intValue > 0 {
|
||||
return destinationURL
|
||||
} else {
|
||||
try? FileManager.default.removeItem(at: destinationURL)
|
||||
return nil
|
||||
}
|
||||
} catch {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
try fileManager.copyItem(at: url, to: destinationURL)
|
||||
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