Timmy
Timmy

Reputation: 107

URL with special character in Objective-c

I'm trying to use this URL in my application so that, when the user provides the right info he will be able to log in:

NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:@"https://www.example.come/login/CheckEligibility?json={'username'%%3A'%@'%%2C'password'%%3A'%@'}", username.text,passowrd.text]]];

For some reason I'm getting the exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

and when I test the URL in the browser, it works fine and gives me the data I want.

Upvotes: 0

Views: 722

Answers (2)

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

Try this

NSString *str = [NSString stringWithFormat:@"https://www.example.come/login/CheckEligibility?json={'username':'%@','password':'%@'}", username.text, passowrd.text];
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
NSData *data = [NSData dataWithContentsOfURL:url];

Upvotes: 2

Roland Keesom
Roland Keesom

Reputation: 8288

You need to encode the string like:

[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Upvotes: 0

Related Questions