NSURL doesn't seem to want to accept my querystring - iphone

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:

Related

Adding components to path - Objective C

I have a url say : www.gmail.com/index.php. I also have three different strings say aa, bb and cc. I want to append the three separate strings to the url in order to get a combined url in Objective C i.e I want www.gmail.com/index.php/aa/bb/cc .
Can someone help me out ?? I am new to objective C. Thanks.
If you are trying to use a URL for later use in retrieving data, etc., you're going to want an NSURL object when you're done. You can certainly create a URL by appending #"/%#" with the next element, but you should look at doing the following:
NSURL *originalURL = [NSURL URLWithString: #"http://www.gmail.com/"];
NSURL *aaNextURL = [originalURL URLByAppendingPathComponent: #"aa"];
NSURL *bbNextURL = [aaNextURL URLByAppendingPathComponent: #"bb"];
NSURL *ccNextURL = [bbNextURL URLByAppendingPathComponent: #"cc"];
This is a bit long-winded if you are adding static path elements, but if you are adding computed information it solves the problem of making sure there are appropriate slashes and allows you to use the URL construct directly.
Use stringByAppendingPathComponent

TBXML root element is null no matter what I do

Wracking my brain for hours trying to figure this one out. TBXML must have the rootXMLElement set in order to begin traversing and parsing data.
No matter what I do, when I NSLog it, it's null.
Here's a sample of the XML:
<?xml version= "1.0" encoding="UTF8"?>
<patients>
<patient>
<patientNumber>1234</patientNumber>
<nameFirst>Jason</nameFirst>
<!--more properties of a patient-->
</patient>
<patient>
<patientNumber>5542</patientNumber>
<nameFirst>Gary</nameFirst>
<!--more properties of a patient-->
</patient>
</patients>
The code I'm using thus far:
NSURL *xmlURL = [NSURL URLWithString:destPath];
TBXML *tbxml = [TBXML tbxmlWithURL:xmlURL];
NSLog shows tbxml.rootXMLElement as null.
Other details of note:
I use a ruby script to delete any non-ASCII characters from the XML file. The app does not require anything more.
I thought the problem might be that the XML file was not UTF-8, so I used bash command iconv to convert it from ASCII to UTF-8. Didn't work either.
Any suggestions are greatly appreciated.
It looks like it is a local XML based on the fact that you are preprocessing it. When you are processing local file paths as URLs you need to use fileURLWithPath: for it to work properly. So you need to do this,
NSURL *xmlURL = [NSURL fileURLWithPath:destPath];
TBXML *tbxml = [TBXML tbxmlWithURL:xmlURL];

NSURLConnection is not downloading files with spaces in the name

I am working on loading images into a gallery on the iPhone and running into an issue. It is apparent that something in the script isn't happy with spaces being in filename's when trying to download the images off of the internet.
This is the connection line.
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]] delegate:self];
And I pass an NSString in the NSURL. It works on all photos that don't have spaces.
Example of evil photos:
thumbs_WJ (16).jpg
thumbs_WJ (25).jpg
Now I know I could go back and update all the photos, update the database, and change the script so it doesn't add spaces anymore...but we are talking about thousands of photos.
And suggestions?
you need to do string:
str =[str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
Use NSString's stringByAddingPercentEscapesUsingEncoding: method on the text you want to include as an argument.
From Apple docs:
stringByAddingPercentEscapesUsingEncoding:
Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.
If your string is a "normal" string, you can use NSUTF8StringEncoding as encoding. Otherwise, specify the encoding your string is in.
Just replace spaces with "%20" in your urlstring.

String Conversion in Objective-C

In my program, I'm allowing the user to input some words into a textfield. Then my program will use these words to form a html string. The string will then be used in NSURL so I can connect to the website.
This method works great for english words. But when I input some chinese (or korean) in there, I does not work. Thus I believe that I need to convert the inputed data before passing it to NSURL. However I could not find a way to do this.
Here's an example of how my code looks like.
NSString *searchedString = theSearchBar.text;
NSString *urlToBeSearched = [[NSString alloc]
initWithFormat:#"http://www.awebsite.com/search/%#",
searchedString];
NSURLRequest *urlRequest = [[NSURLRequest alloc]
initWithURL:[NSURL URLWithString:urlToBeSearched]
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:50];
NSURLConnection *tempCon =[[NSURLConnection alloc] initWithRequest:urlRequest
delegate:self];
Then of course releasing them later.
For example, when searchedString = 你好, the urlLink will be http://www.awebsite.com/search/你好. NSURLConnection doesn't like that and will give me "Bad Url" error.
However, if the urlLink is "%E4%BD%A0%E5%A5%BD" it will give me the correct link.
The set of characters allowed in a URI is pretty much limited to a subset of US-ASCII (see RFC2396). That means your Chinese characters must be percent escaped in the URI. The documentation for NSURL +URLWithString: says the string must conform to the RFC so you need to percent escape the string before calling that method.
Fortunately, NSString has a method that will allow you to do that called -stringByAddingPercentEscapesUsingEncoding:. You still need to choose a suitable encoding and which one you choose depends on how the server decodes the URL string. The easiest option is probably to use UTF-8 at both ends. You need to do something like:
NSString* searchedString = [theSearchBar.text stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString* urlToBeSearched = [NSString stringWithFormat:#"http://www.awebsite.com/search/%#", searchedString];
// everything else the same, except you don't need to release urlToBeSearched
Did you try encoding your search-string?
[NSString stringByAddingPercentEscapesUsingEncoding:/encoding-of-choice/];
Not sure what encoding you would use for chinese characters though.

Google Maps http request not working

I am trying to use an http request to google maps to obtain the driving distance between two locations using JSON. However, it seems these are treated different in a browser than in the (iphone) app.
I create an NSString which holds the URL, using coordinates and %20 (a space). I NSLog() the URL to make sure, and it seems ok (ie it works fine in browser and looks fine)...but when NSLogging the string initialised with the contents of that URL, I get (null).
Here is the code:
NSString *urlString=[[NSString alloc] initWithFormat:#"http://maps.google.com/maps/nav?q=from:%.7f%#%.7f%#to:%.7f%#%.7f", testLocation.coordinate.latitude, #"%%20", testLocation.coordinate.longitude, #"%%20", thePark.coordinate.latitude, #"%%20", thePark.coordinate.longitude];
which I then NSLog() and get http://maps.google.com/maps/nav?q=from:51.4986110%20-0.1236110%20to:51.4960938%20-0.2200041 ...to no avail.
NSString *json=[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]];
When I NSLog() this, it prints null. Does anyone have any suggestions as to why this might be happening or an easier way to do it? I plan to then parse the JSON and get the drving distance
You should escape the urlString using "stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding" instead of escaping manually with %20.
Edit:
I tested with these 3 lines of codes and I got the json string.
NSString *urlString=[[[[NSString alloc] initWithFormat:#"http://maps.google.com/maps/nav?q=from:%.7f %.7f to:%.7f %.7f", 51.4986110, -0.1236110, 51.4960938, -0.2200041] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] autorelease];
NSString *json=[[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];
NSLog(#"json = %#", json);