Lov*_*and 10

谢谢你的评论!

我从主题如何在iOS中编程方式获取用户外部IP和地理位置并编辑代码来找到答案:

NSMutableURLRequest *requestHTTP = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://ip-api.com/json"]
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:10];

[requestHTTP setHTTPMethod:@"GET"];
[requestHTTP setValue: @"text/json" forHTTPHeaderField:@"Accept"];

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:requestHTTP];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"IP response: %@ ",responseObject);
    NSString *myIP = [responseObject valueForKey:@"query"];
    NSLog(@"IP: %@", myIP);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", [error localizedDescription]);

}];
[op start];
Run Code Online (Sandbox Code Playgroud)

谢谢!

  • 因为您使用ip-api.com,所以需要注意他们的政策.http://ip-api.com/docs/ (2认同)