Inability to fetch PHPhotoLibrary photos and albums

robertmiles3robertmiles3 USMember ✭✭

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

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    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.

  • robertmiles3robertmiles3 USMember ✭✭

    Figured it out. I had to add the following to the Info.plist...

    <key>NSPhotoLibraryUsageDescription</key>
    <string>Displays and edits photos to demonstrate the Photos framework.</string>
    

    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.

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    If that key is not listed when you open the entitlements file in VSfM please consider filing an issue.

  • robertmiles3robertmiles3 USMember ✭✭

    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.

Sign In or Register to comment.