xcode NSURLConnection 以错误结束 - 代码 -1002

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46707634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:28:43  来源:igfitidea点击:

NSURLConnection finished with error - code -1002

xcodensurlconnectionmpmovieplayercontrollerios11

提问by Genevios

Friends i have simple audio player (MPMoviePlayerController) which can play audio stream. On iOS 11 i have very interessing trouble, thousand time i have error and my stream was stopped:

朋友们,我有一个可以播放音频流的简单音频播放器(MPMoviePlayerController)。在 iOS 11 上,我遇到了非常有趣的麻烦,一千次出现错误并且我的流被停止:

NSURLConnection finished with error - code -1002

I paste this code (this code i saw on stackowerflow) but it's not help to me:

我粘贴了这段代码(我在 stackowerflow 上看到的这段代码),但这对我没有帮助:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>cast.mysite.com</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict>

Maybe you know best solution?

也许你知道最好的解决方案?

回答by dgatwood

That error should notbe related to using HTTP instead of HTTPS. App Transport Security failures return error code -1022.

该错误应该不会涉及到使用HTTP,而不是HTTPS的。应用传输安全失败返回错误代码 -1022。

The error code -1002 indicates an invalid URL. Perhaps your HTTP live streaming playlist file contains a structurally invalid URL (e.g. missing scheme, a scheme other than http/https, etc.)?

错误代码 -1002 表示 URL 无效。也许您的 HTTP 直播播放列表文件包含结构上无效的 URL(例如,缺少方案、http/https 以外的方案等)?

For additional debugging, set this environment variable

对于额外的调试,设置这个环境变量

CFNETWORK_DIAGNOSTICS=1

in your Xcode project and re-run the app. Once you know what URL is failing, the problem will likely become more obvious.

在您的 Xcode 项目中并重新运行该应用程序。一旦您知道哪个 URL 出现故障,问题可能会变得更加明显。

If it isn't, file a bug.

如果不是,请提交错误。

回答by Muhammad Bilal ahmad

This issue can appear if your URL contains spaces. I solved it by replacing the spaces with "%20", and then you can use it safely. The Objective C code to replace the spaces is below.

如果您的 URL 包含空格,则会出现此问题。我通过用“%20”替换空格来解决它,然后你就可以安全地使用它了。替换空格的目标 C 代码如下。

your_url_variable_name = [your_url_variable_name stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

回答by Zeeshan Arif

First thing you must use secure server (server with valid certificate). I'm not sure either it is necessary or not because i never tried to hit server with invalid certificate. You can try this code (not sure it will work for you or not) put this code in Appdelegate.m

首先,您必须使用安全服务器(具有有效证书的服务器)。我不确定是否有必要,因为我从未尝试过使用无效证书访问服务器。你可以试试这个代码(不确定它是否适合你)把这个代码放在 Appdelegate.m

@implementation NSURLRequest(DataController)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
    return YES;
}
@end