I am attempting to list out the albums in the Photos app, but can't seem to get it working. The code below shows authorized, but both photos
and albums
come back with 0 counts. I have both photos and albums in the Photos app that should be returning. Anybody know what I'm missing here?
public override void DidFinishLaunching(NSNotification notification) { // Insert code here to initialize your application PHPhotoLibrary.RequestAuthorization((status) => { if (status != PHAuthorizationStatus.Authorized) return; Console.WriteLine($"status = {status}"); var photos = PHAsset.FetchAssets(null); Console.WriteLine($"photos.Count = {photos.Count}"); var albums = PHAssetCollection.FetchAssetCollections(PHAssetCollectionType.Album, PHAssetCollectionSubtype.Any, null); Console.WriteLine($"albums.Count = {albums.Count}"); }); }
Answers
I would start by reviewing the Apple sample here.
And read these two, which will help you learn how to translate the apple sample.
https://developer.xamarin.com/guides/mac/application_fundamentals/patterns/
https://developer.xamarin.com/guides/mac/application_fundamentals/mac-apis/
I would also verify that your application is sand boxed with the correct entitlements, as a number of Apple APIs will return zero items if you don't have the correct permissions.
Figured it out. I had to add the following to the Info.plist...
After adding that, macOS properly asked for permission when I started the app the next time and it now gives proper counts.
That key isn't in an easy way to find via the UI. Maybe a clearer way to add that should be in the UI. Just my two cents.
If that key is not listed when you open the entitlements file in VSfM please consider filing an issue.
It's for
Info.plist
, but no I don't see it in the "Application" tab or in the dropdown of easy choices on "Source" tab. I'll add an issue.