iPhone & Google Places API - iphone

I am using Google place API in my application. Well, its working fine and giving response for some places. But for some places, its giving "ZERO RESULTS".
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://maps.googleapis./maps/api/place/search/json?location=%f,%f&radius=5000&types=&name=%#&sensor=false&key=fgewffefewweweewfe",mapView.centerCoordinate.latitude,mapView.centerCoordinate.longitude,searchPlace.text]];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSURLConnection *connect= [[NSURLConnection alloc]initWithRequest:request delegate:self];
Can anyone give me an idea where I am getting wrong?

If the location name is not valid it will give you zero results & in some cases because of extra spaces in the string you will get URL as null. So update your code as I mentioned below and check it out.
NSString *googleApi = [NSString stringWithFormat:#"https://maps.googleapis./maps/api/place/search/json?location=%f,%f&radius=5000&types=&name=%#&sensor=false&key=fgewffefewweweewfe",mapView.centerCoordinate.latitude,mapView.centerCoordinate.longitude,searchPlace.text];
NSString *finalURL = [googleApi stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:finalURL];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSURLConnection *connect= [[NSURLConnection alloc]initWithRequest:request delegate:self];
It may work for you.

Related

error.USER_REQUIRED generated when attempting to submit Reddit link in objective-c

I am having a small Reddit API problem here. I am trying to follow the reddit api, as outlined here:
https://github.com/reddit/reddit/wiki/API
Logging in using a simple NSMutableURLRequest is not a problem:
NSString *user = [[NSString alloc]initWithString:[userFld text]];
NSString *passwd = [[NSString alloc]initWithString:[passwordFld text]];
NSString *urlString = [NSString stringWithFormat:#"http://www.reddit.com/api/login/username/?user=%#&passwd=%#",
[user stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[passwd stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Which gives me a result from which I extract the user mod-hash:
4029916%2C2010-04-30T22%3A51%3A52%2C1243925043100000000000000000000000000000
Next I am trying to post, using:
NSString *redditUrlString = [NSString stringWithFormat:
#"http://www.reddit.com/api/submit/?uh=%#&kind=link&url=%#&sr=%#&title=%#&r=%#&api_type=json",
[appDelegate globalUhString],
#"www.google.com",
#"funny",
#"Google.com",
#"funny"];
NSURL *url = [NSURL URLWithString:redditUrlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
However, when I extract the JSON response form the data received from this connection, I always receive the error below no matter what I use as the mod-hash:
[".error.USER_REQUIRED"]
Can someone explain what I have done incorrectly/how I can fix it?
It looks like you've not passing in the reddit_session cookie along with the uh parameter. The api documentation seems to explain the reason for receiving the error. According to this answer, it looks like you may need to issue the code:
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
in the applicationDidBecomeActive handler, as it may be rejecting the cookies due to the CookieAcceptPolicy being shared amongst all applications.

NSMutableURLRequest loses session information

I have an iphone app that I'm trying to get to talk to a rails back end. I'm using NSMutableURLRequest to pull data back and forth. All the calls work fine on GET requests but when I need to post data, my rails app can't seem to find the session. I've posted the code below for both a get and a POST request.
This is the POST request:
//Set up the URL
NSString *url_string = [NSString stringWithFormat:#"https://testserver.example.com/players.xml"];
NSURL *url = [NSURL URLWithString:url_string];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30];
//To create an object in rails
//We have to use a post request
////This is the format when using xml
NSString *requestString = [[NSString alloc] initWithFormat:#"<player><team_game_id>%#</team_game_id><person_id>%#</person_id></player>", game, person];
NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString length]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:requestData];
NSString *postLength = [NSString stringWithFormat:#"%d", [requestData length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
//For some reason rails will not take application/xml
[request setValue:#"application/xml" forHTTPHeaderField:#"content-type"];
The get request is:
NSString *url_string = [NSString stringWithFormat:#"https://testserver.example.com/people/find_by_passport?passport=%i", passport];
passportString = [[NSMutableString alloc] initWithFormat:#"%i", passport];
NSLog(#"The passport string is %#", passportString);
NSLog(url_string, nil);
NSURL *url = [NSURL URLWithString:url_string];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30];
I am at my wits end here trying to find out whats going on so any help would be GREATLY appreciated.
Faced the same issue. Parse the response headers for the Set-Cookie header, extract the cookies by hand, pass them back as Cookie: header on subsequent requests.
If this is just about sessions in their simplest, you don't have to worry about persistence, HTTPS, domains/paths and such.
EDIT: I found class NSHTTPCookieStorage. See if it can be of help...

Losing a / when converting from NSURL to NSURLRequest

I'm doing an HTTP Post in my iphone app and one of the parameters I send to the server is a URL. The problem is that when I convert from an NSURL to an NSURLRequest, the string http://www.slashdot.org becomes http:/www.slashdot.org (one of the forward slashes is missing)
is there a way around this?
here is the code I'm using:
NSString *host = #"example.host.com";
NSString *urlString = [NSString stringWithFormat:#"/SetLeaderUrl.json?leader_email=%#&url=%#",localEmail,urlToPublish];
NSURL *url = [[NSURL alloc] initWithScheme:#"http" host:host path:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *jsonString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
I've used NSLog to see where it loses the '/' and it's on the fourth line:
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
thanks for taking the time to read!
You're not percent-escaping the query values before substituting them in to the string. I just did a little test, and found that if I set urlToPublish to "http://example.com", then NSURL would transform it into "http:/example.com".
This is because the query value contains special characters, which means you need to add percent escapes. At the very least you can use the mediocre -[NSString stringByAddingPercentEscapesUsingEncoding:] with the NSASCIIStringEncoding. Far better would be to use a different (and more complete) escaping mechanism, such as the one I suggest in this post.
In this case, stringByAddingPercentEscapesUsingEncoding: does not work, because it's a pretty lousy method. It works on an inclusive model, which means you have to tell it which characters you want percent encoded. (Under the hood, it's just calling CFURLCreateStringByAddingPercentEscapes()) This function basically asks you for a string that represents every character it's allowed to percent-encode (as I understand the function). What you really want is an exclusive model: escape everything except [this small set of characters]. The function I linked to above does that, and you'd use it like this:
NSString *urlToPublish = [#"http://stackoverflow.com" URLEscapedString_ch];
NSString *host = #"example.host.com";
NSString *urlString = [NSString stringWithFormat:#"/SetLeaderUrl.json?leader_email=%#&url=%#",localEmail,urlToPublish];
NSURL *url = [[NSURL alloc] initWithScheme:#"http" host:host path:urlString];
And then it will build your URL properly.
Here's another way you could do this (and do it correctly). Go to my github page and download "DDURLBuilder.h" and "DDURLBuilder.m", and then build your URL like this:
NSString *localEmail = #"foo#example.com";
NSString *urlToPublish = #"http://stackoverflow.com"
DDURLBuilder *b = [DDURLBuilder URLBuilderWithURL:nil];
[b setScheme:#"http"];
[b setHost:#"example.host.com"];
[b setPath:#"SetLeaderUrl.json"];
[b addQueryValue:localEmail forKey:#"leader_email"];
[b addQueryValue:urlToPublish forKey:#"url"];
NSURL *url = [b URL];
Here is some code apple use to get a NSURLRequest,
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
#Dave DeLong: I notice in the Apple "URL Loading System Program Guide" the example creating a connection and request does not use any escaping. The Url it uses is from a NSURL URLWithString:
I fixed it, this is what I had to do:
NSString *urlString = [NSString stringWithFormat:#"/SetLeaderUrl.json?leader_email=%#&url=%#",localEmail,urlToPublish];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
urlString = [NSString stringWithFormat:#"%#%#%#",scheme,host,urlString];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

Send http request with Chinese words

NSString *urlAddress = #"http://www.test.com/test.jsp?Query=哈囉";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
NSURLResponse* myURLResponse;
NSError* myError;
NSData* data = [NSURLConnection sendSynchronousRequest: requestObj returningResponse:&myURLResponse error:& myErr];
I want to use http get method to send request, but failed.
English alphabets work well.
I.e., I want to convert "哈囉" to "%E5%93%88%E5%9B%89" so that the response will successfully return. It seems not to be converted by itself automatically.
Anyone could help me solve this problem?
Thanks a lot!
urlAddress = [urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
(docs)

shoutcast pls forbidden for iPhone programmatically?

I have been trying to access the pls file data from shoutcast for some testing but the response seems to be forbidden and I am getting 403 as response.
here is the code
NSURL *myurl = [NSURL URLWithString:#"http://yp.shoutcast.com/sbin/tunein-station.pls?id=9944"] ;
//Accept:*/*
NSMutableURLRequest *myrequest = [[NSMutableURLRequest alloc]initWithURL:myurl];
[myrequest setValue:#"*/*" forHTTPHeaderField:#"Accept"];
//NSURL *myurl = [NSURL URLWithString:#"http://www.google.com"];
NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:myrequest delegate:self];
On the response, it was showing a 403 and no data is received. I tried to check the content-type and it was showing audio/x-scpls
In case of disallowing NSURLRequests, changing the submitted user agent should avoid the 403 response:
[myRequest setValue:userAgent forHTTPHeaderField:#"useragent"];