react-native-siri-shortcut: Not working with Scenes

I’ve had to convert my AppDelegate to Swift, because my radio station app uses react-native-carplay, which now requires use of iOS Scenes which in turns requires a Swift AppDelegate.

Everything is working fine except for react-native-siri-shortcut (it was working fine before this).

I can add a shortcut still, but when I say “Hey Siri, play [name of station]” - the app opens, but getInitialShortcut returns null. Same goes for using addShortcutListener while the app is open - the event is never triggered.

Here’s what AppDelegate.swift looks like. I used Swiftify to convert react-native-siri-shortcut’s bit (and others) to Swift.

import UIKit
import CarPlay
import React

...

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate {

  var window: UIWindow?
  var bridge: RCTBridge?;
  var rootView: RCTRootView?;

  static var shared: AppDelegate { return UIApplication.shared.delegate as! AppDelegate }

  func sourceURL(for bridge: RCTBridge!) -> URL! {
    #if DEBUG
    return RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index");
    #else
    return Bundle.main.url(forResource:"main", withExtension:"jsbundle")
    #endif
  }

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    ...
    
    self.bridge = RCTBridge(delegate: self, launchOptions: launchOptions)

    let rootView = RCTRootView(bridge: self.bridge!, moduleName: "aiirmobile", initialProperties: nil)
    
    if #available(iOS 13.0, *) {
        rootView.backgroundColor = UIColor.systemBackground
    } else {
        rootView.backgroundColor = UIColor.white
    }
    
    self.rootView = rootView

    ...

    return true
  }

  func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    if (connectingSceneSession.role == UISceneSession.Role.carTemplateApplication) {
      let scene =  UISceneConfiguration(name: "CarPlay", sessionRole: connectingSceneSession.role)
      scene.delegateClass = CarSceneDelegate.self
      return scene
    } else {
      let scene =  UISceneConfiguration(name: "Phone", sessionRole: connectingSceneSession.role)
      scene.delegateClass = PhoneSceneDelegate.self
      return scene
    }
  }

  func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  }

  ...

  // Added for react-native-siri-shortcut
  func application(
      _ application: UIApplication,
      continue userActivity: NSUserActivity,
      restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void
  ) -> Bool {
      return RNSSSiriShortcuts.application(application, continue: userActivity, restorationHandler: restorationHandler)
  }
}

Then in myapp-Bridging-Header.h I have:

#import <React/RCTBridge.h>
#import <React/RCTRootView.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTAppSetupUtils.h>

#import <RNSiriShortcuts/RNSiriShortcuts.h>
#import "RNCarPlay.h"

Any thoughts or guidance would be greatly appreciated!

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 31 (31 by maintainers)

Most upvoted comments

Yeah, that seems like a bug on the OS or API side tbh. Btw, I forgot to add the new example project’s folder to .npmignore, so the size of the library in versions 3.2.0…3.2.1 is too big.

You should install version 3.2.2 which fixes this issue, sorry about that

Oh that’s interesting, thanks. I would have had no idea how to do that, or whether it’s a good idea. Thanks!

Well good news. I have got this working!

I would like to share the solution with you, but I’m wondering what the best form for a PR would be.

As the AppDelegate is now in Swift, it wouldn’t make sense to modify the existing example - I think there needs to be a separate Swift/Scenes example. It can be a duplicate of the existing example project, just with the necessary changes.

The problem with this approach is you won’t be easily able to see the differences in a PR. But I’m not sure of an alternative?

In terms of changes to the actual Siri Shortcut library itself, these are purely additions to ios/RNSSSiriShortcuts.h and ios/RNSSSiriShortcuts.m - so it should be possible to merge these without any problems caused to people who don’t need to use Scenes.

Amazing, thank you for your hard work. A new example project for scenes is perfectly fine, just note on the PR which files in the example project are relevant to these changes.

Whenever you can, submit a PR with these fixes please 😄

Thanks for leaving it open, I will definitely give an update once I land on the reason.