2012-12-14 121 views
0

我想使用這個網址在我的應用程序,這樣,當用戶提供正確的信息,他將能夠登錄:URL與Objective-C的特殊字符

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

出於某種原因我得到的例外:

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

當我在瀏覽器中測試的URL,它工作正常,並給我我想要的數據。

回答

0

你需要像編碼字符串:

[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
2

試試這個

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]; 
+0

它的工作,我拼錯的東西。非常感謝! – Timmy