Get root of website from NSString or NSUrl - ios

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"];

Related

convert Nsurl Domain to my country domain

how to convert Nsurl Domain to my country domain
Example :
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
How can get some thing like http://www.google.com.eg
it can detect my zone and change it with my domain zone, if website support multi zones domain.
If there isn't a path in the url just append .eg to the end of the string.
NSString *newCountryString = [[url absoluteString] stringByAppendingString:#".eg"];
NSURL *newCountryUrl = [NSURL URLWithString:newCountryString];
If the url has a path do this:
NSString *host = [url host];
NSString *domain = [[host componentsSeparatedByString:#"."] lastObject];
NSString *newCountryString = [[url absoluteString] stringByReplacingOccurrencesOfString:domain withString:[domain stringByAppendingString:#".eg"]];
NSURL *newCountryUrl = [NSURL URLWithString:newCountryString];

NSURL nil because of spaces

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

NSString object returns nil value

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

How to combined multiple NSString in Objective-C?

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

In Facebook api how to set the limit for Feeds to get next 5 feeds?

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"]];

Resources