AFNetworking: using IP address, application cannot reach server

When I try to use the ip address instead of the host name to let the iOS application in the simulator talk to the local server, the application freezes on the loading screen, waiting for a response from the server that never appears.

I am new to Objective-C, but the code uses the AFNetowrking structure that processes the request, and the SCNetworkReachabilityFlags property is set differently for the ip address of the AFHTTPClient object.

During debugging, the following AFHTTPClient object objects were captured for the ip address and host name.

<SCNetworkReachability 0xd26d8a0 [0x53b9ec8]> {address = 127.0.0.1, flags = 0x00010002, if_index = 1} <SCNetworkReachability 0xcf283f0 [0x53b9ec8]> {name = localhost (server query active), flags = 0x80000000, if_index = 0} 

In the startMonitoringNetworkReachability method in the AFHTTPClient.m class, there is the following if statement that runs for the ip address, but I do not know if this is a problem:

 /* Network reachability monitoring does not establish a baseline for IP addresses as it does for hostnames, so if the base URL host is an IP address, the initial reachability callback is manually triggered. */ if (AFURLHostIsIPAddress(self.baseURL)) { SCNetworkReachabilityFlags flags; SCNetworkReachabilityGetFlags(self.networkReachability, &flags); dispatch_async(dispatch_get_main_queue(), ^{ AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags); callback(status); }); } 

My question is what causes the problem and how can I use the IP address as baseURL to connect to the server?

+6
source share
1 answer

This is a common problem. This was stated on the AFNetworking github page: https://github.com/AFNetworking/AFNetworking/issues/2191

Domains and IP addresses are checked differently.

You can check the IP address using the manager created using managerForAddress: http://cocoadocs.org/docsets/AFNetworking/2.5.0/Classes/AFNetworkReachabilityManager.html#//api/name/managerForAddress :

Hope this helps! Let me know if you have more questions.

EDIT

I think I misunderstood the question.

I sent requests using AFHTTPRequestOperationManager and AFURLSessionManager before, and they both work using IP addresses in the url.

An example of how to use them can be found here: http://cocoadocs.org/docsets/AFNetworking/2.5.0/#usage

+4
source

Source: https://habr.com/ru/post/957872/


All Articles