NSURL not instantiating properly - iphone

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.

Related

Using Local Resource and Relative Link in the same UIWebView

I have a problem with a WebView. I'm loading an Html String into a web view. As base URL I need to use my site URL (because there are some relative link inside the html and I cannot modify it).
The problem is that I also need to use some images and css that are stored inside the Application bundle.
So I'm creating a reference to the local resource using this code (the result string is used as src in html file):
NSString *cssUrl = [NSString stringWithString:#"file:"];
cssUrl = [cssUrl stringByAppendingString:[[NSBundle mainBundle] resourcePath]];
cssUrl = [cssUrl stringByAppendingString:#"/story.css"];
Then I've also added:
cssUrl = [cssUrl stringByReplacingOccurrencesOfString:#"/" withString:#"//"];
cssUrl = [cssUrl stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
Any idea how to solve the problem and how to create a reference to a file that can be used inside a web view?
Thanks
Francesco
First, import a directory, e.g. one called web, into your project where all the files reside. Make sure you select "Create Group References For Any Folders" when Xcode asks you.
Now you can load your initial string like this after putting it into a file inside web, say index.html:
NSString* path = [[NSBundle mainBundle] pathForResource:
#"index" ofType:#"html" inDirectory:#"web"];
if (path)
[self.webView loadRequest:
[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
Now all relative source URLs in your string will work as expected, including <img>, <a> etc.
How about this?
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:#"story" ofType:#"css"];
NSURL *url = [NSURL fileURLWithPath:resourcePath];
NSString *theMagicalURLString = [url absoluteString];
It seems that what I was trying to do it's not possible. To use the bundled resource you have to set the base URL of your web view to the resource path.
Thanks to everyone for your help
Francesco

hpple html parse iphone sdk help?

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.

Displaying localized text unavailable at compile time on iPhone

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.)

How to get short URL inside my iPhone app?

I need to get a short URL from a long one (that I made inside my app). I think that I already read about this, but I couldn't find it anymore here in SO.
Anyone has a piece of code or directions to point this out?
Best Regards
I usually use this one, from brandontreb of iCodeBlog
NSString *apiEndpoint = [NSString stringWithFormat:#"*API URL=*%#",link];
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint]
encoding:NSASCIIStringEncoding
error:nil];

iPhone - dataWithContentsOfURL: does not work for some URLs

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.