Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking: Using IP address, application cannot reach server

When I try to use an ip address instead of a hostname to tell an ios application in a simulator to talk to a local server the app hangs on the loading screen, waiting for a response from the server that never comes.

I'm new with Objective C but the code is using the AFNetowrking framework that's handling the request and the SCNetworkReachabilityFlags property is set differently for the ip address on the AFHTTPClient object.

The following properties of the AFHTTPClient object for ip address and hostname were captured during a debug.

<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 within the AFHTTPClient.m class there is the following if statement that is triggered for the ip address but I don't know if it is the 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's causing the problem and how do I use an IP address as baseURL to connect to a server?

like image 411
wanyo Avatar asked Nov 12 '13 00:11

wanyo


1 Answers

This is a common issue. There was a discussion about this on the AFNetworking github page: https://github.com/AFNetworking/AFNetworking/issues/2191

Domains and ip addresses are checked differently.

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

I hope this helps! Let me know if you have any more questions.

EDIT

I think I misunderstood the question.

I've sent requests using the 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

like image 152
benbarth Avatar answered Oct 06 '22 19:10

benbarth