简体   繁体   中英

getting warning with this link of code

 -(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 is deprecated....

If what made you confused is this "StringWithContentsOfURL is deprecated". A quick search in Apple's documentation shows that:

stringWithContentsOfURL : Returns a string created by reading data from the file named by a given URL. ( Deprecated in iOS 2.0 . Use stringWithContentsOfURL:encoding:error: or stringWithContentsOfURL:usedEncoding:error: instead.)

An example would be:

stringWithContentsOfURL:yourURL encoding:NSUTF8StringEncoding error:NULL

try instead of: NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];

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

the good thing about this way is that you can use NSURLConnection instead to get the data (asynchronously).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM