how can i query google places for country specific cuisine? - iphone

i am trying to accomplish country specific Cuisine places, means like mexican food, spanish food, Thai food, indian food, via google places API on an iPhone app.
How can i do that?? As i see supported types, there is only food, restaurant, etc in it. besides whenever i use this query..
NSString *url = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%#&type=restaurant&query=%#&sensor=true&key=%#", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:#"%i", currenDist], googleType, kGOOGLE_API_KEY];
i am getting few results only, whenever i try to add more types like food|restaurant|establishment like below query my app crashes.
NSString *url = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%#&type=food|restaurant|establishment&query=%#&sensor=true&key=%#", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:#"%i", currenDist], googleType, kGOOGLE_API_KEY];
As i told my app crashes with this error may be i am doing something wrong with url.
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
* First throw call stack: (0x13be022 0x200fcd6 0x1366a48 0x13669b9 0xbadfad 0x2dd2 0x13bfe42 0xaa09df 0x139294f 0x12f5b43 0x12f5424
0x12f4d84 0x12f4c9b 0x15d47d8 0x15d488a 0x17a626 0x236d 0x22d5)
terminate called throwing an exception(lldb)
Please can someone guide me with this?? i hope i cleared all my points.
Thanks & regards,
Malhaar

It appears you are trying to use the Places API Textsearch as you are using the query parameter. If this is correct make sure you are hitting the correct backend:
maps.googleapis.com/maps/api/place/textsearch/json
Also make sure you are using the correct parameter to specify types:
types=food|restaurant|establishment
I would recommend reading the documentation thoroughly and and performing a few test html requests through your browser to make sure you have the right request before plugging it into your application.

Although this is almost 7 months old, and I'm sure you've either solved it or given up on it by now, I have just had the same issue and it took some resolving. I thought I'd put the answer here to help anyone else with a similar error.
The problem I found was to do with the URL encoding not liking the | character.
The way around it I found was to:
// Create an NSString with the URL which includes the '|' characters.
NSString * urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=51.189781,-114.488907&radius=6918&types=food|bar&keyword=&sensor=true&key=YOUR KEY HERE";
// Encode the URL
NSString* encodedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
// Covert to a NSURL
NSURL *googleRequestURL=[NSURL URLWithString:encodedURLString];
And then use the googleRequestURL as the URL.
Hopefully that will solve it for you!

Related

Strange BAD_EXC_ACCESS when declaring a string

Okay, all I am doing is setting an NSString to a value with this code:
NSString *stringURL = [NSString stringWithFormat:#"http://api.themoviedb.org/3/movie/%#/trailers?api_key=1523229ded5824dab8bb7840782db266",searchID];
This is a string that I then turning into a URL for querying the TMDB database. This line of code gives me a BAD_EXC_ACCESS and it is blowing my mind because using this sort of NSString construction is something I have done thousands of times without a problem.
The one other thing to note is that this line is being executed right after another query call is made. The weird thing is that call makes the stringURL the same way, yet it works fine.
Any help would be appreciated...
You need to use %i to log an NSInteger, not %#
You need to use the following
NSString *stringURL = [NSString stringWithFormat:#"http://api.themoviedb.org/3/movie/%d/trailers?api_key=1523229ded5824dab8bb7840782db266",searchID];
Because searchID has NSInteger type and you are using "%#"
If it's an NSInteger you need to use %ld or you will got a warning, you can also use %d and explicitly cast to int via (int)searchID

Recognize URL in xCode

Is it possible to recognize an url in xCode ?
I'm trying to create an if statement, followed by opening the url.
I can't find anything about this on the internet, I've also tried searching for recognizing the first letters (like "http, file, www");
With kind regards,
Tim
NSString class has a method rangeOfStrig:
NSString *urlString = #"http://www.someurl.com";
if ([urlString rangeOfString:#"www"].location != NSNotFound ) {
// do something
}
Use a regular expression to check for the patterns you want (www, http, https, etc)
http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html
And here is a great regex http://www.geekzilla.co.uk/view2D3B0109-C1B2-4B4E-BFFD-E8088CBC85FD.htm
And here is an example from stackoverflow, How to validate an url on the iPhone

NSHost "currentHost" not recognized

i'd like to use this code to know my ip, but i got 2 warnings that i can't fix for now. I also found this post : Accessing IP Address with NSHost
but i just wanted to understand why this code does not work, if anyone has an answer?
here's my code :
-(NSString*)getAddress {
NSString *iphone_ip = [NSString initWithString:#"127.0.0.1"];
NSHost* myhost =[NSHost currentHost];
if (myhost)
{
NSString *ad = [myhost address];
if (ad)
strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]);
}
return [NSString stringWithFormat:#"%s",iphone_ip];
}
the first warning is on :
NSHost* myhost =[NSHost currentHost]
, saying that currentHost is not recognised.
The second one is on
NSString *ad = [myhost address];
" incompatible obj-c types initializing 'struct NSData ", expected 'struct NString" "
I can imagine the second warning might disappear when the first warning is resolved...
Thanks for your help
Paul
As you can see from the documentation here, it's documented under the OS X library.
NSHost
It's actually a private API on the iPhone. You should still be able to use it, but you'll get compiler warnings.
If you need to find out your IPAddress, you can use a NSURLRequest and NSURLConnection using this URL: WhatIsMyIP API
That page is there specifically for programmers to use. They ask that you ping it no more than once every 300 seconds. You can find a FAQ here: FAQ

NSURL Exceptions

I am trying to grab a path value from an array for an NSURL to set an icon in my app. I get an
NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x5622590.
If I use an nslog I get the expected output:
NSLog(#"%#",[[wforecast.wicons objectAtIndex:0]valueForKey:#"nodeContent"]);
Which gives me:
Im setting the value as follows
NSURL *urlpath;
NSString *urls = [[wforecast.wicons objectAtIndex:0] valueForKey:#"nodeContent"];
urlpath = [NSURL URLWithString:(NSString *)urls];
I appreciate this is a longwinded way of doing things but I was trying to break up the individual components to find out what was going wrong but I am at a loss!
You have essentially the same problem as this other questioner had. You passed an object that is not an NSString where you needed to pass an NSString.
Use the debugger to determine exactly where the exception occurred. If you haven't done this, I wouldn't be so sure that the code you showed is what caused it; the debugger will tell you where the exception occurred with no room for doubt.
Once you've found where the exception occurred, you can examine the object that you passed, and look back at where you got it from. You need to fix either how you retrieve the string or how you stored it in the place you're now getting it from.

"Invalid" iphone string

I'm converting data (from a web page) to a string). The basic code works (but there's been some subtle change somewhere - maybe on server).
NSLog shows the expected string (maybe 1000 chars long). However, when I float over responseString, it shows "Invalid". Worse, parsing with componentsSeparatedByCharactersInSet does not work.
Ideas?
NSString *responseString;
responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog([NSString stringWithFormat:#"responsestring ='%#'",responseString]);
if ([responseString compare:#""] != NSOrderedSame) {
lines = [responseString componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:#";"]];
This may happen when the configuration is set to "Release" rather than "Debug" I think.
Do not trust what the debugger says, it is not accurate, this has happened to me and took me a while to realise that the xcode debugger is not always right and should not be trusted, so i no longer trust the debugger and i use nslog statements whenever it tries to tell me something is invalid. So dont worry about it it happens, and when it happened to me I was also parsing responses from some webservice.
Just to be clear -- my experience with seeing "Invalid" in the debugger means that the reference is to an already-released object.
Your question and the comments below seem to suggest that you are thinking "Invalid" is an actual string value -- but are you sure you don't just have a memory management probably?