Skip to content

Bug: Platform Analyzer (CA1416) ignores OperatingSystem.IsIOSVersionAtLeast guard clause #30681

@yuki2006

Description

@yuki2006

Description

Hello .NET MAUI Team,

I'd like to report an issue where the platform compatibility
analyzer (CA1416) incorrectly flags a platform-specific API
call even when it is properly protected by an
OperatingSystem.IsIOSVersionAtLeast() check.

This results in a false-positive warning (which is promoted to
an error in the sample project), preventing the project from
building when targeting an older iOS version, despite the code
being perfectly safe.

        if (OperatingSystem.IsIOSVersionAtLeast(13, 0))
        {
            var request = new BackgroundTasks.BGAppRefreshTaskRequest("com.companyname.mauibugrepro.refreshtask");
        }

Steps to Reproduce

  1. Clone the reproduction repository.
  2. Navigate to the project's root directory.
  3. Run the following command in your terminal:
    dotnet build -f net9.0-ios
    -p:RuntimeIdentifier=iossimulator-x64

Link to public reproduction project repository

https://github.com/yuki2006/maui_sample

Version with bug

9.0.50 SR5

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

Wrap the code with #pragma

Relevant log output

dotnet build -f net9.0-ios
     -p:RuntimeIdentifier=iossimulator-x64
  MauiBugRepro net9.0-ios 1 件のエラーと 1 件の警告で失敗しました (1.7 秒)
    /usr/local/share/dotnet/packs/Microsoft.iOS.Sdk.net9.0_18.5/18.5.9207/tools/msbuild/Xamarin.Shared.targets(609,3): warning :
      サポートされている iPhone の向きが設定されていません

    /Users/admin/maui_bug/MauiBugRepro/Platforms/iOS/AppDelegate.cs(18,27): error CA1416: この呼び出しサイトは 'iOS' 12.2 以降, 'maccatalyst' 13.0 以降 で到達可能です。'BGAppRefreshTaskRequest''ios' 13.0 以降 でのみサポートされています。 (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Metadata

Metadata

Assignees

No one assigned

    Labels

    partner/maciosIssues for the Mac / iOS SDKplatform/ioss/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions