How to get short URL inside my iPhone app? - iphone

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

Related

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.

Loading a local HTML file in Navigation based application

I am new to iOS development.
I have been trying to find an answer to my question but I could not, I have found some related answers but I could not fix the problem.
My problem is, I have made an application which has a table, and for every row there should be a URL which loads a UIWebView, and should be loaded when you click on the row, but it loads an online URL such as #"http://www.google.com". But I want it to load a local HTML file.
I have used this for the URL:
azkarDetailViewController.detailURL=
[[NSURL alloc] initWithString:
[[[azkarData objectAtIndex:indexPath.section] objectAtIndex:
indexPath.row] objectForKey:#"url"]];
And for every row in the table I use:
[AZKAR addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:#"THE NAME OF THE ROW",#"name",#"DayandNight.png",#"picture",#"http://www.google.com",#"url",nil]];
So I want the application to load a local file from the Resources (say for example the name of the HTML file is "index.html").
Can you please help me solving this problem ?
Thank you very much..
When you compile your app, the output is a “bundle” of files and directories. You can actually access the bundle through the NSBundle class; the static method +mainBundle will return a pointer to an instance of NSBundle that represents your app's main bundle.
You can then use the -URLForResource:withExtension: and -URLForResource:withExtension:subdirectory methods of NSBundle (these require iOS 4.0 or higher—there are older equivalents as well). If the HTML file is stored in your main bundle directory (that will be the case unless you created a an actual directory—different from a group—in your bundle), you can find its URL this way:
NSString *myDocumentName = #"index.html";
NSURL *documentURL = [[NSBundle mainBundle] URLForResource:myDocumentName extension:Nil];
Note that you do not have to specify the extension separately if it's already in the filename.
Note: my explanation is a little oversimplified (I'm assuming you don't need to deal with localizations, otherwise there are other issues you should be aware of explained in the docs).
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 40000
// code for iOS below 4.0
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"WindowsTest" ofType:#"html"]];
#else
// code for iOS 4.0 ++
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"WindowsTest" withExtension:#"html"];
#endif

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

Load static google map into UIImageView?

I'm trying to show a small static google map in my app. I'm wondering if it's possible to do this using UIImageView? It doesn't seem to be working for me but maybe I'm doing something wrong.
This is what I have (I stripped the URL so that it's easier to read):
NSURL *mapurl = [[NSURL alloc] initWithString:#"http://maps.google.com/maps/api/staticmap?[...]"];
NSData *mapdata = [[NSData alloc] initWithContentsOfURL:mapurl];
UIImage *uimap = [[UIImage alloc] initWithData:mapdata];
self.map.image = uimap; // map is my UIImageView
[mapurl release];
[mapdata release];
[uimap release];
I know my UIImageView is setup correctly because if I replace the URL with one of an image then it loads fine. Also I know the google maps URL is correct because if I load it straight into a browser it shows up fine.
I'm starting to think that this might not be possible. If anyone has any ideas or alternatives please share.
Thanks.
Well I ended up finding a solution for this so I'll post it in case someone else runs into the same issue.
First I changed the way I load the image to using NSURLConnection. I don't think this particular change makes a difference but this is what allowed me to find the problem. When trying to load the URL I got a "bad url" error. So I did some searching on that and turns out there are some invalid characters in the google static map URL that must be escaped.
So basically just call this on the url and you should be good to go:
// The URL you want to load (snipped for readability)
NSString *mapurl = #"http://maps.google.com/maps/api/staticmap?[...]";
// Escape the string
mapurl = [mapurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
I haven't tested this solution with the code I have in my original post but I did test it with my new NSURLConnection implementation and it works perfectly. I assume it'll work fine with the code above as well since it was just a matter of escaping the string.

NSURL not instantiating properly

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.