Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic shortcuts defined as documents suggest will not work for iOS #2

Open
ccompton-ip opened this issue Jun 25, 2024 · 0 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@ccompton-ip
Copy link

ccompton-ip commented Jun 25, 2024

Documentation states that dynamic shortcuts can be defined as below.

const shortcutItem = {
  id: 'my.awesome.action',
  title: 'Do awesome things',
  shortTitle: 'Do it',
  subtitle: 'iOS only',
  iconName: 'ic_awesome',
  symbolName: 'house.fill', // SF Symbol Name (iOS only)
  data: {
    foo: 'bar',
  },
};

This will throw an unexpected error on iOS

rn_quick_actions/Shortcuts.swift:29: Fatal error: 'try!' expression unexpectedly raised an error: rn_quick_actions.ShortcutsError.invalidShortcutItem

Because iOS requires the keys type and title.

    static func from(_ value: [String: Any]) throws -> UIApplicationShortcutItem? {
        guard
            let type = value["type"] as? String,
            let title = value["title"] as? String
            else {
                throw ShortcutsError.invalidShortcutItem
        }

        let subtitle = value["subtitle"] as? String
        let icon = UIApplicationShortcutIcon.from(value)
        let userInfo = value["data"] as? [String: NSSecureCoding]

        return UIApplicationShortcutItem(
            type: type,
            localizedTitle: title,
            localizedSubtitle: subtitle,
            icon: icon,
            userInfo: userInfo
        )
    }

Workaround would be to define the dynamic shortcut as

const shortcutItem = {
  type: 'my.awesome.action',
  title: 'Do awesome things',
  shortTitle: 'Do it',
  subtitle: 'iOS only',
  iconName: 'ic_awesome',
  symbolName: 'house.fill', // SF Symbol Name (iOS only)
  data: {
    foo: 'bar',
  },
};

Except this will break on Android as it expects a key of id. Android will fail silently and the shortcut will not appear when the app icon is long-pressed.

Defining dynamic shortcut as

const shortcutItem = {
  id: 'my.awesome.action',
  type: 'my.awesome.action',
  title: 'Do awesome things',
  shortTitle: 'Do it',
  subtitle: 'iOS only',
  iconName: 'ic_awesome',
  symbolName: 'house.fill', // SF Symbol Name (iOS only)
  data: {
    foo: 'bar',
  },
};

will work for both platforms but is prone to error.

Suggested fix is to standardize how a dynamic shortcut is defined on RN side, and do proper conversions in the bridging code for the respective platforms.

@yorickshan yorickshan self-assigned this Jul 17, 2024
@yorickshan yorickshan added the bug Something isn't working label Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants