Facebook SLRequest messing with NSURLConnection

544 Views Asked by At

I am trying to get facebook data using SLRequest iOS API, which seems to be working fine -

NSDictionary *parameters = @{};
NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/home"];

SLRequest *feedRequest = [SLRequest 
    requestForServiceType:SLServiceTypeFacebook
    requestMethod:SLRequestMethodGET
    URL:feedURL 
    parameters:parameters];

feedRequest.account = facebookAccount;

[feedRequest performRequestWithHandler:^(NSData *responseData, 
       NSHTTPURLResponse *urlResponse, NSError *error)
{
    // something
}];

However, following this I make an HTTP POST request to one of my servers,

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSData *requestData = [NSData dataWithBytes:[jsonData bytes] length:[jsonData length]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: requestData];
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

which POSTS the data fine (verified from the server logs), but I do not get any HTTP response for it in

connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response

This used to be working fine without the SLRequest POST, and I am able to comment that piece out and it starts working again.

What am I missing here ?

2

There are 2 best solutions below

1
On

How about you create an strong reference to your NSURLConnection, my guess that iOS is releasing that object, so that is why your delegate never get's called.

So, instead of:

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

Use:

self.connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];

Where:

@property(nonatomic, strong)NSURLConnection * connection;

Let me know how it goes, cheers.

Kind regards

0
On

The fix that work for me is not to use the SLRequest block loading and to use a NSURLConnection with the appropriate NSURLConnectionDelegate methods in the first instants. The second time you use this delegate the loading should be fine, try keeping all you loading in the same Utility Class, so they share the same delegate. For me it was issues with the twitters v1.1 API.

So in your code try removing the block and then you need to prepare the SLRequest:

urlData=[[NSData alloc] init];
// change that SLRequest to a NSURLRequest
NSURLRequest *request = [feedRequest preparedURLRequest];
dispatch_async(dispatch_get_main_queue(), ^{
    [NSURLConnection connectionWithRequest:request delegate:self];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
});

Then use the NSURLConnectionDelegate Methods, with capturing the loaded NSData:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    connection=nil;
    NSError *jsonParsingError = nil;
    NSMutableDictionary *deserializedData  = [NSJSONSerialization JSONObjectWithData:urlData options:NSJSONReadingAllowFragments error:&jsonParsingError];
    if (deserializedData) {
         // handle the loaded dictionary as you please
    } 
}

This fix was inspired by Keith Harrison post at : http://useyourloaf.com/blog/2013/06/24/migrating-to-the-new-twitter-search-api.html

So credit goes to him too.