Trying to make share extension, have to recreate project
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<?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.application-groups</key>
|
||||
<array>
|
||||
<string>group.com.abunchofknowitalls.gif</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -15,6 +15,12 @@ class MessagesViewController: MSMessagesAppViewController {
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
setupChildViewController()
|
||||
|
||||
// Register for notifications when app becomes active
|
||||
NotificationCenter.default.addObserver(self,
|
||||
selector: #selector(appDidBecomeActive),
|
||||
name: UIApplication.didBecomeActiveNotification,
|
||||
object: nil)
|
||||
}
|
||||
|
||||
private func setupChildViewController() {
|
||||
@@ -90,11 +96,22 @@ class MessagesViewController: MSMessagesAppViewController {
|
||||
// Called when the extension is about to move from the inactive to active state.
|
||||
// This will happen when the extension is about to present UI.
|
||||
|
||||
// Check for GIFs shared from the Share Extension
|
||||
GIFStorageService.shared.checkForSharedGIFs()
|
||||
|
||||
// Refresh GIFs list when becoming active
|
||||
gifCollectionVC?.viewWillAppear(true)
|
||||
}
|
||||
|
||||
override func didResignActive(with conversation: MSConversation) {}
|
||||
override func didResignActive(with conversation: MSConversation) {
|
||||
// No action needed when the extension becomes inactive
|
||||
}
|
||||
|
||||
@objc private func appDidBecomeActive() {
|
||||
// Check for GIFs shared through the Share Extension
|
||||
GIFStorageService.shared.checkForSharedGIFs()
|
||||
gifCollectionVC?.loadGIFs()
|
||||
}
|
||||
override func didReceive(_ message: MSMessage, conversation: MSConversation) {}
|
||||
override func didStartSending(_ message: MSMessage, conversation: MSConversation) {}
|
||||
override func didCancelSending(_ message: MSMessage, conversation: MSConversation) {}
|
||||
|
||||
@@ -11,6 +11,12 @@ class GIFFileManager {
|
||||
// MARK: - File Storage
|
||||
|
||||
private var documentsDirectory: URL {
|
||||
// First try to get the App Group container
|
||||
if let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.gifcollector") {
|
||||
return containerURL
|
||||
}
|
||||
|
||||
// Fall back to the app's documents directory if App Group is not available
|
||||
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
|
||||
return paths[0]
|
||||
}
|
||||
@@ -32,6 +38,11 @@ class GIFFileManager {
|
||||
}
|
||||
|
||||
func storeGIF(data: Data, fromURL urlString: String) -> String? {
|
||||
// Check if this is a shared GIF from the share extension
|
||||
if let sharedGIFPath = checkForSharedGIF(withURL: urlString), FileManager.default.fileExists(atPath: sharedGIFPath) {
|
||||
return sharedGIFPath
|
||||
}
|
||||
|
||||
// Create a unique filename based on the URL hash and timestamp
|
||||
let urlHash = urlString.hashValue
|
||||
let timestamp = Int(Date().timeIntervalSince1970)
|
||||
@@ -129,4 +140,38 @@ class GIFFileManager {
|
||||
print("Error cleaning up old GIFs: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we already have this GIF in the shared container
|
||||
private func checkForSharedGIF(withURL urlString: String) -> String? {
|
||||
guard let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.gifcollector") else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let sharedGIFsFolder = containerURL.appendingPathComponent("GIFs", isDirectory: true)
|
||||
|
||||
guard FileManager.default.fileExists(atPath: sharedGIFsFolder.path) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
do {
|
||||
let fileURLs = try FileManager.default.contentsOfDirectory(at: sharedGIFsFolder, includingPropertiesForKeys: nil)
|
||||
|
||||
// Find any shared GIF that matches our URL (usually won't find any, but helps avoid duplicates)
|
||||
let userDefaults = UserDefaults(suiteName: "group.gifcollector")
|
||||
if let pendingGIFs = userDefaults?.array(forKey: "pendingGIFs") as? [[String: Any]] {
|
||||
for gifInfo in pendingGIFs {
|
||||
if let originURL = gifInfo["originalURL"] as? String,
|
||||
let path = gifInfo["localFilePath"] as? String,
|
||||
originURL == urlString {
|
||||
return path
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
} catch {
|
||||
print("Error checking for shared GIFs: \(error)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,16 @@ class GIFStorageService {
|
||||
|
||||
private let userDefaults = UserDefaults(suiteName: "group.gifcollector")
|
||||
private let savedGIFsKey = "savedGIFs"
|
||||
private let pendingGIFsKey = "pendingGIFs"
|
||||
|
||||
private init() {
|
||||
// Make sure the shared UserDefaults exists
|
||||
if userDefaults == nil {
|
||||
print("Error: Could not create UserDefaults with app group")
|
||||
}
|
||||
|
||||
// Process any pending GIFs from the Share Extension
|
||||
checkForSharedGIFs()
|
||||
}
|
||||
|
||||
func saveGIF(data: Data, fromURL urlString: String, completion: @escaping (GIF?) -> Void) {
|
||||
@@ -86,4 +90,46 @@ class GIFStorageService {
|
||||
func getGIFData(for gif: GIF) -> Data? {
|
||||
return GIFFileManager.shared.loadGIFData(from: gif.localFilePath)
|
||||
}
|
||||
|
||||
// MARK: - Share Extension Integration
|
||||
|
||||
func checkForSharedGIFs() {
|
||||
guard let pendingGIFsData = userDefaults?.array(forKey: pendingGIFsKey) as? [[String: Any]] else {
|
||||
return
|
||||
}
|
||||
|
||||
guard !pendingGIFsData.isEmpty else {
|
||||
return
|
||||
}
|
||||
|
||||
var savedGIFs = fetchGIFs()
|
||||
var newGIFsAdded = false
|
||||
|
||||
for gifInfo in pendingGIFsData {
|
||||
if let localFilePath = gifInfo["localFilePath"] as? String,
|
||||
let originalURL = gifInfo["originalURL"] as? String,
|
||||
let createdAt = gifInfo["createdAt"] as? TimeInterval {
|
||||
|
||||
// Create a GIF object
|
||||
let gif = GIF(
|
||||
localFilePath: localFilePath,
|
||||
originalURL: originalURL
|
||||
)
|
||||
|
||||
// Don't add duplicates
|
||||
if !savedGIFs.contains(where: { $0.localFilePath == localFilePath }) {
|
||||
savedGIFs.append(gif)
|
||||
newGIFsAdded = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save updated GIFs list and clear pending ones
|
||||
if newGIFsAdded {
|
||||
saveToUserDefaults(gifs: savedGIFs)
|
||||
}
|
||||
|
||||
// Clear pending GIFs
|
||||
userDefaults?.removeObject(forKey: pendingGIFsKey)
|
||||
}
|
||||
}
|
||||
@@ -93,7 +93,7 @@ class GIFCollectionViewController: UIViewController {
|
||||
updateEmptyState()
|
||||
}
|
||||
|
||||
private func loadGIFs() {
|
||||
func loadGIFs() {
|
||||
gifs = GIFStorageService.shared.fetchGIFs()
|
||||
collectionView.reloadData()
|
||||
updateEmptyState()
|
||||
|
||||
Reference in New Issue
Block a user