用淘寶的介面獲取手機ip
阿新 • • 發佈:2019-01-09
-(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];
}