Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to get responses from local server when connecting via iOS device
    text
    copied!<p>Im using AFNetworking to retrieve items from a basic rails server in a simple iOS project.</p> <p>When I make the request in the simulator, everything works smoothly. However, when I make the same request while running the project on my device, I'm getting a frustrating error.</p> <p>I understand that I cannot connect to localhost directly from my device and therefore need to use my ip address, which I am doing. This is the weird part: when I make a request to the server, I can see in my terminal that the server was hit and is returning a 200 response. However, the request is failing (on the client end) with the error message: 'The request timed out.'</p> <p>Information and code:</p> <p>My rails server is extremely basic. I've essentially generated a new project, set up a simple model called 'items' that has a single column -- a string -- for the item's content. I have the routes set up to only respond to JSON requests and the index method on the items_controller just returns the results of Item.all in json form.</p> <p>Here are my routes:</p> <pre><code>TestingServer::Application.routes.draw do scope :format =&gt; true, :constraints =&gt; { :format =&gt; 'json' } do resources :items, :only =&gt; [:index] end end </code></pre> <p>and here is my items_controller.rb</p> <pre><code>class ItemsController &lt; ApplicationController def index @items = Item.all render :status =&gt; 200, :json =&gt; @items end end </code></pre> <p>As for the iOS project, here is my AFHTTPClient subclass header:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "AFHTTPClient.h" @interface PNAPIClient : AFHTTPClient + (PNAPIClient *)sharedClient; @end </code></pre> <p>and here is its implementation:</p> <pre><code>#import "PNAPIClient.h" #import "AFJSONRequestOperation.h" static NSString * const kPNAPIClientBaseURLString = @"http://&lt;ip address&gt;:9292/"; @implementation PNAPIClient + (PNAPIClient *)sharedClient { static PNAPIClient *_sharedClient = nil; static dispatch_once_t onceToken; dispatch_once(&amp;onceToken, ^{ _sharedClient = [[PNAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kPNAPIClientBaseURLString]]; }); return _sharedClient; } - (id)initWithBaseURL:(NSURL *)url { self = [super initWithBaseURL:url]; if (!self) { return nil; } [self registerHTTPOperationClass:[AFJSONRequestOperation class]]; [self setDefaultHeader:@"Accept" value:@"application/json"]; return self; } @end </code></pre> <p>And finally, here is the request that is failing:</p> <pre><code>- (IBAction)testRequest:(id)sender { [[PNAPIClient sharedClient] getPath:@"/items.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) { NSLog(@"success: %@", JSON); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"failure %@", error.localizedDescription); }]; } </code></pre> <p>One last comment: I tried using a different URL (one that is online from another example) and it worked fine. This makes me suspect that their is either an issue with my Rails server, or that there is an issue with my device connecting to it locally. Like I said, I am able to do everything from the simulator just fine, and can see that I am hitting the server from my device.</p> <p><strong>Update 1</strong></p> <p>It appears that the failure block on the -getPath:parameters:success:failure: is being called no matter what the server response is. That is, if the server throws a 422 response with a json representation of the error, I am able to get the error message on my device. However, if the server returns a 200 response with some other json object, the failure block is still thrown... with no errors of course.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload