Search code examples
iosobjective-ciphonelocationphasset

(PHAsset location) returns (null) for Camera Roll photo even though location services are enabled


I need to access the GPS coordinates of user-submitted photos that originate from the user's camera roll/photo library. I'm implementing PHAsset instead of ALAsset for the sake of modernity.

I know that location services ARE enabled because I can print out the user's current location and have added the "NSLocationAlwaysUsageDescription", "NSLocationWhenInUseDescription", and "privacy - location usage description" keys to my info.plist file. I've imported CoreLocation, MobileCoreServices, and Photos in the .h file for my ViewController.

My current code in the didFinishPickingMediaWithInfo delegate method is as follows:

NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
PHFetchResult *res = [PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:url] options:nil];
PHAsset *thisPhoto = [res objectAtIndex: 0];
CLLocation *photoLocation = [thisPhoto location];
NSLog(@"%@", photoLocation);

For some reason, this always outputs (null). Is there something I'm missing that's stopping me from accessing PHAsset location data? I know that the PHAsset is being properly assigned because [PHAsset creationDate] works fine for any photo that I choose.

Thanks!


Solution

  • You do have access to PHAsset location data (provided it is available). The user could have chosen to not allow the camera app to access location services which results in photos without location data.