title: App Transport Security issue with AVAudioPlayer loading local file objective-C, XCode 9
body: <p>I have an app which loads a bundled m4a audio file as a local resource and it has worked well for many years now.  I'm updating the app to iOS 11.3/XCode 9.3 and it is now failing on iPad (works on iPhone) when I press my play button:</p>
  
  <pre><code>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 &lt;117E064E-ABB3-45F2-8D64-76397B140092&gt;.&lt;0&gt; 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
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>NSURL *medURL   = [[NSBundle mainBundle] URLForResource: @"MyFile"
                                            withExtension: @"m4a"];
  NSError *playerError = nil;
  AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: medURL error: &amp;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];
  </code></pre>
  
  <p>It fails on the <code>prepareToPlay</code>.  </p>
  
  <p>Reading other answers I've found a lot of information on the ATS error so I added this to my plist file for the app, cleaned, rebuilt and ran it -- but I am still getting the error:</p>
  
  <pre><code>&lt;key&gt;NSAppTransportSecurity&lt;/key&gt;
  &lt;dict&gt;
      &lt;key&gt;NSAllowsLocalNetworking&lt;/key&gt;
      &lt;true/&gt;
      &lt;key&gt;NSAllowsArbitraryLoadsForMedia&lt;/key&gt;
      &lt;true/&gt;
      &lt;key&gt;NSAllowsArbitraryLoads&lt;/key&gt;
      &lt;true/&gt;
      &lt;key&gt;NSAllowsArbitraryLoadsInWebContent&lt;/key&gt;
      &lt;true/&gt;
      &lt;key&gt;NSExceptionAllowsInsecureHTTPLoads&lt;/key&gt;
      &lt;true/&gt; &lt;/dict&gt;
  </code></pre>
  
  <p>I can't use the NSExceptionDomain key because this is a local resource, there is no domain.  How can I fix this?</p>
  
  <p>Note: to be clear, this file is bundled into the app and has not been downloaded at any time.  This is not an "http" url, it is a "file:" url.</p>
  
  <p>Update: Looking at the <a href="https://useyourloaf.com/blog/app-transport-security/" rel="nofollow noreferrer">CFNetworkLog</a> I realized that there was a <code>NSURLConnection</code> to the network happening just before the audio prep in <code>AppDelegate</code>.  When I removed this call everything started working.  Since both work independently, it appears to me that there is some sort of conflict in <code>NSURLConnection</code> which is happening -- possibly a bug in the API?  It would be good to know what the proper work-around is for this, I have gotten it to work well enough by removing the <code>prepareToPlay</code> call above -- this is not ideal as it takes longer when the user goes to play the audio file.</p>
  
  <p>Another update: the app started working today for no apparent reason, I hadn't changed anything since the last time I tried it.  Unfortunately I can't determine if fixes work at this point!  To be sure, it does appear that my plist keys are working in my iOS 11.3 simulator.</p>