Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix file sharing via share ext

Zack Bartel5bd12b3e559d
diff --git a/apple/Tether.xcodeproj/project.pbxproj b/apple/Tether.xcodeproj/project.pbxproj
index 33593a8..64119ec 100644
--- a/apple/Tether.xcodeproj/project.pbxproj
+++ b/apple/Tether.xcodeproj/project.pbxproj
@@ -502,7 +502,7 @@
AUTOMATION_APPLE_EVENTS = NO;
CODE_SIGN_ENTITLEMENTS = Tether/Tether.entitlements;
CODE_SIGN_STYLE = Automatic;
- CURRENT_PROJECT_VERSION = 8;
+ CURRENT_PROJECT_VERSION = 9;
DEVELOPMENT_TEAM = BN376Y4X83;
ENABLE_APP_SANDBOX = YES;
ENABLE_FILE_ACCESS_DOWNLOADS_FOLDER = readwrite;
@@ -570,7 +570,7 @@
AUTOMATION_APPLE_EVENTS = NO;
CODE_SIGN_ENTITLEMENTS = Tether/Tether.entitlements;
CODE_SIGN_STYLE = Automatic;
- CURRENT_PROJECT_VERSION = 8;
+ CURRENT_PROJECT_VERSION = 9;
DEVELOPMENT_TEAM = BN376Y4X83;
ENABLE_APP_SANDBOX = YES;
ENABLE_FILE_ACCESS_DOWNLOADS_FOLDER = readwrite;
@@ -737,7 +737,7 @@
buildSettings = {
CODE_SIGN_ENTITLEMENTS = TetherShareExtension/TetherShareExtension.entitlements;
CODE_SIGN_STYLE = Automatic;
- CURRENT_PROJECT_VERSION = 8;
+ CURRENT_PROJECT_VERSION = 9;
DEVELOPMENT_TEAM = BN376Y4X83;
GENERATE_INFOPLIST_FILE = NO;
INFOPLIST_FILE = TetherShareExtension/Info.plist;
@@ -762,7 +762,7 @@
buildSettings = {
CODE_SIGN_ENTITLEMENTS = TetherShareExtension/TetherShareExtension.entitlements;
CODE_SIGN_STYLE = Automatic;
- CURRENT_PROJECT_VERSION = 8;
+ CURRENT_PROJECT_VERSION = 9;
DEVELOPMENT_TEAM = BN376Y4X83;
GENERATE_INFOPLIST_FILE = NO;
INFOPLIST_FILE = TetherShareExtension/Info.plist;
diff --git a/apple/TetherShareExtension/ShareViewController.swift b/apple/TetherShareExtension/ShareViewController.swift
index 0dd8445..cd55882 100644
--- a/apple/TetherShareExtension/ShareViewController.swift
+++ b/apple/TetherShareExtension/ShareViewController.swift
@@ -58,39 +58,66 @@ final class ShareViewController: UIViewController {
let provider = attachments[0]
- // Check for plain text first
- if provider.hasItemConformingToTypeIdentifier(UTType.plainText.identifier) {
- provider.loadItem(forTypeIdentifier: UTType.plainText.identifier, options: nil) { item, _ in
+ // 1. Explicit File URL (Files app, high-quality Photo Library shares)
+ if provider.hasItemConformingToTypeIdentifier(UTType.fileURL.identifier) {
+ provider.loadItem(forTypeIdentifier: UTType.fileURL.identifier, options: nil) { item, _ in
DispatchQueue.main.async {
- let text = (item as? String) ?? (item as? URL)?.absoluteString ?? ""
- completion(.text(text))
+ if let url = item as? URL {
+ let isSecured = url.startAccessingSecurityScopedResource()
+ defer { if isSecured { url.stopAccessingSecurityScopedResource() } }
+ let filename = url.lastPathComponent
+ let data = (try? Data(contentsOf: url)) ?? Data()
+ completion(.file(data, filename: filename))
+ } else {
+ completion(.unsupported)
+ }
}
}
return
}
- // Check for URL (treat as text)
+ // 2. General URL (Could be web link or disguised file URL)
if provider.hasItemConformingToTypeIdentifier(UTType.url.identifier) {
provider.loadItem(forTypeIdentifier: UTType.url.identifier, options: nil) { item, _ in
DispatchQueue.main.async {
- let text = (item as? URL)?.absoluteString ?? ""
+ if let url = item as? URL, url.isFileURL {
+ let isSecured = url.startAccessingSecurityScopedResource()
+ defer { if isSecured { url.stopAccessingSecurityScopedResource() } }
+ let filename = url.lastPathComponent
+ let data = (try? Data(contentsOf: url)) ?? Data()
+ completion(.file(data, filename: filename))
+ } else {
+ let text = (item as? URL)?.absoluteString ?? (item as? String) ?? ""
+ completion(.text(text))
+ }
+ }
+ }
+ return
+ }
+
+ // 3. Plain Text (Must be checked after URLs so we don't accidentally treat links as raw strings too early, though order here is flexible)
+ if provider.hasItemConformingToTypeIdentifier(UTType.plainText.identifier) {
+ provider.loadItem(forTypeIdentifier: UTType.plainText.identifier, options: nil) { item, _ in
+ DispatchQueue.main.async {
+ let text = (item as? String) ?? ""
completion(.text(text))
}
}
return
}
- // Check for image
+ // 4. Image fallback (If it didn't conform to fileURL, like in-memory UIImages)
if provider.hasItemConformingToTypeIdentifier(UTType.image.identifier) {
provider.loadItem(forTypeIdentifier: UTType.image.identifier, options: nil) { item, _ in
DispatchQueue.main.async {
- if let url = item as? URL {
+ if let image = item as? UIImage, let data = image.jpegData(compressionQuality: 0.9) {
+ completion(.file(data, filename: "image.jpg"))
+ } else if let url = item as? URL {
+ let isSecured = url.startAccessingSecurityScopedResource()
+ defer { if isSecured { url.stopAccessingSecurityScopedResource() } }
let filename = url.lastPathComponent
let data = (try? Data(contentsOf: url)) ?? Data()
completion(.file(data, filename: filename))
- } else if let image = item as? UIImage,
- let data = image.jpegData(compressionQuality: 0.9) {
- completion(.file(data, filename: "image.jpg"))
} else {
completion(.unsupported)
}
@@ -99,14 +126,18 @@ final class ShareViewController: UIViewController {
return
}
- // Generic file
+ // 5. Generic Data (Other file representations)
if provider.hasItemConformingToTypeIdentifier(UTType.data.identifier) {
provider.loadItem(forTypeIdentifier: UTType.data.identifier, options: nil) { item, _ in
DispatchQueue.main.async {
if let url = item as? URL {
+ let isSecured = url.startAccessingSecurityScopedResource()
+ defer { if isSecured { url.stopAccessingSecurityScopedResource() } }
let filename = url.lastPathComponent
let data = (try? Data(contentsOf: url)) ?? Data()
completion(.file(data, filename: filename))
+ } else if let data = item as? Data {
+ completion(.file(data, filename: "shared_file.bin"))
} else {
completion(.unsupported)
}