Issues with escaping a NSString - ios7

I want to escape an NSString to use in NSURL. Here the line...
NSURL *url = [NSURL URLWithString:#"http://cdn.tutsplus.com/mobile/uploads/2013/12/sample.jpg" stringByAddingPercentEscapesUsingEncoding:NSUFT8StringEncoding];
But I get an error:
No known class method for selector: "URLWithString:string By Adding
Percent Escape Using Encodin"
Could you figured out what's the problem?

You mean surely:
NSURL *url = [NSURL URLWithString:[#"http://cdn.tutsplus.com/mobile/uploads/2013/12/sample.jpg" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
And take care of NSUFT8StringEncoding vs NSUTF8StringEncoding :)

Related

Why do I get this error when I try to pass a parameter in NSURL in iOS app?

This is what I have in a public method - (IBAction)methodName
NSString *quoteNumber = [[self textBox] text];
NSURL *url = [[NSURL alloc] initWithString:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber];
The error I get is:
Too many arguments to method call, expected 1, have 2
What am I doing wrong?
I think you are thinking of NSString's stringWithFormat::
[NSURL URLWithString:[NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%#", quoteNumber]]
Also note the change to %# for the format specifier, since it is an instance of NSString (not an int)
You need to format your string. Try this:
NSString *urlString = [NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%#", quoteNumber];
NSURL *url = [[NSURL alloc] initWithString:urlString];
The initWithString method can only accept a normal NSString, you are passing it a formatted NSString, Take a look at this code:
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quotedNumber]];
That might be a bit confusing, you can break it up as follows:
NSString *urlString = [NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quotedNumber];
NSURL *url = [[NSURL alloc] initWithString:urlString];
Now your string is properly formated, and the NSURL initWithString method will work!
Also, just so it is clearer for you in the future, you can take advantage of Objective-C's dot notation syntax when you set your quoteNumber string, as follows:
NSString *quoteNumber = self.textBox.text;
Also, you are trying to pass this quoted number into your urlString as a digit (as seen with the %d), remember that quotedNumber is a NSString object, and will crash when you try to pass it to the stringWithFormat method. You must convert the string first to a NSInteger, or NSUInteger.
Please refer to this SO question to how to do that (don't worry it's very easy)!
The problem is
[NSURL initWithString:]
requires ONE parameter of NSString type but you passed TWO parameters .
You need to pass a single NSString parameter . Change your code from
NSURL *url = [[NSURL alloc] initWithString:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber];
to
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber]];

How to encode sharp sign in NSURL

I have this scenario that I need to send a GET http request to the remote server. I went with NSURLConnection and intercept the request in HTTPScoop.
The url format is something like this:
http://domain.com?key=username##somehash&url=someotherurl.com
I am doing it like this:
NSString *urlString = [[NSString alloc]init];
urlString = #"http://domain.com?key=username##somehash&url=someotherurl.com";
NSString *encodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:encodedString]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:#"GET"];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
In this case I didn't escape the # sign, and the request I see in httpscoop is:
http://domain.com?key=username223somehash&url=someotherurl.com
If I escape the sharp sign to %23, it gets to something like this in httpscoop:
http://domain.com?key=username22523somehash&url=someotherurl.com
I have tried different combinations but always have issue with the sharp sign. Are there any walk-around for this? Thanks!
replace # with %23 (source).
edit - oops, didn't see the rest of your message. Not sure about NSURL, but I have had difficulty encoding parameters in URLs with NSURL before, too. I ended up using ASIHTTPRequest, which took care of all the encoding issues. I would recommend doing the same.

Unable to create Request: Bad URL

When i try to look for a location like this: Florida,USA it goes OK, but when i try it like this: Florida USA, i got that error:
Unable to create Request (bad url?)
The problem belongs with the spaces on the location taped, is there any way to resolve that?
NSString *urlString = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/geocode/json?address=%#&sensor=true",theLocationString];
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
I think the problem is that you need to urlencode your string. A space character isn't a valid url string:
NSString *urlString = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/geocode/json?address=%#&sensor=true",theLocationString];
//Create a URL object.
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding]];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
If NSASCIIStringEncoding doesn't work you could try: NSUTF8StringEncoding
..fredrik

NSURL returns Nil Value

Here Below is my code
NSString *string = [NSString stringWithFormat:#" http://abc.com /Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#& ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];
Here in string there is value but url returns nil.
Can Anyone tell why this happened.
Thanks ....
"This won't work, so here's what I did instead"
NSString *string = [NSString stringWithFormat:#"http://abc.com/Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];
NSURL will return nil for URLs that contain illegal chars, like spaces.
Before using your string with [NSURL URLWithString:] make sure to escape all the disallowed chars by using [NSString stringByAddingPercentEscapesUsingEncoding:].
Here is the class reference for NSString.
Hi All Now My Problem Is solved here I did a mistake left a blank space before "http".
The correct code is
NSString *string = [NSString stringWithFormat:#"http://myserver.com/Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];

Cocoa path string conversion

I want to convert /Users/Irwan/Documents/test.jpg into MyMac:Users:Irwan:Documents:test.jpg
I can do that manually but I wonder if there is easy way to do it ?
thanks
NSURL *url = [NSURL fileURLWithPath:path];
path = (NSString *)CFURLCopyFileSystemPath((CFURLRef)url, kCFURLHFSPathStyle);
[path autorelease];