Iphone 什么是「;stringWithContentsOfURL";替代目标C?

Iphone 什么是「;stringWithContentsOfURL";替代目标C?,iphone,objective-c,Iphone,Objective C,我在网上找到了一个使用stringWithContentsOfURL命令的教程,该命令在iPhoneOS3.0中已被弃用。然而,我不知道我要使用什么,以及如何实现它 下面是围绕stringWithContentsOfURL行的代码,以防您需要它作为参考 NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", [addressFiel

我在网上找到了一个使用stringWithContentsOfURL命令的教程,该命令在iPhoneOS3.0中已被弃用。然而,我不知道我要使用什么,以及如何实现它

下面是围绕stringWithContentsOfURL行的代码,以防您需要它作为参考

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
     [addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];

谢谢。

它已被替换为
stringWithContentsOfURL:encoding:error:
stringWithContentsOfURL:usedEncoding:error:
谢谢Greg,但对于所有其他初学者,这里是一个例子

NSError* error = nil;
NSString* text = [NSString stringWithContentsOfURL:TheUrl encoding:NSASCIIStringEncoding error:&error];
if( text )
{
    NSLog(@"Text=%@", text);
}
else 
{
    NSLog(@"Error = %@", error);
}

对于任何非琐碎的内容,我建议使用HttpRequest:


以下代码将删除您的警告信息……。如有任何问题,请告知mo

- (void) connectedToNetwork 
{
    BOOL aflag = ([NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.co.in/"] encoding:NSASCIIStringEncoding error:nil]!=NULL)?YES:NO; 
    if (!aflag) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Sorry!....You are not connected to network " 
                              delegate:self cancelButtonTitle:@"Exit" otherButtonTitles:nil];
        [alert show];
        [alert release];

    }
}

您好,如果我没有错,即使您的服务器返回内部错误或任何类似错误,文本也不会为FALSE或NIL?这个错误更多的是关于编码错误?苹果在处理连接错误方面非常严格,所以我的问题是,当你不能处理真正的“不可用”错误时,stringWithContentsOfURL:encoding:error:method有什么用?谢谢L@luigi7up我想你是对的。如果存在导致未捕获返回数据的错误(服务器连接错误、编码错误),则返回nil。如果发生404错误,将返回一些文本(404错误页面),错误应包含该错误。我不确定像这样的同步连接会发生什么,但这是异步连接的行为。这不会给其他任何人带来不兼容的指针类型警告吗?只是我吗?