UIWebView - adding an object to the address - iphone

I have a UIWebView that I'd like to load a web page and also append an object to the end of the address.
this is what I have:
NSString *urlAddress = #"http://www.wikipedia.org/%#",recipeName;
the name of a particular recipe would be appended to the end of the URL
I'm getting the "statically allocated instance of Objective-C class NSString" error. Is my syntax incorrect or is it something else?
I've been looking at this for quite a while now and thought I'd ask the community here.
thanks in advance.

NSString *urlAddress = [NSString stringWithFormat:#"http://www.wikipedia.org/%#",recipeName];
Try this code it will work..
hAPPY cODING...

Try this:
NSString *urlAddress = [NSString stringWithFormat: #"http://www.wikipedia.org/%#",recipeName];

You cannot create string with format directly, try [NSString stringWithFormat].
Well not fast enough I guess ...

Related

NSURL Connection Warning "Unused variable"

I am getting data from an NSURL connection like below. The code works, but I get a compiler warning that says "Unused variable strResult."
I don't actually need to use what is returned. I just need the URL to be executed.
How can I go about modifying this to get rid of the warning?
NSString *strURL = [NSString stringWithFormat:#"http://www.site.com/file.php?id=%#", id];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL
encoding:NSUTF8StringEncoding];
Thank you!
You have initialized and assigned data in strResult variable but you haven't used that variable.
So use that variable or just NSLog#("%#",strResult)
i suggest if the variable is not useful then remove that variable it occupies memory
You are not using the strResult anywhere after declaring ie the Warning .
Just log it you can see the warning goes off.
NSString *strResult = [[NSString alloc] initWithData:dataURL
encoding:NSUTF8StringEncoding];
NSLog(#"%#", strResult);
use NSURLConnection and call it's static method:
[NSURLConnction sendSynchronousRequest:.....]
and if you wanna send Asynchronous request use:
[NSURLConnection sendAsynchronusRequest:....]
The compiler just informs you that you created a variable and you are not using it (yet) .. so the best solution is to use it, as a return value of your method. It would help to see the complete implementation of the relevant method to help you better.
Hope this clarifies the warning.
You get that warning, because nowhere in your code, you actually use strResult.
Use it, and the warning is gone :) Or, remove the statement if you don't actually need the repsonse.

how can i query google places for country specific cuisine?

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!

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

Issue posting variable with iPhone SDK

I am encountering an issue while posting a variable with Xcode:
While running this code the app crashes while posting the variable to a webservice:
NSArray *array = [stringFromFile componentsSeparatedByString: #","];
NSString *time = [array objectAtIndex:1];
UTCorLocal = time;
NSLog(#"%#", UTCorLocal);
UTCorLocal variable is declared earlier in the code. The NSLog outputs the correct string, but when I try to use it further along in the code it crashes.
When I give the variable a static value like this:
UTCorLocal = #"UTC";
It all runs like it should do!
Could anybody please help, it's driving me crazy!
Thanks a lot,
Ron
It's probably released somewhere along the way, I would guess, without knowing the rest of your code. Try copying or retaining 'time' and see what happens.

NSURL doesn't seem to want to accept my querystring

Hi there I have this code
NSLog(#"%#",URLRequestQueryString);
NSString *sendToServerString = [NSString stringWithFormat:#"http://mydomain.co.uk/req.php%#",URLRequestQueryString];
NSURL *sendToServer = [[NSURL alloc] initWithString:sendToServerString];
NSLog(#"%#",sendToServer);
NSLog(#"%#",sendToServerString);
URLRequestQueryString is just a standard querystring that I have built up throughout the script.
The first NSLog works fine and outputs a proper querystring (if I copy and paste it into a browser then the page will load and run correctly.
This is also the case when I output sendToServerString it correctly outputs the URL with querystring (which I can also copy and paste into a browser).
However sendToServer ouputs (null). If I remove the querystring it will correctly output the domain and path.
Any idea why this happens? How can I sort it?
Thank you.
NSURL *sendToServer = [NSURL URLWithString: [sendToServerString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
Could be what you are looking for. :)
You need see NSString reference.
A Section named "Working with URLs".
Under this section has two method
stringByAddingPercentEscapesUsingEncoding:
stringByReplacingPercentEscapesUsingEncoding: