NSString object returns nil value - ios

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

Related

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

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

Read URL content XCode sigabrt error

Here is my code and it gives "Program received signal "SIGABRT".
I'm new at iOS Development.
- (IBAction)getData {
NSURL *URL = #"http://www.oeslabs.com/aa.txt";
NSData *data = [NSData dataWithContentsOfURL:URL];
NSString *string = [NSString stringWithUTF8String:[data bytes]];
if (string == nil) {
// an error occurred
label1.text =#"Error";
}
else{
label1.text = string;
}
}
You Also Can put code as
NSString *urlString = [NSString stringWithFormat:#"http://www.oeslabs.com/aa.txt"];
NSURL *myUrl = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
.
.
.
.
.
Here is Official Documentation of stringByAddingPercentEscapesUsingEncoding. use for convert the legal URL string.
You are trying to assign NSString
to NSURL
replace
NSURL *URL = #"http://www.oeslabs.com/aa.txt";
With
NSURL *URL = [NSURL URLWithString:#"http://www.oeslabs.com/aa.txt"];

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

Get root of website from NSString or NSUrl

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

Resources