Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS/xcode: didReceiveData empty when run on device but fine on simulator?
    primarykey
    data
    text
    <p>I am developing an iphone app which is using an API.<br/></p> <p>On the '<strong>simulator</strong>' it works fine. On the '<strong>device</strong>' the api calls all run but response delegates all return EMPTY. <br/></p> <p><strong>LOG:</strong><br/> <strong>1)</strong> <code>didReceiveData</code> returns (empty <code>NSData</code> i.e. &lt;20>) below for <strong>device</strong>:</p> <pre><code>append data &lt;20&gt; </code></pre> <p>But this for <strong>simulator</strong>:</p> <pre><code>append data &lt;dfgdf89fgd.....&gt; </code></pre> <p><br/> <strong>2)</strong> The <code>didReceiveResponse</code> I have below returns below for the <strong>device</strong> (set NSLog to monitor NSURLConnection object). Confused why this is not complete:</p> <pre><code>1 connection &lt;NSURLConnection: 0x147a30&gt; </code></pre> <p>But this for the <strong>simulator</strong>:</p> <pre><code>1 connection &lt;NSURLConnection: 0x4b76ea0, http://api.site.com/search?id=111&gt; </code></pre> <p><br/> <strong>3)</strong> The <code>connectionDidFinishLoading</code> is below for <strong>simulator</strong></p> <pre><code>DONE. Received Bytes: 1400 </code></pre> <p>But below for <strong>device</strong>:</p> <pre><code>DONE. Received Bytes: 1 </code></pre> <p><br/></p> <p><strong>My code:</strong><br/> I have the below code inside an API file:</p> <pre><code>- (void)fetch: (NSString *)id{ NSMutableData *receivedData = [[NSMutableData alloc]init];//initialised self.receivedData = receivedData;//set to available globally NSString *initialUrl = [NSString stringWithFormat: @"http://api.site.com/search?id=%@",id]; NSURL *url = [NSURL URLWithString:initialUrl]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; [request setHTTPMethod:@"GET"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"Token token=\"apitoken\"" forHTTPHeaderField:@"Authorization"]; [NSURLConnection connectionWithRequest:[request autorelease] delegate:self]; } -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"1 connection %@",connection); NSLog(@"didReceiveResponse"); [self.receivedData setLength: 0]; } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSLog(@"2 connection %@",connection); NSLog(@"append data %@",data); [self.receivedData appendData:data]; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { //TODO error handling for connection NSLog(@"Cannot Connect"); if ([_delegate respondsToSelector:@selector(apiFailed:)]) { [_delegate apiFailed:[error localizedDescription]]; } } -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"DONE. Received Bytes: %d", [self.receivedData length]); NSLog(@"FULL DATA: %@",self.receivedData); } </code></pre> <p>The <strong>simulator</strong> and <strong>device</strong> (my iphone with provisioned profile on and working) are both running off the same Wifi (my home router).<br/></p> <p><br/> <strong>The API:</strong><br/> The API returns a small amount of JSON and I have tested with a different set of JSON and both device and simulator return full JSON no problem, I only cant get it to work with THIS API. <br/> I even have the same JSON (the api returns) inside a different location and I can do the EXACT same request (with headers and authentication) and it works no problem for <strong>device</strong>. Strangely though the connection object still doesnt look right for the fake api server. On <strong>device</strong> <code>didReceiveResponse</code> has <code>1 connection &lt;NSURLConnection: 0x1af600&gt;</code> still and not with the connection url (but when run on <strong>simulator</strong> it has <code>1 connection &lt;NSURLConnection: 0x8193fd0, http://test-server.com:81/api-data.json&gt;</code>). This suggest that its a normal difference between <strong>device</strong> and <strong>simulator</strong> <code>NSURLCONNECTION</code> objects.<br/> I feel it might be the authentication getting in the way but the API is completely open (assuming you have token) and works no problem with the simulator plus <code>didFailWithError</code> is not run..<br/></p> <p>I have declared the delegate methods inside the header file. <br/> No idea why the connection object as well as the data object would both be having problems.</p> <p><br/> <strong>Alternative:</strong><br/> I tried the below inside the fetch method instead of what I have already but it also works on <strong>Simulator</strong> but same thing happens on <strong>Device</strong>.</p> <pre><code>//NSURLCONN and NSDATA in response still return empty??? NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.site.com/search?id=111"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [request setHTTPMethod:@"GET"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"Token token=\"mytoken\"" forHTTPHeaderField:@"Authorization"]; NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; </code></pre> <p>I would appreciate ANY ideas on this.</p> <p>thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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