Problems on downloading html page through NSData - ios

I am currently trying to download the html code of a page for an app I am working on. For some reason when I try to download the code through
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://www.fedex.com/fedextrack/?tracknumbers=%#,#"Tracking#"]]];
I dont get the right HTML code however. When I set up the same load request through a UIWebView have it load then then get the HTML code from stringByEvauluatingJavaScript:#"document.body.innerhtml" I do get the right one.
My question is, how do I make the dataWithContentsOfURL work and get the right code?

Related

Display image on UIImageView using Web server image

I want to display an image on UIImageView calling the Web server image.
Because I want to replace and show it right away.
here's the code
NSURL *imageURL = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
webImage.image = [UIImage imageWithData:imageData];
The problem is...
The image is displayed well at first,
but if I replaced the image on the web server with another image.(The file name is the same, only the file is replaced) It didn't change and still displayed the first image.
Please help me...
The code in the question seems flawed, i.e. you apparently do a network request in the UI thread. Such approach tends to make your application UI unresponsive. However, if we boil down the question to "Why NSData returns outdated data for the same URL?" then the answer is because it has internal caching. The caching logic could controlled either by the the response headers from the server (e.g. Expires) or with NSDataReadingUncached option (to suppress any caching logic):
NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:nil];

How to parse xml into a table veiw

Im new to IOS from Android and am wondering how to parse xml with several tags into a listview. and once that has been parsed have the click action launch a detail webview with the url from the link tag.Here is one of the lists. a rough guideline or a tutorial would be amazing. http://graffiti.hostoi.com/00Graffiti00/lists/arts.xml
it looks like this in android
Check this link for parse xml parse
NSString *url=#"http://www.lancers.jp/work/search/.rss";
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
NSDictionary *dict=[XMLReader dictionaryForXMLData:data error:nil];
Check this link also
Demo code of XML

Get html code and edit then load on to web view

I want to load a web site on a UIWebView which is not under my control and edit/add certain UI changes (Some texts, images, etc) to it. Can I do this within my iOS source code? I can't change the hosted html contents since them not under my control.
If this cannot doable within iOS source code, please advice me the correct way to achieve this.
Load the webpage into an NSString, make any modifications and then put the html into the UIWebView.
NSURL *url = [NSURL URLWithString:#"http://example.com/"];
NSString *page = [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil];
/* Make changes to page here */
[self.webView loadHTMLString:page baseURL:nil];
I'd get the dom with JavaScript, manipulate, then inject back with JavaScript.
See stringByEvaluatingJavaScriptFromString:.
You can write your own full featured, minified JavaScript, then pass into using this method.
// Change body color of any HTML content inside a UIWebView.
NSString *javaScript = #"document.getElementByTagName('body').backgroundColor = '#888';";
[webView stringByEvaluatingJavaScriptFromString:javaScript];

UIWebView get favicons

How can I get favicons from websites in ios UIWebView, like it is possible in android.
Maybe my question are idiotic, but I can't find anything.
You can get favicon of any website using its url string.Lets take an example in which we're trying to get favicon of google :
NSString *myURLString = #"http://www.google.com/s2/favicons?domain=www.stackoverflow.com";
NSURL *myURL=[NSURL URLWithString: myURLString];
NSData *myData=[NSData dataWithContentsOfURL:myURL];
UIImage *myImage=[[UIImage alloc] initWithData:myData];
This method of getting favicon worked nicely for me.
All the best.

Getting Image URLs in a Web Directory

I want to get URLs of all images or lets say "JPEG" files in a web directory (www.abcde.com/images). I just want their URLs in an array.. I couldnt manage that. Could u pls help me with this?
Thanks in advance..
Assuming you have access to an index file you could simply load via NSURL the whole html file and cut out the link lines. This however will not work (or hardly work) when you want to search ("spider or crawl") for links in more complex documents. On iOS i would suggest you use the simple, yet quite powerfull "hpple" framework (https://github.com/topfunky/hpple). It is used to parse html. You can search with it for certain html elements, such as <a href...> constructs.
a sample with hpple could looks like this:
NSURL *url = [NSURL URLWithString:#"whatver.com/images"];
NSData *data = [NSData url];
TFHpple *hppleParser = [TFHpple data];
NSString *images = #"//img"; // grabbs all image tags
NSArray *node = [hppleParser searchWithXPathQuery:images]
find a bigger example at http://www.raywenderlich.com/14172/how-to-parse-html-on-ios
Create a server side script(eg php) which gives you a list of all images in that directory as xml or json. From iOS send a request to that script get the xml or JSON parse it and use the image urls.

Resources