escape character not convert properly when calling webservice in iPhone app...? - iphone

I'm using JSON web service to collect data and display in my UITableView cell. My problem is when I try to call my web service, if the data has characters like & it gives me &.
When I try to store this JSON data in NSDictionary and after that display in UITableView cell it appear as it is.
Eg. if data is Chris & Priscilla originally it retrieves as Chris & Priscilla and same shows in my cell. I want to display it as Chris & Priscilla and not Chris & Priscilla.
It displays properly in browser.
I can't find what I missed.
NSError *error = nil;
NSString *strUrl = #"MY-WEBSERVICE-URL";
NSURL *url = [[NSURL alloc] initWithString:strUrl];
NSString *returnText = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
txt.text = returnText;

Usually HTML unescaping is done adding a category to NSString. There are many examples, i.e. here: http://google-toolbox-for-mac.googlecode.com/svn/trunk/Foundation/GTMNSString+HTML.h.
Particularly, have a look at
(NSString *)gtm_stringByUnescapingFromHTML;

Related

Take contents of UITextField and insert it into an online form, press button, and get result back

I hope that this flow chart adequately describes what I would like to happen.
The webpage in question is here. It is the top textbox and the "Latin to English" button. You can type in "Semper" as an example of how it outputs/works.
My Question: How in the world do you code this kind of thing?!
Thanks for helping on what seems to me like a huge request!
You need to use this url: http://www.archives.nd.edu/cgi-bin/wordz.pl?english= and attach the world.
So, If you called the text field textInput the code is this:
NSString *toTranslate = self.textInput.text;
NSString *url = [NSString stringWithFormat:#"http://www.archives.nd.edu/cgi-bin/wordz.pl?english=%#", toTranslate];
NSString *response = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:UTF8Encoding error:nil];
NSLog(#"Response %#", response);
If you want to have a single word you need to parse the response of the server, but it's a little bit more complicated.

iPhone App: how to get app icon from app id?

I am looking for a way to get the app icon from the app id. Do you know how to do it? Please share the way. Thanks.
e.g
Instagram, where the id I'm looking for is: id389801252
https://itunes.apple.com/jp/app/instagram/id389801252?mt=8
I want to get this image:
(I composed this answer after 2 minutes of googling... It's just the matter of the correct keyword!)
This is possible using an undocumented documented API of the iTunes Store. It might change in the future, but it doesn't seem to have changed in the near past, so here you are...
NSString *idString = #"id389801252";
NSString *numericIDStr = [idString substringFromIndex:2]; // #"389801252"
NSString *urlStr = [NSString stringWithFormat:#"http://itunes.apple.com/lookup?id=%#", numericIDStr];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *json = [NSData dataWithContentsOfURL:url];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:json options:0 error:NULL];
NSArray *results = [dict objectForKey:#"results"];
NSDictionary *result = [results objectAtIndex:0];
NSString *imageUrlStr = [result objectForKey:#"artworkUrl100"]; // or 512, or 60
NSURL *artworkURL = [NSURL URLWithString:imageUrlStr];
NSData *imageData = [NSData dataWithContentsOfURL:artworkURL];
UIImage *artworkImage = [UIImage imageWithData:imageData];
Note that this performs two synchronous round-trips using the NSURL API, so you better wrap this in a backgorund thread for maximal user experience. Feed this program an ID string (idString in the code above) and in the end, artworkImage will contain a UIImage with the desired image.
Just for reference, you can use the app's bundle id too:
http://itunes.apple.com/lookup?bundleId=com.burbn.instagram
Not sure if this is at all relevant anymore, but Apple provides an iTunes Link Maker tool. If you use this tool to find your app, you'll also see where it shows an App Icon section. Click embed and grab the img link from there. One thing to note, I did end up playing with the url a bit to find the right size and format I needed (for instance you can get a jpg render instead of png or select an arbitrary size like 128x128)

iphone: how to get text from web site and show it on application

i have web site that show a text and i update this text every day , i want to show this text on iphone application , how can i get this text from web site from application ?
what should i do ?
thanks
1-> you require to connect with your web server thought HTTP connection.
2-> Make the request to server.
3-> Parse server response that may contain your "Text".
For technical assistance Read below.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
I don't recommend this as the best way to obtain a string from your own web server.
This should point you in the right direction, don't expect it to compile cleanly.
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
/* set headers, etc. on request if needed */
[request setURL:[NSURL URLWithString:#"http://example.com/whatever"]];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
NSString *html = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSScanner *scanner = [NSScanner scannerWithString:html];
NSString *token = nil;
[scanner scanUpToString:#"<h1>" intoString:NULL];
[scanner scanUpToString:#"</h1>" intoString:&token];
This will capture text from first h1 tag.
The simplest way is to create a REST API. That might sound tough but it's really easy. On the server side, create a new page which holds only the raw text. Usually it's best to keep it there in JSON/XML format, but a simple text will also work. Now from the iPhone, just contact that address and the response data will contain the text. Parsing an existing page is not something I recommend, because changing that page in the future might result in the app not working anymore.
This is a answer quite late but I think it still might help in your future. You can go into parsing the website and that is the right way to do it but I will show you how to do it a different way, this can also be used to read xml, html, .com, anything and also, .rss so it can read RSS Feeds.
Here :
This can get your first paragraph, if you request I will show you how to get the second paragraph and so on.
//This is your URL
NSURL *URL = [NSURL URLWithString:#"URL HERE"];
//This is the data your pulling (dont change)
NSData *data = [NSData dataWithContentsOfURL:URL];
// Assuming data is in UTF8. (dont change)
NSString *string = [NSString stringWithUTF8String:[data bytes]];
//Your textView your not done.
description.text = string;
//Do this with your textview
NSString *webStringz = description.text;
// Leave this
NSString *mastaString;
mastaString = webStringz;
{
NSString *webString2222 = mastaString;
NSScanner *stringScanner2222 = [NSScanner scannerWithString:webString2222];
NSString *content2222 = [[NSString alloc] init];
//Change <p> to suit your need like <description> or <h1>
[stringScanner2222 scanUpToString:#"<p>" intoString:Nil];
[stringScanner2222 scanUpToString:#"." intoString:&content2222];
NSString *filteredTitle = [content2222 stringByReplacingOccurrencesOfString:#"<p>" withString:#""];
description.text = filteredTitle;
}
Title ? Same deal change the <p> to a <title> in RSS <description> and <title>.
Image ? Same deal change the <p> to what ever your RSS or website uses to get a image to find
But remember for both of them when you change the` you see the link which says stringByReplacingOccurences of you have to change that as well.
out then you have to delete this and make your code like this :
/This is your URL
NSURL *URL = [NSURL URLWithString:#"URL HERE"];
//This is the data your pulling (dont change)
NSData *data = [NSData dataWithContentsOfURL:URL];
// Assuming data is in UTF8. (dont change)
NSString *string = [NSString stringWithUTF8String:[data bytes]];
//Your textView your not done.
description.text = string;
NLog(#"%#", string)
//Do this with your textview
NSString *webStringz = description.text;
// Leave this
NSString *mastaString;
mastaString = webStringz;
Now check your log it shows your whole website html or rss code then you scroll and read it and find your image link and check the code before it and change the String Scanner to your needs which is quite awesome and you have to change the stringByReplacingOccurences of.
Like I said images are a bit tricky when you do it with this method but XML Parsing is a lot easier ONCE you learn it , lol. If you request I will show you how to do it.
Make sure :
If you want me to show you how to do it in XML just comment.
If you want me to show you how to find the second paragraph or image or title or something just comment.
IF YOU NEED ANYTHING JUST COMMENT.
Bye have fun with code I provided, anything wrong JUST COMMENT! !!!!
:D

(iPhone) Reading from a web file - NSMutableArray

Right now I have an NSArray whose items are just written in the implementation file.
I'd like to know how to make it so I can store all the items on a web (text) file. Example:
http://mysite.com/files/objects.txt
On that document:
Object 0 goes here
Object 1 goes here
Object 2 goes here
Object 3 goes here
etc. All separated by a line break.
How would I be able to do this? And when I update that text file, I'd like the objects to update as well. I'd do this via an NSMutable array, correct?
Thanks in advance.
- (NSArray*)getObjectsFromWeb {
NSURL *url = [NSURL URLWithString:#"http://mysite.com/files/objects.txt"];
NSStringEncoding usedEncoding;
NSError *error;
NSString *data = [NSString stringWithContentsOfURL:url
usedEncoding:&usedEncoding
error:&error];
return [data componentsSeparatedByString:#"\n"];
}
after data is assigned here, usedEncoding (and possibly error) will have a value that you can inspect and tak action on if you like.

Problem with Tiny URL

I am developing Twitter API to my application. In my app, I want to display the tiny url with parsed xml link in UITexField as dynamically. statically, I could able to display tiny url in UITextField, but dynamically I am not able to display the tiny url. Please help me!
Code: statically(Working fine),
NSURL *url = [NSURL URLWithString"http://tinyurl.com/api-create.php?
url=https://gifts.development.xxxxx.edu/xxxx/Welcome.aspx?appealcode=23430"];
NSString *link = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
Dynamically,
NSString * tiny = [NSString stringWithFormat:#"http://tinyurl.com/api-create.php? url=%#", shtUrl];//shtUrl is parsed string
NSURL *url = [NSURL URLWithString:tiny];
NSString *link = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
In above, dynamically app running without warning and error, but url not displayed and when I am checking debugger, url and link both are show nil value. But shtUrl show whole url value as properly.
I tried with all NSURL class and instance methods and String methods also.
In this line of your code:
NSString * tiny = [NSString stringWithFormat:#"http://tinyurl.com/api-create.php? url=%#", shtUrl];
there is a space after the 'api-create.php?'. This will result in a space in the formated string you are creating and will probably result in URLWithString: being unable to parse the url and returning nil.
Remove the extra space (assuming that it is really there and not just a cut-n-paste error) and see if that fixes the problem.
It's also possible that the shtUrl that you are building a tinyurl for contains special characters that would need to be urlencoded (i.e. percent escaped.) Try adding this:
NSString * encodedShtUrl = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)shtUrl,
NULL,
(CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",
kCFStringEncodingUTF8 );
to encode the shtUrl, then use the encodedShtUrl when creating tiny:
NSString * tiny = [NSString stringWithFormat:#"http://tinyurl.com/api-create.php? url=%#", encodedShtUrl];
See http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/ for more about the escaping.