【问题标题】:App Transport Security issue with AVAudioPlayer loading local file objective-C, XCode 9AVAudioPlayer加载本地文件objective-C,XCode 9的应用程序传输安全问题
【发布时间】:2018-10-23 16:19:45
【问题描述】:

我有一个应用程序,它可以将捆绑的 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

我的代码:

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];

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>

我不能使用 NSExceptionDomain 键,因为这是本地资源,没有域。我该如何解决这个问题?

注意:需要明确的是,此文件已捆绑到应用程序中,并且在任何时候都没有下载过。这不是“http”网址,而是“文件:”网址。

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

另一个更新:该应用程序今天无缘无故开始运行,自上次尝试以来我没有更改任何内容。不幸的是,我无法确定此时修复是否有效!可以肯定的是,我的 plist 键似乎在我的 iOS 11.3 模拟器中工作。

【问题讨论】:

  • 您的应用中有任何 API/网络调用吗?我认为您正在下载歌曲并保存到本地,但由于 ATS,歌曲文件无法保存在本地,因为 HTTP 调用失败,如果我错了,请纠正我。
  • @AbhishekThapliyal -- 你错了,这个文件被捆绑到应用程序中,它没有被下载。
  • 您的应用中还有其他 API 调用吗?
  • 有很多 API 调用,但这是调试器在异常上中断的地方,所以我认为这意味着这就是导致异常的原因。这是一个糟糕的假设吗?
  • 您添加了赏金,但尚未提供聊天的详细信息。请尝试聊天中的内容。 ATS 不应阻止本地文件系统中引用的任何内容。我认为这里正在发生其他事情。专门打开 CFNetwork 日志记录,以便您可以看到它正在尝试加载的 URL。 prepareToPlay 方法的工作方式可能还有另一个问题。

标签: objective-c avaudioplayer app-transport-security xcode9.3 ios11.3


【解决方案1】:

在我的项目中是使用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];

【讨论】:

  • 恐怕问题消失了,您的建议确实有效,但不幸的是我不知道它是否修复了我今天之前看到的错误!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-09
  • 2016-06-07
  • 2016-01-05
  • 2016-04-29
  • 1970-01-01
  • 2016-02-28
  • 2015-09-17
相关资源
最近更新 更多