Wednesday, June 19, 2024
HomeiOS Developmentswift - iOS ShareExtension not notifying fundamental app

swift – iOS ShareExtension not notifying fundamental app


I’m attempting to construct a share extension that permits the person to pick images and share with my app. As soon as they click on share, I’ve a customized UI that prompts for a selection. Relying on the selection they make the principle app ought to open a selected view and go the photographs as arguments to a selected operate inside the view.

I can efficiently save the picture to a shared container. After that, I wish to notify the principle app. The next code is meant to do this (within the extension’s ShareViewController):

personal func notifyMainApp() {
        // Notify the principle app utilizing URL scheme or UserDefaults
        UserDefaults(suiteName: "group.com.mycompany.myapp")?.set(true, forKey: "newImageAvailable")
        
        
        completeRequest()
    }
    
    personal func completeRequest() {
        self.extensionContext?.completeRequest(returningItems: nil, completionHandler: { _ in
                    guard let url = URL(string: "myapp://handleImage") else { return }
                    _ = self.openURL(url)
                })
        print("Finishing")
    }
    
    @objc personal func openURL(_ url: URL) -> Bool {
            var responder: UIResponder? = self
            whereas responder != nil {
                if let software = responder as? UIApplication {
                    return software.carry out(#selector(openURL(_:)), with: url) != nil
                }
                responder = responder?.subsequent
            }
            return false
        }

On my fundamental App’s aspect, that is the AppCode:

import SwiftUI

@fundamental
struct myApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var physique: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class AppDelegate: NSObject, UIApplicationDelegate {
    func applicationDidBecomeActive(_ software: UIApplication) {
        NotificationCenter.default.addObserver(self, selector: #selector(handleSharedImage), title: UserDefaults.didChangeNotification, object: nil)
        handleSharedImage()
    }

    func software(_ app: UIApplication, open url: URL, choices: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        print("Software opened by way of URL scheme: (url)")
        if url.scheme == "myApp" && url.host == "handleImage" {
            handleSharedImage()
            return true
        }
        return false
    }

    @objc func handleSharedImage() {
        print("Dealing with shared picture")
        let userDefaults = UserDefaults(suiteName: "group.com.mycompany.myapp")
        if let selectedType = userDefaults?.string(forKey: "selectedType") {
            print("Discovered chosen kind: (selectedType)")
            let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.yourcompany.yourapp")
            let fileURL = containerURL?.appendingPathComponent("sharedImage.png")

            if let imageData = attempt? Information(contentsOf: fileURL!), let picture = UIImage(information: imageData) {
                // Deal with the picture and the chosen kind (e.g., present it in a view or add it to a server)
                print("Picture loaded from shared container: (picture) with kind: (selectedType)")
                NotificationCenter.default.submit(title: NSNotification.Title("SharedImageHandled"), object: nil, userInfo: ["image": image, "type": selectedType])
            } else {
                print("Didn't load picture information from shared container")
            }

            // Reset the flag
            userDefaults?.removeObject(forKey: "selectedType")
        } else {
            print("No chosen kind present in UserDefaults")
        }
    }
}

The app is efficiently opened however nothing occurs and the logs present me that the URL scheme, regardless of having opened the App, does not set off any logs. I’ve tried to seek out some details about this however most tutorials take care of creating standalone extensions, not extensions that share the app. Any assistance is thus appreciated.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments