Can't use the Photos framework from within XCTestCase

Hello there,


I am trying to use the photos framework from within a XCTestCase because I dont want to verify that an asset gets saved to the user's photo library when they tap on a button in our app. But app is crashing when I run the test. I did NSPhotoLibraryUsageDescription to my test target's plist but it looks like thats not the right place to add it. Here is the error I am seeing in the console:


XCTRunner[25404:8392034] [access] This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.


So it looks like I need to add NSPhotoLibraryUsageDescription to XCTRunner's plist but how do I do that? Is there an alternative?


thanks,

-Akshay

Replies

It sounds like you’re creating a library test when you should be creating an app test. Testing with Xcode explains that terminology. A library test is run by Xcode infrastructure and you can’t tweak its

Info.plist
. An app test, OTOH, is run within your app, and that gives you control over the
Info.plist
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Perhaps you're trying to access the photo library from a UI test.


Xcode doesn't provide a built-in way to modify the test runner app's Info.plist. (Your UI test bundle has an Info.plist but your UI test bundle is not the main bundle.)


I was able to hack it using a Run Script build phase.


Find your UI test bundle target in your project and go to its Build Phases tab. Add a Run Script build phase. Here's the script:


set -x -e
/usr/libexec/PlistBuddy -c 'Delete :NSPhotoLibraryUsageDescription' $CODESIGNING_FOLDER_PATH/Info.plist || true
/usr/libexec/PlistBuddy -c 'Add :NSPhotoLibraryUsageDescription string Testing' $CODESIGNING_FOLDER_PATH/Info.plist


This worked for me in Xcode 9 beta 3. I don't know if it works in older Xcodes, and it's quite possible that it won't work in future versions.