1. 程式人生 > >用淘寶的介面獲取手機ip

用淘寶的介面獲取手機ip

-(void)urlRequestOperation{
NSString *URLTmp = @"http://ip.taobao.com/service/getIpInfo.php?ip=myip";
NSString *URLTmp1 = [URLTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //轉碼成UTF-8  否則可能會出現錯誤
//    [URLTmp stringByAddingPercentEncodingWithAllowedCharacters:(nonnull NSCharacterSet *)]
URLTmp = URLTmp1; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: URLTmp]]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) { NSLog
(@"Success: %@", operation.responseString); NSString *requestTmp = [NSString stringWithString:operation.responseString]; NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]]; //系統自帶JSON解析 NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves
error:nil]; NSLog(@"aaaaaaa=====%@",resultDic); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failure: %@", error); }]; [operation start]; }