OSLog is not working when launching the app with Siri.

I am implementing AppIntent into my application as follows:

// MARK: - SceneDelegate

    var window: UIWindow?
    private var observer: NSObjectProtocol?
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        
        // Setup window
        window = UIWindow(windowScene: windowScene)
        let viewController = ViewController()
        window?.rootViewController = viewController
        window?.makeKeyAndVisible()
        
        setupUserDefaultsObserver()
        
        checkShortcutLaunch()
    }
    
    private func setupUserDefaultsObserver() {
        // use NotificationCenter to receive notifications.
        NotificationCenter.default.addObserver(
            forName: NSNotification.Name("ShortcutTriggered"),
            object: nil,
            queue: .main
        ) { notification in
            if let userInfo = notification.userInfo,
               let appName = userInfo["appName"] as? String {
                print("📱 Notification received - app is launched: \(appName)")
            }
        }
    }
    
    private func checkShortcutLaunch() {
        if let appName = UserDefaults.standard.string(forKey: "shortcutAppName") {
            print("🚀 App is opened from a Shortcut with the app name: \(appName)")
        }
    }
    
    func sceneDidDisconnect(_ scene: UIScene) {
        if let observer = observer {
            NotificationCenter.default.removeObserver(observer)
        }
    }
}

// MARK: - App Intent

struct StartAppIntent: AppIntent {
    static var title: LocalizedStringResource = "Start App"
    
    static var description = IntentDescription("Launch the application with the command")
    
    static var openAppWhenRun: Bool = true

    @MainActor
    func perform() async throws -> some IntentResult {
        UserDefaults.standard.set("appName", forKey: "shortcutAppName")
        UserDefaults.standard.set(Date(), forKey: "shortcutTimestamp")
        return .result()
    }
}

// MARK: - App Shortcuts Provider

struct AppShortcutsProvider: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: StartAppIntent(),
            phrases: [
                "let start \(.applicationName)",
            ],
            shortTitle: "Start App",
            systemImageName: "play.circle.fill"
        )
    }
}

the app works fine when starting with shortcut. but when starting with siri it seems like the log is not printed out, i tried adding a code that shows a dialog when receiving a notification from userdefault but it still shows the dialog, it seems like the problem here is when starting with siri there is a problem with printing the log.

I tried sleep 0.5s in the perform function and the log was printed out normally

try? await Task.sleep(nanoseconds: 500_000_000) // 0.5 seconds

I have consulted some topics and they said that when using Siri, Intent is running completely separately and only returns the result to Siri, never entering the Main App. But when set openAppWhenRun to true, it must enter the main app, right? Is there any way to find the cause and completely fix this problem?

An App Intent can run in the background without any UI, which means it is common for the system to launch the app without creating a scene. In your code here, you won't always see the print statements log because your app will sometimes run without a scene.

Setting openAppWhenRun is true asks the system to display the app on screen, and create a scene in the process, which is why you see the logs in that scenario instead. However, there's a discrete order to how things proceed during the launch of the app, and the creation of a scene. Your sleep approach works because it's changing the order of when things happen during this launch and scene creation process. Rather than setting and then reading a value in the user defaults, you could directly log from the intent's perform method, as well as the app delegate and scene delegate methods, to see the order of launching your app, creating the scene (when openAppWhenRun is true), and the running of your intent.

— Ed Ford,  DTS Engineer

Thank for your response, I tried to place a log in the perform method as follows:

    @MainActor
    func perform() async throws -> some IntentResult {
        for item in 0...10000 {
            os_log(.debug, "perform boot: \(item)")
        }
        return .result()
    }

Each time I initiate from Siri and fetch logs, the logs appear to be uneven. Most of the time, I can only log from 400 onwards, and after that, the logs are not printed fully. Then I tried using async await when logging:

@MainActor
    func perform() async throws -> some IntentResult {
        for item in 0...10000 {
            await printLog(item)
        }
       
        return .result()
    }

and this time the print seems to work more reliably, but it still doesn't print all the numbers within the for loop (Only able to print around 9500 to 9990 numbers).I understand that app intents run in the extension process and it does not operate reliably for handling heavy and sequential tasks, is that correct?

OSLog is not working when launching the app with Siri.
 
 
Q