I am looking to find out if a web page has changed, I was going to use the content length of the web page but have not seen a way to do so. Any ideas? Or can anyone think of another way to check periodically if a web page has changed?
If you mean with changed wether navigation has occured, you could use a custom UIWebViewDelegate and set a flag when e.g. -(void)webViewDidFinishLoad: occured.
You might want to check UIWebViews property request to check wether the URL actually differs.
If you want to check wether the content has changed you could retrieve it e.g. like this:
NSString* script = #"document.body.innerHTML";
NSString* content = [webView stringByEvaluatingJavaScriptFromString:script];
Or retrieve the length e.g. like this:
NSString* script = #"document.body.innerHTML.length";
int length = [[webView stringByEvaluatingJavaScriptFromString:script] integerValue];
I am assuming you want to know if the web page you are already showing is different than the web page you would get if you hit the server again.
You cannot do much with the documented interfaces in UIWebView.
You can use an NSURLConnection to ask for just the headers of a web page and not the actual content. Once you get the headers, look at fields like "Last-Modified" and "Content-Length" to see if it has changed. You can also look into the 304 not modified response code.
Set the HTTPMethod of a new NSURLRequest to HEAD instead of GET to not get the body.
Set your class as the delegate of an NSURLConnection created with that request.
Handle the following delegate callback and examine headers in the response.
-(void) connection:(NSURLConnection *)inConnection didReceiveResponse:(NSURLResponse *)inResponse;
For more information look here:
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
Related
I am very new to iOS and I've just begun reading about HTTP requests and POST and GET methods. Let's say, for example, I want to have the user input a string, and then send that data to a website, (for this example, say www.rhymezone.com), search with that string, and get the results of that search within my application. Is this done with an HTTP post method? Or what? Any help / examples would be greatly appreciated. Also, if there are tutorials for this stuff, that would be appreciated as well.
For sake of example, here is what I've tried:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.rhymezone.com/r/rhyme.cgi?Word=test&typeofrhyme=perfect&org1=syl&org2=l&org3=y"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSString *dataAsString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"data: %#",dataAsString);
}
This outputs the entire source of the website (searching for rhymes of the word test). While I can certainly write a method to go through the source of the website and extract the words it returns, I feel like this is not correct. My way of getting rhymes of different words is simply to change the URL here, so where it says 'test' I change it to whatever the user inputs.
Thanks
Look into AFNetworking and RestKit.
It's easiest if you're calling a public API that uses JSON/XML, and then use a built in parser or a parser library to extract the data you want.
Simply downloading the contents of a URL is an HTTP GET request, such as going to a website.
This link talks a bit more about the difference between GET and POST.
When do you use POST and when do you use GET?
If I understand correctly what you are trying to do, I fear that the only option for you is sending the HTTP request (GET or POST according to what the website expects, just like you are doing) and then parse the result to filter all the information that is not relevant.
An alternative approach would be possible if you were using a website offering a REST API, or a JSON API so that you send the query and you get back just the information you need (in a specific format).
So, it depends strongly on the website you are using, but for the generic case, the only option you have is parsing.
(Or, you could display the full content of the page through UIWebView. This would not require explicitly setting up a connection, but I am not sure it is what you are trying to do.)
You are looking for a way to communicate with your website from your iOS application. The common approach is to get the string entered by the user, encode and send it as http request to a sort of script (webservice). This script will do all the stuff you want (search with this string). Then re-send the result to the client (your iOS app) as a http response which will be parsed in your iOS app(with a JSON parser for instance).
There is good resources around that, as an example, you may read this: http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service
In my app, when I send request to server either I will get url or pdf or image in response and based on that I will display the result (url->webview, image->imageview etc).But I do not know how to identify whether the response is url or pdf or image?
Please anyone know how I can achieve this?
Thanks in advance!
I assume you do use NSURLConnection to download your file. So, implement this delegate method ...
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
... cast response to NSHTTPURLResponse and get Content-Type value from allHeaderFields.
But it depends what do you really use to download your files. If you do use another class/framework/..., you get an idea.
I would like to suggest you to use UIWebview for both case either it is PDF or image url from server.
[imgwebVw loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strImgURL]]];
If you really must do this you can read the file headers but I wouldn't suggest it. here is a category I wrote to get the size of a remote image, you can adapt it to detect if the file is indeed an image or not. And you can do similar stream parsing to search for the PDF Header: %PDF-1.0 etc. Otherwise you would assume it is a url.
In my application, I have a webview which loads up a page that contains both normal links and custom URL schemes, such as myapp://id=1234.
Right now, I am trying to trap the request within this function:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType{
NSLog(#"NSURLRequest = %#", [[request URL] absoluteString]);
}
Printing out the url as above gives me this:
http://localhost:8888/myapp://id=1234
Which is my localhost setup on MAMP with the custom URL appended to the URL.
I had hoped that I could use [request scheme] directly, but in this case now, it simply returns "http". Is there a way that I can handle custom URL schemes from within my own app?
At this point, I would like to be able to do the following:
User is on the webview
User taps the custom url link ( myapp://id=1234 )
My app handles the custom url and directs the user to another page ( http://www.someotherpage.com/?id=1234 )
Thank you!
When a URL has a double-slash following the colon, like yours does, the next part is supposed to look like userinfo#hostname:port (both userinfo# and :port can be omitted). Since id=1234 does not look like a hostname, the URL parser is not recognizing your URL as an absolute URL. So the URL parser treats it as a relative URL.
Try changing your URL syntax to either myapp:id=1234 or myapp:///id=1234.
I guess you shall declare any custom URL scheme as handled by your application.
Check reference
I'm looking for a solution for reading the http status code with a UIWebView.
I have found this page on the topic How do I get the last HTTP Status Code from a UIWebView? but i cannot use AsiHttpRequest in my case.
Si I was wondering if somebody have found a solution since 2009, and if something similar to NSLog(#"Status code = %#",[webView stringByEvaluatingJavaScriptFromString:#"document.status"]);
could possibly work.
Thanks,
I don't think you can get it from the UIWebView, but I think it would work to have the result of an HTTP request put into an NSString, then parse the status code out of the header part of that string, thing feed that string to a UIWebView.
See the NSURL Class Reference and the URL Loading Programming Guide.
A possible alternative would be to implement an HTTP proxy directly inside your App, then feed a localhost URL to UIWebView. Your proxy would just make an HTTP connection with the web server and sit passively by while UIWebView drives the HTTP protocol. You then snoop on the incoming data before passing it on to UIWebView from your proxy. That would avoid the need to buffer the whole page in an NSString before feeding it to your UIWebView.
I wanted to know if it is possible and if so how to know the url you are accessing via objective-c takes you to the correct webpage. To elaborate further:
If a user is using an app that connects to a webpage to get it's data, and if this user is say at an airport. The internet connection at airport in many cases will redirect u to their webpage (regardless of what url u maybe type) where u have to either pay or something to get any further internet access. Thus if a app needing data from a webpage is used without user's knowledge of this limited access to internet, i want to display a popup message that informs the user saying something that the webpage it tried to access was required to a different webpage thus cannot access data. As oppose to getting false data and the user thinking the app is buggy.
Is there a way to do this. Thanks in advance for all of your help.
If you are using NSURLConnection you can implement the NSURLConnectionDelegate method
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
which is called if it is determined that the request must redirect to a new location. The delegate can the choose to allow the redirect, modify the destination, or deny the redirect.
You can read more about NSURLConnectionDelegate and its methods here