NSString Converting # to %40 on Request - iphone

Currently I'm using Resty (http://projects.lukeredpath.co.uk/resty/) to build my own API wrapper for Objective-C.
I digress however, in the request itself, I am able to determine what is actually being sent and it seems that the NSString *email (I scan for an # to make sure it is a legitimate email before storing in another NSString) is actually changed.
An example, joe#example.com has been scanned for the #, and then sent to the NSString *email for the request. However, when I send the actual GET request, the NSString is changed to joe%40example.com.
Is this due to the Resty request, or do I have to encode/decode the NSString before the request?

Yes, you should URL decode and then perform any validation tasks.
Here is what their documentation has:
NSData or any other data-encodable
payloads set the request body as-is;
encodable payloads will be encoded
using UTF8
http://projects.lukeredpath.co.uk/resty/documentation.html

Related

Validate a XML file in IOS

I have a XML parser. I'm getting the XML file from server and write that XML file in to a local file in cache. Before do that, I want to check the URL has the XML file. How Can I check the available URL's page is a XML page or another type of page(Ex:HTML,PHP)?? Simply how can I identify a XML file ??
Ultimately, you have to look at the contents of the data retrieved to make sure it's valid XML, and parsing is the easiest way to do that.
If you're retrieving the data via a HTTP request, you can, though, also look at the response you receive before you start receiving the actual data. For example, if using NSURLConnection, you can implement a didReceiveResponse, which should often return a 200 for status code and text/xml for content type:
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if ([response isKindOfClass:[NSHTTPURLResponse class]])
{
NSHTTPURLResponse *httpResponse = (id)response;
NSInteger statusCode = httpResponse.statusCode;
NSString *contentType = httpResponse.allHeaderFields[#"Content-Type"];
NSLog(#"%d; %#", statusCode, contentType);
// check to see if statusCode == 200 and/or [contentType isEqualToString:#"text/xml"] here;
}
}
As an aside, the status code and the content type are set by the server, so it is, admittedly, dependent upon the server's implementation (e.g. if the XML is being generated programmatically by the server, hopefully it's setting these HTTP response fields correctly, but if you're retrieving XML from third party servers, you can't be guaranteed that they're well-behaved). But a status code of 200 and content type of "text/xml" are customary and most servers will set these values appropriately if you're just retrieving a XML file.
The most reliable technique for validating your XML is to just receive the data from the server, and submit it to a parser, and see if the parser returns an error or not.
There are various solution available for this:
http://knol2share.blogspot.in/2009/05/validate-xml-against-xsd-in-c.html
http://wiki.njh.eu/XML-Schema_validation_with_libxml2
Checking for proper xml before parsing in NSXMLParser
Hope this will help you.

How to store NSData from NSURLConnection sendSynchronousRequest as a pdf file to documents directory in iOS?

I am getting the NSData using
NSData * responseData = [NSURLConnection sendSynchronousRequest:mutableUrlRequest returningResponse:nil error:nil];
How to store this as a PDF to local documents directory? My service is in java which returns byte array.
Thanks !
try like this
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
[data writeToFile:[docPath stringByAppendingPathComponent:#"name.pdf"]
atomically:YES];
You really need to confirm that your request has been successful:
Provide a pointer for the response which on return contains the status code and the MIME type of the response data, possibly also content length and other useful info. Check against what you expect.
Provide a NSError pointer, too in order to get the error when the connection fails (returns nil).
That's what you should always do when you make a toy app. When you make a more serious app, you should use the asynchronous style to perform a network request. Basically, you implement the two NSURLConnection Delegate protocols. There is lot of info already in SO how to accomplish this, and as well in Apple samples. If you have any specific questions, please ask again :)
How to store this as a PDF to local documents directory?
This question has been answered already.

Decode REST Url in Objective-C

I'm creating a module that receive and REST URL and need to match a pattern and extract the parameters
For example:
the URL "http://Product/1" should match the pattern "http://Product/{productId:long}"
and return the Dictionary with productId as a key and "1" as the value in as long
Does anyone knows about a Framework for IPhone that does it, or at least some of it?
NSURL has a method pathComponents, which returns an array with all the different path components. That should help you get the integer part. To get the name I'd use the host method of the NSURL. The docs say, that it should work if the URL is properly formatted, might as well give it a try then.
All in all, no need to convert into a string, there seems to be plenty of methods to work out the components of the URL from the NSURL object itself.
NSString *path = [[#"path+with+spaces"
stringByReplacingOccurrencesOfString:#"+" withString:#" "]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

API call returning different results based on whitespace encoding (%2520 vs. %20)

I just switched from ASIHTTPRequest to AFNetworking.
My app allows a user to enter a search term, and then I make an api call to my server (rails) to retrieve a list of relevant objects to display on the phone. Simple enough.
Too Long, Won't Read
With the ASI library, the search term sent to the server would be something like st%20helena. Now, with the AFNetworking, my search term sent to the server is st%2520helena. Oddly enough, this is actually making a difference. With %20, I receive no results. With %2520, I receive the results I was expecting.
My question is, why does this make a difference? I know that %2520 is an encoded '%' + 20, which equals a whitespace character, which in my mind should be identical to passing %20.
Details
Before, I was simply appending the search term to the URL after first encoding it:
NSString *encoded = [#"st helena" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *url = [NSString stringWithFormat:#"http://www.example.com/?=%#, encoded];
Now, with AFNetworking, I'm passing an NSDictionary to the parameters argument of AFHTTPClient#getPath.
NSString *encoded = [#"st helena" stringByAddingPercentEscapesUsingEncoding:NSUTR8StringEncoding];
NSDictionary *dict = #{ #"q" : encoded };
[[myClient sharedClient] getPath:#"" parameters:dict ...]
Since AFNetworking encodes items in the dictionary, my search string is being double encoded, which is why the % of '%20' is being converted into '%25'.
My question is, why does this make a difference? I know that %2520 is an encoded '%' + 20, which equals a whitespace character, which in my mind should be identical to passing %20.
No, they aren't identical. The parsing code isn't supposed to just keep decoding over and over again until there aren't any more percent signs left. If that were the case, it would be impossible to transmit the actual percent character. It's only supposed to decode it once. This means that if %2520 goes over the wire, it is decoded into the data %20 and processing stops there. If %20 goes over the wire, it is decoded once into a space. The decoding is only supposed to happen once. If your web service is treating %2520 as a space, then it has a double-decoding bug.

How to fetch web service response as a string in iPhone?

I've webservice which converts CelsiusToFahrenheit. This webservice response is in string format instead of xml so how can I display a response in a label programmatically?
Is there a sample available for that?
The simplest method is a one-liner:
NSError *error = nil;
myLabel.text = [NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];
As with most one-line solutions, there's a caveat. This method blocks the current thread until the request completes or fails, so you'd better use it in conjunction with performSelectorInBackground:withObject: to avoid locking UI.
Here is a tutorial that I used to retrieve data from a web service using the requestWithURL method of NSURLRequest:
http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/
The example is for JSON, but you can just omit all of the JSON stuff from the tutorial, since the string response is going to be fed into your connectionDidFinishLoading method.