通过此代码链接获得警告

时间:2010-12-21 12:04:51

标签: iphone

 -(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 个答案:

答案 0 :(得分:2)

如果让你困惑的是“StringWithContentsOfURL已被弃用”。在Apple的文档中快速搜索显示:

<强> stringWithContentsOfURL : 返回通过从给定URL指定的文件中读取数据而创建的字符串。 (在iOS 2.0中不推荐使用。使用 stringWithContentsOfURL:encoding:error: stringWithContentsOfURL:usedEncoding:error:。)

一个例子是:

stringWithContentsOfURL:yourURL encoding:NSUTF8StringEncoding error:NULL

答案 1 :(得分:2)

尝试而不是: NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];

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

这种方式的好处是你可以使用NSURLConnection来获取数据(异步)。