好的,你如何做到這一點與知識產權。
所以,geoplugin.net有它的驚人的JSON API,它默認的IP到當前連接如,所以你必須做的是對這個地址的請求:
http://www.geoplugin.net/json.gp
Astunishing!對我來說,它返回這個數據:
geoPlugin({
"geoplugin_request":"201.6.226.233",
"geoplugin_status":200,
"geoplugin_city":null,
"geoplugin_region":"São Paulo",
"geoplugin_areaCode":0,
"geoplugin_dmaCode":0,
"geoplugin_countryCode":"BR",
"geoplugin_countryName":"Brazil",
"geoplugin_continentCode":"SA",
"geoplugin_latitude":-23.473301,
"geoplugin_longitude":-46.665798,
"geoplugin_regionCode":27,
"geoplugin_regionName":"São Paulo",
"geoplugin_currencyCode":"BRL",
"geoplugin_currencySymbol":"R$",
"geoplugin_currencyConverter":2.0198
})
那麼,現在你所要做的只是解析這個「JSON」。它實際上並不是一個JSON,因爲它有這個geoplugin({data})
包裝。所以你可以懶惰地執行一些過濾,也許刪除NSSTring的那些部分。
我看你很匆忙,所以我花了我的空餘時間爲你寫一些代碼。這是非常不規範的,因爲我不知道,如果你使用任何REST框架會有所幫助,但這裏有雲:
NSString *url = [NSString stringWithFormat:@"http://www.geoplugin.net/json.gp"];
NSString *locationData = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]
encoding:NSUTF8StringEncoding]];
locationData = [locationData stringByReplacingOccurrencesOfString:@"geoPlugin(" withString:@""];
locationData = [locationData stringByReplacingOccurrencesOfString:@")" withString:@""];
//user some json parser here.
if([[[locationData JSONValue] valueForkey:@"geoplugin_countryCode"] isEqualToString:@"US"]){
//proceed.
}
現在我可以檢測「EN_US」與區域...這在美國會是這樣嗎? –
區域設置是用戶可配置的。例如,居住在美國的講中文的人可能會將其手機的語言和語言環境設置爲中國。您不應該使用用戶的區域設置來確定它們的位置;它應該用於確定如何本地化他們的內容。 –