Below is the string that I need to create as a url for GET request. I am always getting nil in NSURL.
Can anyone help me ?
http://example.com/webexternal1/api/Values/sendorder?orderlist={code:"122|155",rate:"60|5",qty:"5|5"}&tab=N2
//strParams = {code:122|155,rate:60|5,qty:5|5}&tab=N2
dataUrl = [#"http://example.com/webexternal1/api/Values/sendorder?orderlist=" stringByAppendingString:strParams];
urlComponants = [[NSURLComponents alloc]initWithString:dataUrl];
NSURL *url = urlComponants.URL;
Try url encoding the strParams.
NSString* strParams = [#"{code:122|155,rate:60|5,qty:5|5}&tab=N2" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Related
I am creating a NSURL URL will contain some escape character (Japanese)
NSString* currentlocationbarString = #"mbos.help.jp/search?q=専門&pg=1"
NSString *escapedString = [currentlocationbarString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:escapedString];
//url is mbos.help.jp%2Fsearch%3Fq=%E5%B0%82%E9%96%80&pg=1
When I create NSURLComponents and try to get query items it gives me nil.
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url
resolvingAgainstBaseURL:YES];
NSArray *queryItems = urlComponents.queryItems;
//here issue with queryItems
if anybody has solution to get query items please help. Thanks in advance
Issue is not with Unicode Characters, whenever you add encoding use proper character set for my case I was using following setURLHostAllowedCharacterSet it means your NSURLComponents only give encoding for your Host, to get correct queryItems use URLQueryAllowedCharacterSet like this way.
NSString* currentlocationbarString = #"mbos.help.jp/search?q=専門&pg=1"
NSString *escapedString = [currentlocationbarString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:escapedString];
So now you can get queryItems.
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url
resolvingAgainstBaseURL:YES];
NSArray *queryItems = urlComponents.queryItems;
At least one of the characters 専門 that you use in your search string is invalid Unicode in the form of unpaired UTF-16 surrogate chars, and thus cannot be encoded by stringByAddingPercentEncodingWithAllowedCharacters:, which therefore returns nil.
You can find an example in this post.
Apparently, you had to check for Japanese characters, if encoding is possible.
I must say, I did not expect that either!
I am getting something when I convert string to NSURL.
My code id...
NSString *urlString = [[NSString alloc]initWithFormat:#"http://host name/index.php?id=%#&mob=%#&name=%#&mail=%#&m=23", self.deviceId, self.pnumber, self.name, self.email];
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *urlPattern = [NSURL URLWithString:urlString];
NSLog(#"%#", urlPattern);
http://host name/index.php?dev_id=73EA1D1D-E6E3-485C-883D-DF952E116&mob=%3CUITextField:%200x7fe247063c00;%20frame%20=%20(0%20128.333;%20310.667%2040.6667);%20text%20=%20'';%20opaque%20=%20NO;%20autoresize%20=%20RM+BM;%20gestureRecognizers%20=%20%3CNSArray:%200x608000244c50%3E;%20layer%20=%20%3CCALayer:%200x60800003eee0%3E%3E&name=%3CUITextField:%200x7fe247055800;%20frame%20=%20(0%2044;%20310.667%2040.3333);%20text%20=%20'';%20opaque%20=%20NO;%20autoresize%20=%20RM+BM;%20gestureRecognizers%20=%20%3CNSArray:%200x608000244410%3E;%20layer%20=%20%3CCALayer:%200x60800003e240%3E%3E&mail=%3CUITextField:%200x7fe24601ae00;%20frame%20=%20(0%20213;%20310.667%2040.3333);%20text%20=%20'';%20opaque%20=%20NO;%20autoresize%20=%20RM+BM;%20gestureRecognizers%20=%20%3CNSArray:%200x608000243c90%3E;%20layer%20=%20%3CCALayer:%200x60c000038580%3E%3E&m=23
How to convert string to NSURL
Probably you are assigning textfield to urlString. Check for self.pnumber whether its a textfield or string. If textField then set as self.pnumber.text and similarly check for all data.
change self.pnumber, self.name, self.email to self.pnumber.text, self.name.text, self.email.text
use like
NSString *urlString = [[NSString alloc]initWithFormat:#"http://host name/index.php?id=%#&mob=%#&name=%#&mail=%#&m=23", self.deviceId, self.pnumber.text, self.name.text, self.email.text];
Try This
let NSHipster = URL(string: "http://nshipster.com/") //returns a valid URL
let invalidURL = URL(string: "www.example.com/This is a sentence") //Returns nil
for more detail:
http://www.codingexplorer.com/creating-and-modifying-nsurl-in-swift/
I need to send french string "Commentaire d’arret" to webservice in objective c.
But my app crashes when I am sending this string as parameter to service. And normal string like "india" is working fine.
Can any one help me.
Encode your parameter for a GET request:
let param = "Commentaire d’arret"
let encodedParam = (param as NSString).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
Results in Commentaire%20d%E2%80%99arret.
If you want to send a POST request prepare your post body like this:
let postString = "param=consultés"
let postData = postString.data(using: .utf8)
Objective-C
NSString *param = #"Commentaire d’arret";
NSString *encodedParam = [param stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *postString = #"param=consultés";
NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
As you said I have checked your code and you need to do encoding before converting to your URL. try this.
NSString *searchApi = [NSString stringWithFormat:#"xxx/xxx/index?
q=#consultés"];
searchApi = [searchApi stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *tempURL = [NSURL URLWithString:searchApi];
Try this:
NSString *strParam = #"Commentaire d’arret";
NSString *encodedParam = [strParam stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
I have a strange issue with Xcode6.1
_mainURl is my ' ServerLink '
service is ' GetUserById '
NSString* str = [NSString stringWithFormat:#"%#%#",_mainURl,service];
NSURL *url = [NSURL URLWithString:str];
When I append two strings and Create a url with NSURL, url getting 'null'
But, When I directly given the server link followed by serviceName I can generate URL.
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"ServerLink/GetUserById"]];
Based on what you have written, the string you format would be "ServerLinkGetUserById". That is different to the string you enter manually of #"ServerLink/GetUserById".
Try updating your format to be:
NSString* str = [NSString stringWithFormat:#"%#/%#",_mainURl,service];
NSString* str = [NSString stringWithFormat:#"%#/%#",_mainURl,service];
NSURL *url = [NSURL URLWithString:str];
don't you try like this ??
i think i am not sure exact what issue having you but as your bellow description, i think you left this part
I used this code from the Stack Overflow question: URLWithString: returns nil:
//localisationName is a arbitrary string here
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* stringURL = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];
When I copied it into my code, there wasn't any issue but when I modified it to use my url, I got this issue:
Data argument not used by format string.
But it works fine. In my project:
.h:
NSString *localisationName;
.m:
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* stringURL = [NSString stringWithFormat:#"http://en.wikipedia.org/wiki/Hősök_tere", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
How can I solve this? Anything missing from my code?
The # in the original string is used as a placeholder where the value of webName is inserted. In your code, you have no such placeholder, so you are telling it to put webName into your string, but you aren't saying where.
If you don't want to insert webName into the string, then half your code is redundant. All you need is:
NSString* stringURL = #"http://en.wikipedia.org/wiki/Hősök_tere";
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
The +stringWithFormat: method will return a string created by using a given format string as a template into which the remaining argument values are substituted. And in the first code block, %# will be replaced by value of webName.
In your modified version, the format parameter, which is #"http://en.wikipedia.org/wiki/Hősök_tere", does not contain any format specifiers, so
NSString* stringURL = [NSString stringWithFormat:#"http://en.wikipedia.org/wiki/Hősök_tere", webName];
just runs like this (with the warning Data argument not used by format string.):
NSString* stringURL = #"http://en.wikipedia.org/wiki/Hősök_tere";