0

I have a SwiftUI app (XCode 14 / iOS 15+) that tries to receive fmp12 files sent from the FileMaker app (using the share sheet). I got to the point that my app is shown in the share sheet and gets started. What I cannot achieve is access to the file.

Following some tutorials and Apple's documentation I imported "com.filemaker.fmp12" as an imported type identifier in my plist.info and added it to Document Types.

first strategy: DocumentGroup

To access the file I have first tried to use a DocumentGroup in SwiftUI based on Apple's documentation here: https://developer.apple.com/documentation/swiftui/documentgroup

@main
struct KioskBridgeApp: App {
    
    var body: some Scene {
        DocumentGroup(viewing: FMP12File.self) { file in
            KioskBridgeView(file: file.fileURL?.absoluteString ?? "undefined")
        }
    }

While this works when I send an fmp12 file with the files app, it does not work when I send it from FileMaker: Then it always starts with the Document Browser. The document browser also opens when I start the app without sending anything to it and I could not find a hint how to suppress that.

So I developed the feeling that I might be on the wrong track entirely with DocumentGroup here and so tried I strategy number 2:

second strategy: App delegate

Using an Application Delegate as suggested here https://stackoverflow.com/a/46572886 and as for the Adaptor here: SwiftUI app life cycle iOS14 where to put AppDelegate code? and here https://developer.apple.com/documentation/swiftui/uiapplicationdelegateadaptor

class MyAppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        print("application opened with \(url.absoluteString)")
        return true
    }

@main
struct DelegateTestApp: App {
    @UIApplicationDelegateAdaptor(MyAppDelegate.self) var appDelegate

   var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }

}        

But this delegate is never called, not even when I open my app using the files app.

I am fairly new to iOS development, so even after hours of reading I am not even sure that any of those strategies is even the right approach. Any help would be highly appreciated.

Edson Passos
  • 117
  • 10
norman
  • 599
  • 1
  • 5
  • 14
  • Please add the code of what you have tried so far. – burnsi Sep 23 '22 at 06:52
  • Didn´t downvote. Please consider, the issue you are seeing might very well be in the implementation. If you need a general advice on how to do this, there are a lot of good tutorials out there. Try to follow one that´s more recent and come back if you have a certain problem. If you have problems please add what you have tried and highlight exactly where you struggle. – burnsi Sep 23 '22 at 15:42
  • I tried that and hope it makes the question a bit more useful, too. – norman Sep 23 '22 at 18:51

1 Answers1

0

While I have not figured out ways to make the first two strategies work, more research has finally led to the rather simple solution of .onOpenUrl. This was a useful source to get there: https://betterprogramming.pub/swiftui-3-ways-to-observe-apps-life-cycle-in-swiftui-e9be79e75fef. So my code to catch the file sent to the app is now:

@main
struct KioskBridgeApp: App {
    @State var openedUrl: URL? = nil
    var body: some Scene {
        WindowGroup {
            KioskBridgeView(openedUrl: $openedUrl)
                .onOpenURL { url in
                    openedUrl = url
            }
        }
    }
}

Quite clear once one knows it. After that I also found a stackoverflow question on the topic I had overlooked so far: Handle incoming custom URL types with SwiftUI while app is closed

norman
  • 599
  • 1
  • 5
  • 14