AVAudioPlayer的App Transport Security问题加载本地文件objective-C,XCode 9

Ala*_*ore 8 objective-c avaudioplayer app-transport-security xcode9.3 ios11.3

我有一个应用程序,它将捆绑的m4a音频文件作为本地资源加载,并且它已经运行了很多年了.我正在将应用程序更新到iOS 11.3/XCode 9.3,当我按下我的播放按钮时,它现在在iPad上失败(适用于iPhone):

2018-05-13 20:45:24.437626-0700 my app[6175:218735] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
2018-05-13 20:45:24.437791-0700 my app[6175:218735] Cannot start load of Task <117E064E-ABB3-45F2-8D64-76397B140092>.<0> since it does not conform to ATS policy
2018-05-13 20:45:24.437948-0700 my app[6175:218732] NSURLConnection finished with error - code -1022
Run Code Online (Sandbox Code Playgroud)

我的代码:

NSURL *medURL   = [[NSBundle mainBundle] URLForResource: @"MyFile"
                                          withExtension: @"m4a"];
NSError *playerError = nil;
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: medURL error: &playerError];
self.appSoundPlayer = newPlayer;

// "Preparing to play" attaches to the audio hardware and ensures that playback
//      starts quickly when the user taps Play
[appSoundPlayer prepareToPlay];
Run Code Online (Sandbox Code Playgroud)

它失败了prepareToPlay.

阅读其他答案我发现了很多有关ATS错误的信息,所以我将其添加到我的应用程序的plist文件中,清理,重建并运行它 - 但我仍然收到错误:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsLocalNetworking</key>
    <true/>
    <key>NSAllowsArbitraryLoadsForMedia</key>
    <true/>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
    <key>NSExceptionAllowsInsecureHTTPLoads</key>
    <true/> </dict>
Run Code Online (Sandbox Code Playgroud)

我无法使用NSExceptionDomain密钥,因为这是一个本地资源,没有域.我怎样才能解决这个问题?

注意:要清楚,此文件已捆绑到应用程序中,并且未随时下载.这不是"http"网址,而是"文件:"网址.

更新:看看CFNetworkLog我意识到NSURLConnection在音频准备之前有一个网络正在发生AppDelegate.当我删除此调用时,一切都开始工作.由于两者都是独立工作的,在我看来NSURLConnection,正在发生某种冲突- 可能是API中的一个错误?很高兴知道正确的解决方法是什么,我已经通过删除prepareToPlay上面的调用使其工作得很好- 这并不理想,因为用户去播放音频文件需要更长的时间.

另一个更新:应用程序今天开始工作没有明显的原因,自从我上次尝试它以来我没有改变任何东西.不幸的是,我无法确定此时修复是否有效!可以肯定的是,我的plist键确实在我的iOS 11.3模拟器中工作.

cha*_*unv 5

在我的项目中使用AVAudioPlayer播放音频,我的源代码:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"MyFile"  ofType:@"m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1; //Infinite
[audioPlayer setVolume:1];
[audioPlayer play];
Run Code Online (Sandbox Code Playgroud)