The endpoint does not care if the URL has spaces in it, but NSURL seems to.
I get nil for NSURL *url = [NSURL URLWithString:string]; every time:
NSString *string = [NSString stringWithFormat:#"http://endpoint.com/search?one=%#&two=%#", textField1.text, textField2.text];
NSURL *url = [NSURL URLWithString:string];
NSString *urlAbsolute = [url absoluteString];
[theManager GET:urlAbsolute parameters:nil progress:nil success:^(NSURLSessionTask * _Nonnull task, id _Nullable responseObject) {
I want to pass spaces into the endpoint because I want to get spaces out of the endpoint. (i.e. pass "Star Wars" in, so I get "Star Wars" back out instead of "StarWars" if I removed spaces from the string).
Any ideas?
Use the string encoding with allowed characters
Please find the below code.
NSString *string = [NSString stringWithFormat:#"http://endpoint.com/search?one=%#&two=%#", textField1.text, textField2.text];
NSURL *url = [NSURL URLWithString:[string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]];
Try this code :
NSString *firstArgument = [self.textField1.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSString *secondArgument = [self.textField2.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSString *string = [NSString stringWithFormat:#"http://endpoint.com/search?one=%#&two=%#", firstArgument, secondArgument];
NSURL *url = [NSURL URLWithString:string];
NSString *urlAbsolute = [url absoluteString];
Related
NSString *main = [NSString stringWithFormat:#"http://www.google.co.in/maps?q=restaurants&sll=%f,%f&radius=50&output=json",objRestaurantFinderAppDelegate.currentLatitude,objRestaurantFinderAppDelegate.currentLongitude];
NSError *error;
NSURL *mainUrl = [[NSURL alloc]initWithString:main];
NSString *str = [NSString stringWithContentsOfURL:mainUrl encoding:NSStringEncodingConversionAllowLossy error:&error];
You can use as
NSURL *mainURL = [NSURL URLWithString:#"http://www.google.co.in/maps?q=restaurants&sll=23.03957,72.56600&radius=10&output=json"];
In your case Just do
NSURL *mainUrl = [NSURL URLWithString:main];
Like the title. How to combined multiple NSString in Objective-C ?
NSString *SERVER_IP = #"123.45.678.123";
NSString *PORT = #"12345";
NSString *USER_ID = #"123123-123123-123-123-123123";
I want to combined the above String in to URL , and I try to use the following code. But it seems didn't work...
NSURL *url = [NSURL URLWithString:#"http://%#:%#/user/%#",SERVER_IP,PORT,USER_ID];
How to combined multiple NSString in Objective-C ?
I am new to IOS...Thanks in advance.
You were almost right ! You should do it like this :
NSString *SERVER_IP = #"123.45.678.123";
NSString *PORT = #"12345";
NSString *USER_ID = #"123123-123123-123-123-123123";
NSString *URLString = [NSString stringWithFormat:#"http://%#:%#/user/%#",SERVER_IP,PORT,USER_ID];
NSURL *url = [NSURL URLWithString:URLString];
your coding is fine
NSString *SERVER_IP = #"123.45.678.123";
NSString *PORT = #"12345";
NSString *USER_ID = #"123123-123123-123-123-123123";
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#:%#/user/%#",SERVER_IP,PORT,USER_ID];];
u need to convert NSUrl to NSString
NSString *myString = [url absoluteString];
I have to parse the return of a JSON API for which I've taken the URL into a string and after that I passed it NSURL but in NSLog it is showing nil.
Here is my code,
NSString *urlstring=[NSString stringWithFormat:#"http://api.v3.factual.com/t/places?q=%#&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[self.strurl lowercaseString]];
NSURL *url=[[NSURL alloc]initWithString:urlstring ];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
Thanks in advance.
You must encode your url as it contains special characters, Try something like this
NSString *paramString=[NSString stringWithFormat:#"/t/places?q=%#&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[self.strurl lowercaseString]];
NSString *encodedSearchString = [paramString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"http://api.v3.factual.com%#", encodedSearchString];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(#"urlstring is %#",url)
Hope this helps
Try this, Tested and Working
-(void) sample
{
// NSString *strurl = #"hello";
NSString *urlstring=[NSString stringWithFormat:#"http://api.v3.factual.com/t/places?q=%#&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[strurl lowercaseString]];
NSURL *url=[NSURL URLWithString:[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSLog(#"%#",req);
}
I am using the following code to get the feeds:
NSDictionary *dirTemp;
NSError *error;
NSStringEncoding encoding;
NSString *strUrl = [NSString stringWithFormat:#"https://graph.facebook.com/%#/posts?access_token=AppID|AppSecret&limit=5",strIdValue];
//NSLog(#"Your String URL is %#",strUrl);
NSURL *url = [NSURL URLWithString:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *strResonse = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error];
dirTemp = [strResonse JSONValue];
NSDictionary *dirTemp;
NSStringEncoding encoding;
NSString *strUrl = [NSString stringWithFormat:#"graph.facebook.com/%#/…;
NSURL *url = [NSURL URLWithString:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *strResonse = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error];
dirTemp = [strResonse JSONValue];
NSDictionary *paginationDict=[dirTemp valueForKey:#"paging"];
NSString *pagelink=[[NSString alloc]initWithString:[paginationDict objectForKey:#"next"]];
Any ideas how I can get the root of a website from an NSString or an NSURL? So if my URL was http://www.foo.com/bar/baragain how would I get http://www.foo.com/?
By using [url scheme] and [url host] like so:
NSURL *url = [NSURL URLWithString:#"http://www.foo.com/bar/baragain"];
NSLog(#"Base url: %#://%#", [url scheme], [url host]);
// output is http://www.foo.com
NSURL *url = [NSURL URLWithString:#"http://www.foo.com/bar/baragain"];
NSURL *root = [NSURL URLWithString:#"/" relativeToURL:url];
NSLog(#"root = '%#'", root.absoluteString); // root = 'http://www.foo.com/'
You can remove the NSURL's path from the string. This accounts for port numbers and other data that are part of the root site.
NSString *urlString = #"http://www.foo.com/bar/baragain";
NSURL *rootUrl = [NSURL URLWithString:urlString];
NSString *rootUrlString = [urlString stringByReplacingOccurrencesOfString:rootUrl.path withString:#""];
NSString *str = #"http://www.foo.com/bar/baragain";
NSArray *temp = [str componentsSeparatedByString: #".com"];
str = [NSString stringWithFormat: #"%#%#", [temp objectAtIndex:0], #".com"];