2010-12-21 77 views
0
-(CLLocationCoordinate2D) addressLocation { 

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

    double latitude = 0.0; 
    double longitude = 0.0; 

    if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) { 
     latitude = [[listItems objectAtIndex:2] doubleValue]; 
     longitude = [[listItems objectAtIndex:3] doubleValue]; 
     NSLog(@"listItems %@",[listItems objectAtIndex:2]); 
    } 
    else { 
     //Show error 
    } 
    CLLocationCoordinate2D location; 
    location.latitude = latitude; 
    location.longitude = longitude; 

    return location; 
} 

StringWithContentsOfURL已棄用此鏈接警告....掌握的代碼

回答

2

嘗試代替: NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];

NSData *locationData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]; 
NSString *locationString = [[NSString alloc] initWithData:locationData encoding:NSASCIIStringEncoding]; 

關於這樣的好事是使y您可以使用NSURLConnection來獲取數據(異步)。

2

如果是什麼讓你感到困惑是這個「StringWithContentsOfURL已經過時」。蘋果的文檔中快速搜索表明:

stringWithContentsOfURL: 返回從給定的URL指定的文件中讀取數據創建一個字符串。 (棄用在IOS 2.0使用stringWithContentsOfURL:編碼:錯誤:stringWithContentsOfURL:usedEncoding:錯誤:代替)

一個例子是:

stringWithContentsOfURL:yourURL encoding:NSUTF8StringEncoding error:NULL