Skip to content

AdditionalAppExtensions: .entitlements file location - better lookup or error #19242

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

Closed
tipa opened this issue Oct 18, 2023 · 14 comments · Fixed by #22882
Closed

AdditionalAppExtensions: .entitlements file location - better lookup or error #19242

tipa opened this issue Oct 18, 2023 · 14 comments · Fixed by #22882
Assignees
Labels
enhancement The issue or pull request is an enhancement
Milestone

Comments

@tipa
Copy link

tipa commented Oct 18, 2023

Steps to Reproduce

  1. Xcode: File -> New -> Project -> iOS App
  2. Xcode: File -> New -> Target -> (Share) Extensions
  3. Xcode: Select target "Share Extension" & add capability (e.g. App Group, which is very common for extensions)

The last step will create a NAME_OF_EXTENSION.entitlements file in the folder of the extension

When using AdditionalAppExtensions, the documentation links to this example project where the AdditionalAppExtensions Include path points to the project folder, where the .xcodeproj is located.

Expected Behavior

  • App extension is signed correctly & and can use the entitlements granted (e.g. can use the App Group folder).
  • The expected location of the Entitlements file is documented and/or
  • The build process errors out if it expects an Entitlements file but it cannot be found and/or
  • The build process is smarter in finding the Entitlements file, e.g. also looks in the extension-subfolder or searches all subfolders recursively

Actual Behavior

The app extension is not signed correctly and crashes when trying to use features granted by the entitlement.
It appears that this condition fails:
https://github.com/xamarin/xamarin-macios/blob/17523c3e4a6dc94db0731bde06eff3d01cc85cc2/msbuild/Xamarin.Shared/Xamarin.Shared.targets#L2294

The build process expects the .entitlements file to be located in the project folder, but it is located in the extension subfolder. In the build process this shows by a missing "--entitlements "path/to/entitlements" part in the _CodesignAppBundle step.

I have encountered this problem yesterday and it took lots of time to find the cause of why the extension didn't work correctly. It appears that most developers trying to use AdditionalAppExtensions face this problem, as can be seen here.

Environment

.NET 8 RC2

@rolfbjarne rolfbjarne added this to the .NET 9 milestone Oct 18, 2023
@rolfbjarne rolfbjarne added the enhancement The issue or pull request is an enhancement label Oct 18, 2023
@github-project-automation github-project-automation bot moved this to Bugs in .NET 9 Aug 27, 2024
@rolfbjarne rolfbjarne modified the milestones: .NET 9, .NET 10 Sep 27, 2024
@rolfbjarne rolfbjarne removed this from .NET 9 Sep 27, 2024
@qasimmajeed
Copy link

@tipa do we have any workaround of it?

@qasimmajeed
Copy link

@tipa i think we must need the feature to have multiple entitlements for different build configuration.

@tipa
Copy link
Author

tipa commented Nov 13, 2024

@qasimmajeed my .csproj looks something like this:

  <ItemGroup>
    <AdditionalAppExtensions Include="../native">
      <Name>ShareExtension</Name>
      <BuildOutput Condition="'$(ComputedPlatform)' == 'iPhone'">build/$(Configuration)-iphoneos</BuildOutput>
      <BuildOutput Condition="'$(ComputedPlatform)' == 'iPhoneSimulator'">build/$(Configuration)-iphonesimulator</BuildOutput>
    </AdditionalAppExtensions>
  </ItemGroup>

The native folder is where I have my xcodeproj. My .entitlements files are placed directly in that folder (not in subfolders of it, where it is placed by default when creating a new project in XCode).

@qasimmajeed
Copy link

@tipa Thanks i have done the same thing from my side and its working as expected. I am just wondering if we have the build configuration different for staging or dev environment how the entitlement will work for the same because they could have different key chain group, so is there any way that we can define the different entitlements here to pick it from the root directory basis on the build configuration?

@tipa
Copy link
Author

tipa commented May 8, 2025

I'm struggling with this again today. It would be great if we could manually specify the entitlements path manually. In my case I have a widget extension that I want to share between iOS and macOS, but since it requires two different entitlement files (e.g. macOS needs sandbox entitlement while iOS doesn't), I also need to maintain two different extension projects in Xcode, in order to get two different "product names" (.appex files), one for iOS and one for macOS

@rolfbjarne
Copy link
Member

@tipa can you try changing your local version of Xamarin.Shared.targets like this:

https://gist.github.com/rolfbjarne/cfe67a2735339725330549824023ddbf

This should make it possible to set the CodesignEntitlements property on your AdditionalAppExtensions item to the path of the entitlements file for that app extension.

@tipa
Copy link
Author

tipa commented May 8, 2025

@rolfbjarne where is that file located? Is it possible to "overwrite" the target in just my project folder (e.g. in my csproj) without having to edit files from the toolchain that might change again as I update .NET workloads?

@rolfbjarne
Copy link
Member

Adding this to the csproj might work (by overriding the existing target we ship):

https://gist.github.com/rolfbjarne/637575e393f78e08a28869aa464a9361

@tipa
Copy link
Author

tipa commented May 8, 2025

I added that snippet into my csproj (as direct child of Project) but it seems to be ignored completely, unless I am doing something wrong. E.g. I renamed my appex file on the file system and made the same name change to the snippet, when building it still tries to copy the original appex file (from %(AdditionalAppExtensions.Name).appex) and errors out

@rolfbjarne
Copy link
Member

Can you upload a binlog of your build?

@tipa
Copy link
Author

tipa commented May 8, 2025

In case someone else faces the same problem, I was able to workaround it. Instead of forcing the .NET toolchain to pick another .appex or entitlements file, I provide a PRODUCT_NAME when building it in the command line - this will create WidgetExtensionMac.appex in my case:

xcodebuild -project ../native/myswiftapp.xcodeproj -configuration Release PRODUCT_NAME=WidgetExtensionMac -target WidgetExtension -sdk macosx

@ivmirx
Copy link

ivmirx commented May 8, 2025

@tipa thank you, how do you get it signed from .xcodeproj config? I've been trying to add a simple sticker extension via xcodebuild but then spctl -a -vv iOS.app/iOS invalidates the whole bundle no matter if I re-sign it or not (and this is the check the App Store uses on uploads)

@tipa
Copy link
Author

tipa commented May 9, 2025

@ivmirx I have set the Entitlement in Xcode but you could probably also do that in the call to xcodebuild

Image

But then .NET re-signs your package when building your app (if it finds your Entitlements file!)

rolfbjarne added a commit that referenced this issue May 22, 2025
#19242.

* Add support for a 'CodesignEntitlements' metadata to the 'AdditionalAppExtensions' item group, to make it easier to specify the entitlements for such an app extension.
* Warn if no entitlements are set for app extensions in 'AdditionalAppExtensions' items.

Fixes #19242.
@rolfbjarne
Copy link
Member

A fix is in progress, which:

  • Documents the AdditionalAppExtensions item group a little bit better + all the supported metadata.
  • Adds support for a CodesignEntitlements metadata, so you can choose your own location for the entitlements for an app extension.
  • Warns if no entitlements are set for an app extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement The issue or pull request is an enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants