- (void)getIp
{
? ? NSLog(@"開(kāi)始獲取ip");
? ? NSError*error;
? ? NSString*ip =@"";
? ? // 方法1? ? 不穩(wěn)定驰徊,有時(shí)太慢融求,有時(shí)得不到結(jié)果
? ? NSURL *url = [NSURL URLWithString:@"https://ifconfig.me/ip"];
? ? ip = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
? ? // 方法2
? ? NSURL *url = [NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo.php?ip=myip"];
? ? NSString *response = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
? ? NSDictionary*dicResponse = [responsemj_JSONObject];
? ? if(dicResponse && dicResponse[@"data"]) {
? ? ? ? NSDictionary*dicData = dicResponse[@"data"];
? ? ? ? if(dicData && dicData[@"ip"]) {
? ? ? ? ? ? ip = dicData[@"ip"];
? ? ? ? }
? ? }
? ? // 方法3?
? ? NSURL *url = [NSURL URLWithString:@"http://pv.sohu.com/cityjson?ie=utf-8"];
? ? NSString*response = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
? ? //判斷返回字符串是否為所需數(shù)據(jù)
? ? NSString *prefix = @"var returnCitySN = ";
? ? if([responsehasPrefix:prefix]) {
? ? ? ? // 刪除多余內(nèi)容
? ? ? ? response = [responsesubstringFromIndex:prefix.length];
? ? ? ? response = [responsesubstringToIndex:response.length-1];
? ? ? ? NSDictionary*dicResponse = [responsemj_JSONObject];
? ? ? ? if(dicResponse && dicResponse[@"cip"]) {
? ? ? ? ? ? ip = dicResponse[@"cip"];
? ? ? ? }
? ? }
? ? NSLog(@"當(dāng)前網(wǎng)絡(luò)ip %@", ip);
}