1

I am using the Yelp Search API to basically just get a list of businesses for a search query.

It is pretty much a NSURLConnection is OAuth, but here is the code to initialize the request:

 NSURL *URL = [NSURL URLWithString:appDelegate.yelpAdvancedURLString];


OAConsumer *consumer = [[OAConsumer alloc] initWithKey:@"this-is-my-key" secret:@"this-is-my-secret"];
OAToken *token = [[OAToken alloc] initWithKey:@"this-is-my-key" secret:@"this-is-my-secret"];

id<OASignatureProviding, NSObject> provider = [[OAHMAC_SHA1SignatureProvider alloc] init];
NSString *realm = nil;

OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:URL

                                                               consumer:consumer
                                                                  token:token
                                                                  realm:realm
                                                      signatureProvider:provider];

[request prepare];

responseData = [[NSMutableData alloc] init];


yelpConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

Then here:

  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error: %@, %@", [error localizedDescription], [error localizedFailureReason]);

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Oops." message: @"Something screwed up. Please search again." delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

                [alert show];

     [MBProgressHUD hideHUDForView:self.view animated:YES];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{       
    if (connection == self.yelpConnection) {
        [self setYelpString];

    }
}

When I run this on iPhone, everything is working fine. However, when I run on iPad, the connection gets timed out. The following is from this line

 NSLog(@"Error: %@, %@", [error localizedDescription], [error localizedFailureReason]);

 Error: The request timed out., (null)

Also if I use a synchronous request, it seems to work using this:

 NSData* result = [NSURLConnection sendSynchronousRequest:request  returningResponse:&response error:&error];

NSDictionary* JSON = [NSJSONSerialization
                      JSONObjectWithData:result
                      options:kNilOptions
                      error:&error];

However, I want to avoid using synchronous as it freezes the app.

Is this Yelp API specific? Or am I just doing something wrong? Thanks in advance, I would appreciate any help.

If it helps, it times out approximately 10 seconds after sending the request.

Spenciefy
  • 892
  • 11
  • 33

2 Answers2

0

create this type of NSMutableURLRequest :

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:240.0];

Ajay Malviya
  • 131
  • 4
  • I can't use initWithURL since I have to use OAuth, so I have to use initWithRequest – Spenciefy Aug 30 '13 at 05:20
  • The request is still timing out. – Spenciefy Aug 30 '13 at 05:31
  • Can you retrieve the timeout after statement `[request prepare];`? Maybe this method is overriding your timeout setting. Try to set it after the last statement that may modify the request. – CouchDeveloper Aug 30 '13 at 07:44
  • 1
    Looking into the sources of OAMutableURLRequest reveals this, which explains your issue: in the `init` method of the subclass of NSMutableURLRequest: `if (self = [super initWithURL:aUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0])` – CouchDeveloper Aug 30 '13 at 07:48
  • @CouchDeveloper Are you saying to do `yelpConnection = [[NSURLConnection alloc] initWithRequest:OArequest delegate:self]; [OArequest prepare];` ? Because it doesn't work. – Spenciefy Sep 01 '13 at 18:25
  • My comment is too long, so I added an answer. – CouchDeveloper Sep 02 '13 at 06:19
0

I think the best approach is to change the init method in http://oauth.googlecode.com from

- (id)initWithURL:(NSURL *)aUrl
         consumer:(OAConsumer *)aConsumer
            token:(OAToken *)aToken
            realm:(NSString *)aRealm
signatureProvider:(id<OASignatureProviding, NSObject>)aProvider
{
     if (self = [super initWithURL:aUrl
                       cachePolicy:NSURLRequestReloadIgnoringCacheData
               timeoutInterval:10.0])
{    
       ...
    }

}

to

- (id)initWithURL:(NSURL *)aUrl 
      cachePolicy:(NSURLRequestCachePolicy)cachePolicy 
  timeoutInterval:(NSTimeInterval)timeoutInterval
         consumer:(OAConsumer *)aConsumer
            token:(OAToken *)aToken
            realm:(NSString *)aRealm
 signatureProvider:(id<OASignatureProviding, NSObject>)aProvider
 {
     if (self = [super initWithURL:aUrl
                       cachePolicy:cachePolicy
               timeoutInterval:timeoutInterval])
{    
       ...
    }

and then check again, whether the timeout value which you specify will be honored by the connection.

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67