Objective c 使用NSData时如何处理连接问题?
NSXMLParser的一个init方法是initWithData:(NSData*data)。 当我像这样初始化NSData时Objective c 使用NSData时如何处理连接问题?,objective-c,cocoa,nsdata,nsurl,Objective C,Cocoa,Nsdata,Nsurl,NSXMLParser的一个init方法是initWithData:(NSData*data)。 当我像这样初始化NSData时 NSURL *url = [NSURL URLWithString:@"http://221.34.21.9"]; NSData *date = [[NSData alloc] initWithContentsOfURL:url]; 如果无法访问IP地址, 我的应用程序将永远等待该地址的响应 我怎样才能解决这个问题 谢谢 您需要从实现Internet可访问性。 这是
NSURL *url = [NSURL URLWithString:@"http://221.34.21.9"];
NSData *date = [[NSData alloc] initWithContentsOfURL:url];
如果无法访问IP地址,
我的应用程序将永远等待该地址的响应
我怎样才能解决这个问题
谢谢 您需要从实现Internet可访问性。
这是一项测试,可以让您知道您的internet连接是否可以访问。您必须通过
NSURLConnection
获取数据,该连接将具有超时值,如中所述。这应该给你一个提示;本指南提供了更完整的示例代码:
// Create an NSURLRequest object with your desired URL and timeout value
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://221.34.21.9"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:15.0];
// Begin a connection using that request
// Assign a delegate object that will get callbacks when things happen
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
您可能最感兴趣的是
连接:diReceiveData
和连接:didFailWithError:
非常感谢。这很有帮助!