I am using NSData to get content from a URL (RSS feed) and then parse it. While most of the URLs are loaded fine, some of them don't return any data.
These URLs open on the web so I know they are valid, but they just don't return any NSData. One such URL - feed://jpl.nasa.gov/multimedia/rss/rovers.xml
Here's how I am using it (the NSURL object is formed properly)
NSURL *feedURL = [NSURL URLWithString:[myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [NSData dataWithContentsOfURL:feedURL];
Any ideas why?
Thanks a lot.
Switch to NSURLConnection so you have some delegate methods to watch what's happening in debugger. check out the URL Loading System docs if you haven't.
Related
I am working on my app where I have to read an HTML file:
The URL is http://www.youtube.com/user/danielmorcombefd/videos.
Then I want to save it as a HTML file, say example.html, and then store it's data in an NSData:
NSData *data = [[NSData alloc] initWithContentsOfFile:#"example.html"];
Kindly suggest me solutions.
-initWithContentsOfURL: perhaps?
I have an IOS project with html file resources shown in a webview. These html files have different sections which correspond to fragments (eg, index.html#section1, index.html#section2), which I would like to load in the webview. Unfortunately using [NSURL fileURLWithPath:url] does not work with fragments. The # is converted to %23 in the url, and the file is not found. If I use the [NSURL URLWithString:url] method, it works, but this method cannot load local resources.
Is there a way to have the webview load the local resource url with the fragment?
As you have noticed, this seems to be impossible for file URLs, but you could use a workaround if you don't mind:
[webView stringByEvaluatingJavaScriptFromString:#"window.location.hash = '#section2';"];
It's not obvious how Apple intended this sort of navigation to be triggered. There might be several ways to do it, but this one does the trick for me:
NSURL *url = [NSURL fileURLWithPath:#"/full/path/to/index.html"];
NSString *fragment = #"#section1";
url = [NSURL URLWithString:fragment relativeToURL:url];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
I want to parse html.. so I have found some sample code over: http://blog.objectgraph.com/index.php/2010/02/24/parsing-html-iphone-development/
it uses hpple to parse html... but there is one problem this application is constantly crashing for some reason most probably it is this line over here:
NSURL *url = [NSURL URLWithString: #"http://www.objectgraph.com/contact.html"];
NSString *contents = [NSString stringWithContentsOfURL:url];
NSData *htmlData = [contents dataUsingEncoding:NSUTF8StringEncoding];
xCode gives me warning stringWithCOntentsofVariable is deprecated..
so could anyone help me solve this problem....by showing what code should I change?
thanks
30 seconds in the documentation shows:
Returns a string created by reading data from the file named by a given URL. (Deprecated in Mac OS X v10.4. Use stringWithContentsOfURL:encoding:error: or stringWithContentsOfURL:usedEncoding:error: instead.)
So, it looks like you should be using stringWithContentsOfURL:encoding:error or stringWithContentsOfURL:usedEncoding:error: instead.
Just like the documentation says.
For use in a questionnaire application, a web service will provide a list of questions in one of several languages, chosen by the user at runtime. The questions will be downloaded from the web service in the chosen language and displayed to the user.
The problem: I have no idea how to do this.
As a sample, I tried loading in UTF-8 text files (e.g. arabic.txt) in the resources containing samples of text in said languages. The files open and render properly in TextMate and TextEdit, but are illegible in Xcode. They are successfully read in, but their contents will not display.
Example:
I create a UITextView and, at initialization:
...
NSString *path = [[NSBundle mainBundle] pathForResource:#"arabic" ofType:#"txt"];
NSString *arabicString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringENcoding error:&error];
myTextView.text = arabicString;
...
The NSError returns NULL, so there's no error reading in the text file, but the contents of the UITextView is not set. Nothing happens. No error (compilation or runtime), nothing.
Any ideas for a different approach? Or a way to make this work?
Thanks so much.
What you are currently doing sounds reasonable, but for a different approach that I have used (that worked for me), try using a UIWebView. Something along the lines of:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/arabic.html",path]];
NSData *data = [NSData dataWithContentsOfURL:url];
[self.helpWebView loadData:data MIMEType:#"text/html" textEncodingName:#"utf-8" baseURL:baseURL];
(Be interesting to see if using arabic.txt and mime-type of text/plain loads any differently in the web view.)
I am new to IPhone developing.
I have got very strange issue with NSURL which is driving me crazy. I want to instantiate NSURL object to use it in loading image. But instantiation never happens in proper way. It always says my url in invalid.
Basically I use code like below:
NSString *str = #"http://google.com";
NSURL *url = [NSURL URLWithString:str];
but I also have tried a lot of different modifications (with CFStringRef + CFURLCreateStringByAddingPercentEscapes and so on). All of them are not working for me. In debugger url is set to "Invalid".
What it could be? I don't think this is algorithmic or environment issue. It could be about some settings?
Have anyone any idea of what happens?
Your string isn't escaped properly - you should do it like this:
NSString *str = #"http://google.com";
NSString *escStr = [str stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:escStr];
Are you using that actual code or just code "like" it? That method will return nil if you don't feed it a valid string with everything escaped properly.