簡體   English   中英

通過此代碼鏈接獲得警告

[英]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。...

如果讓您感到困惑的是,此“不推薦使用StringWithContentsOfURL”。 在Apple文檔中的快速搜索顯示:

stringWithContentsOfURL :返回一個字符串,該字符串是通過從給定URL命名的文件中讀取數據而創建的。 在iOS 2.0中已棄用改為使用stringWithContentsOfURL:encoding:error:stringWithContentsOfURL:usedEncoding:error:。

一個例子是:

stringWithContentsOfURL:yourURL encoding:NSUTF8StringEncoding error:NULL

嘗試而不是: NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];

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

這種方式的好處是,您可以改用NSURLConnection來獲取數據(異步)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM