i创建一个简单的浏览器,可以使用UIWebview加载本地文件。首先,当我尝试预览html文件时,uiwebview可以加载源文件并对其进行预览。但是,当我最小化应用程序(应用程序进入后台),然后再次打开应用程序后,出现了此错误:
Error Dict: {
NSNetServicesErrorCode = "-72000";
NSNetServicesErrorDomain = 10;
}
之后,uiwebview无法加载源文件,当我在(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
中记录错误时,它显示此消息:
Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo=0x53d610 {NSErrorFailingURLStringKey=http://localhost:9898/local/a.html?83C66B33-874C-41A7-BBF5-78D1615512DF, NSErrorFailingURLKey=http://localhost:9898/local/a.html?83C66B33-874C-41A7-BBF5-78D1615512DF, NSLocalizedDescription=Could not connect to the server., NSUnderlyingError=0x5ccaa0 "Could not connect to the server."}
应用程序没有崩溃,但旋转指示器从未停止。
有人可以告诉我这是什么原因吗?以及如何解决?
谢谢:)
如果您查看NSNetServices
标头内部,将看到以下枚举说明了每个错误:
typedef NS_ENUM(NSInteger, NSNetServicesError) {
/* An unknown error occured during resolution or publication.
*/
NSNetServicesUnknownError = -72000L,
/* An NSNetService with the same domain, type and name was already present when the publication request was made.
*/
NSNetServicesCollisionError = -72001L,
/* The NSNetService was not found when a resolution request was made.
*/
NSNetServicesNotFoundError = -72002L,
/* A publication or resolution request was sent to an NSNetService instance which was already published or a search request was made of an NSNetServiceBrowser instance which was already searching.
*/
NSNetServicesActivityInProgress = -72003L,
/* An required argument was not provided when initializing the NSNetService instance.
*/
NSNetServicesBadArgumentError = -72004L,
/* The operation being performed by the NSNetService or NSNetServiceBrowser instance was cancelled.
*/
NSNetServicesCancelledError = -72005L,
/* An invalid argument was provided when initializing the NSNetService instance or starting a search with an NSNetServiceBrowser instance.
*/
NSNetServicesInvalidError = -72006L,
/* Resolution of an NSNetService instance failed because the timeout was reached.
*/
NSNetServicesTimeoutError = -72007L,
};
您遇到服务冲突错误,这意味着具有相同域,类型和名称的网络服务已经在网络上发布了它们的服务。
通常,我总是在头文件中查询错误代码。它们几乎总是由一个枚举值分配,并且该枚举通常具有比使用的数字更具描述性的名称。
问题似乎是您尝试连接的主机:http://localhost:9898/local/[..]
您计算机上的本地主机是您的计算机,ipad上的本地主机是您的ipad-测试使用可解析的域名或IP地址。
Sergiu Todirascu在问题中的评论为我解决了。 Mac Sandbox造成了这种情况,我不得不在“权限”选项卡中打开“网络”,就我的情况而言为“入站和出站网络”复选框。创建一个答案,这样更容易看到。