Read URL content XCode sigabrt error - ios

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

Related

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 decode NSURL in iOS?

i am NewBie in iOS Development. I want to Decode my NSURL With Pagination Here my Url Like as
http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%B8%E0%AB%8D%E0%AA%B5%E0%AA%BE%E0%AA%B8%E0%AB%8D%E0%AA%A5%E0%AA%AF&page=0
And i Decode this URL Like as
NSURL *urls = [NSURL URLWithString:#"http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%B8%E0%AB%8D%E0%AA%B5%E0%AA%BE%E0%AA%B8%E0%AB%8D%E0%AA%A5%E0%AA%AF&page=0"];
NSString *urlString = [urls absoluteString];
NSLog(#"UrlString %#",urlString);
NSURL * url=[NSURL URLWithString:urlString];
But it Give me Only 0 Page Url i want to Like as make my url as
NSURL *urls=[NSURL URLWithString:#"My Url &page=%d",pagenum];
Here i want at place of My Url as Decoding of my Original Url If it is Possible then Please Give me Solution for it.
you are doing it in wrong way.
this is the right way
NSString *urlString = #"http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%B8%E0%AB%8D%E0%AA%B5%E0%AA%BE%E0%AA%B8%E0%AB%8D%E0%AA%A5%E0%AA%AF&page=";
NSURL *targetURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#%d",urlString,pageNumber]];
NSLog(#"targetURL is : %#",targetURL);
Hope this will help you..
NSString *str=#"http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%B8%E0%AB%8D%E0%AA%B5%E0%AA%BE%E0%AA%B8%E0%AB%8D%E0%AA%A5%E0%AA%AF&page=0";
NSString *result =[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *urls = [NSURL URLWithString:result] ;

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

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