Skip to content

[iOS 15] App crashes when setting attributedTitle on UIAction #163

@nicolasdevienne

Description

@nicolasdevienne

Description

The app crashes on iOS 15 devices when building context menus with UIAction because the property attributedTitle does not exist before iOS 16.


Steps to Reproduce

  1. Open the app on a device running iOS 15.
  2. Trigger a context menu that sets a custom title color using attributedTitle.
  3. The app crashes with NSUnknownKeyException.

Crash Log

Fatal Exception: NSUnknownKeyException
[UIAction setValue:forKey:]: this class is not key value coding-compliant for the key attributedTitle

Expected Behavior

  • On iOS 15: the menu should still show, falling back to the system default title style.
  • On iOS 16+: the title should display with the custom color applied via attributedTitle.

Actual Behavior

  • App crashes on iOS 15 due to attributedTitle being unavailable.

Root Cause

  • UIAction.attributedTitle was only introduced in iOS 16.
  • Using setValue:forKey:@"attributedTitle" on iOS 15 leads to NSUnknownKeyException.

Fix / Suggested Solution

Wrap the code with an iOS availability check:

if (@available(iOS 16.0, *)) {
    NSDictionary *attributes = @{NSForegroundColorAttributeName: titleColor};
    NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:action.title attributes:attributes];
    [actionMenuItem setValue:attributedTitle forKey:@"attributedTitle"];
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions