菜鸟教程小白 发表于 2022-12-11 20:11:58

ios - 当 NSAllowsArbitraryLoadsInWebContent 为真时,无法使用 AVPlayer 播放视频


                                            <p><p>我有一个带有 <code>AVPlayer</code> 的非常小的应用程序。我正在使用以下代码播放视频:</p>

<pre><code>if let urlToPlay = URL(string: &#34;http://some_video.m3u8&#34;) {

    self.player = AVPlayer.init(url: urlToPlay)
    self.player!.play()
}
</code></pre>

<p>我还在“Info.plist”文件中添加了一些行,以便能够播放带有 HTTP 架构的视频:</p>

<pre><code>&lt;key&gt;NSAppTransportSecurity&lt;/key&gt;
&lt;dict&gt;
    &lt;key&gt;NSAllowsArbitraryLoads&lt;/key&gt;
    &lt;true/&gt;
&lt;/dict&gt;
</code></pre>

<p>而且效果很好。但后来我发现了一个名为 <code>NSAllowsArbitraryLoadsInWebContent</code> 的标志。我的应用程序中没有任何 <code>WKWebView</code> 或 <code>UIWebView</code> 实例,因此我认为使用此标志不会导致我的应用程序出现任何崩溃或错误。现在我的文件“Info.plist”有这个片段:</p>

<pre><code>&lt;dict&gt;
    &lt;key&gt;NSAllowsArbitraryLoads&lt;/key&gt;
    &lt;true/&gt;
    &lt;key&gt;NSAllowsArbitraryLoadsInWebContent&lt;/key&gt;
    &lt;true/&gt;
&lt;/dict&gt;
</code></pre>

<p>播放器不工作。有人能解释一下为什么会这样吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>对于通过 AVFoundation 加载媒体,添加键 <code>NSAllowsArbitraryLoadsForMedia</code> 并将其设置为 true。</p>

<p>根据文档:</p>

<blockquote>
<p>In iOS 10 and later, and macOS 10.12 and later, the value of this key
is ignored—resulting in an effective value for this key of its default
value of NO—if any of the following keys are present in your app’s
Info.plist file:</p>

<p>NSAllowsArbitraryLoadsForMedia,
NSAllowsArbitraryLoadsInWebContent,
NSAllowsLocalNetworking</p>
</blockquote>

<p>请注意,不鼓励在生产环境中设置任意负载,这些标志会触发 AppStore 审核,并且需要证明这样做的理由。</p>

<p>如需进一步阅读,请访问 <a href="https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33" rel="noreferrer noopener nofollow">ATS Documentation</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 当 NSAllowsArbitraryLoadsInWebContent 为真时,无法使用 AVPlayer 播放视频,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/50506308/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/50506308/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 当 NSAllowsArbitraryLoadsInWebContent 为真时,无法使用 AVPlayer 播放视频