Add iOS Share Extension support

- Create share extension to handle shared content from other apps
- Support sharing text, URLs, and prepare for future image support
- Use app groups for data sharing between extension and main app
- Add URL scheme handling for app communication
- Add entitlements for app groups
- Display system message when content is shared
- Update project configuration to include share extension
This commit is contained in:
jack
2025-07-08 04:18:42 +02:00
parent 2afc32afbf
commit 7f6a32e350
7 changed files with 281 additions and 0 deletions
+55
View File
@@ -26,6 +26,14 @@ struct BitchatApp: App {
.environmentObject(chatViewModel)
.onAppear {
NotificationDelegate.shared.chatViewModel = chatViewModel
#if os(iOS)
appDelegate.chatViewModel = chatViewModel
#endif
// Check for shared content
checkForSharedContent()
}
.onOpenURL { url in
handleURL(url)
}
}
#if os(macOS)
@@ -33,10 +41,57 @@ struct BitchatApp: App {
.windowResizability(.contentSize)
#endif
}
private func handleURL(_ url: URL) {
if url.scheme == "bitchat" && url.host == "share" {
// Handle shared content
checkForSharedContent()
}
}
private func checkForSharedContent() {
// Check app group for shared content from extension
guard let userDefaults = UserDefaults(suiteName: "group.chat.bitchat"),
let sharedContent = userDefaults.string(forKey: "sharedContent"),
let sharedDate = userDefaults.object(forKey: "sharedContentDate") as? Date else {
return
}
// Only process if shared within last 30 seconds
if Date().timeIntervalSince(sharedDate) < 30 {
let contentType = userDefaults.string(forKey: "sharedContentType") ?? "text"
// Clear the shared content
userDefaults.removeObject(forKey: "sharedContent")
userDefaults.removeObject(forKey: "sharedContentType")
userDefaults.removeObject(forKey: "sharedContentDate")
userDefaults.synchronize()
// Show notification about shared content
DispatchQueue.main.async {
// Add system message about sharing
let systemMessage = BitchatMessage(
sender: "system",
content: "preparing to share \(contentType)...",
timestamp: Date(),
isRelay: false
)
self.chatViewModel.messages.append(systemMessage)
}
// Send the shared content after a short delay
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
let prefix = contentType == "url" ? "Shared link: " : ""
self.chatViewModel.sendMessage(prefix + sharedContent)
}
}
}
}
#if os(iOS)
class AppDelegate: NSObject, UIApplicationDelegate {
weak var chatViewModel: ChatViewModel?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
return true
}
+11
View File
@@ -40,5 +40,16 @@
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>bitchat</string>
</array>
<key>CFBundleURLName</key>
<string>chat.bitchat</string>
</dict>
</array>
</dict>
</plist>
+12
View File
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.chat.bitchat</string>
</array>
</dict>
</plist>