Iphone 使用此代码链接获取警告
StringWithContentsOfURL已弃用 如果让您感到困惑的是“StringWithContentsOfURL不推荐使用”。在苹果的文档中快速搜索显示: stringWithContentsOfURL: 返回通过从给定URL命名的文件中读取数据而创建的字符串。(在iOS 2.0中不推荐使用。请改用stringWithContentsOfURL:encoding:error:或stringWithContentsOfURL:usedEncoding:error:) 例如:Iphone 使用此代码链接获取警告,iphone,Iphone,StringWithContentsOfURL已弃用 如果让您感到困惑的是“StringWithContentsOfURL不推荐使用”。在苹果的文档中快速搜索显示: stringWithContentsOfURL: 返回通过从给定URL命名的文件中读取数据而创建的字符串。(在iOS 2.0中不推荐使用。请改用stringWithContentsOfURL:encoding:error:或stringWithContentsOfURL:usedEncoding:error:) 例如: -(CLL
-(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已弃用”。在苹果的文档中快速搜索显示: stringWithContentsOfURL: 返回通过从给定URL命名的文件中读取数据而创建的字符串。(在iOS 2.0中不推荐使用。请改用stringWithContentsOfURL:encoding:error:或stringWithContentsOfURL:usedEncoding:error:) 例如:
-(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;
}
尝试而不是:
NSString*locationString=[NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]]代码>
stringWithContentsOfURL:yourURL encoding:NSUTF8StringEncoding error:NULL
这种方式的好处是,您可以使用NSURLConnection(异步)来获取数据。尝试而不是:
NSString*locationString=[NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]]代码>
stringWithContentsOfURL:yourURL encoding:NSUTF8StringEncoding error:NULL
这种方式的好处是可以使用NSURLConnection(异步)获取数据