How to get the public IP address of the device

前端 未结 9 597
陌清茗
陌清茗 2020-12-29 10:32

I found this sample code to get all local IP addresses, but I don\'t find an easy solution to get the public IP.

A legacy class from Apple allowed to do that ... bu

9条回答
  •  灰色年华
    2020-12-29 10:57

    I use ipify and with no complaints.

    NSURL *url = [NSURL URLWithString:@"https://api.ipify.org/"];
    NSString *ipAddress = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"My public IP address is: %@", ipAddress);
    

提交回复
热议问题