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
Related
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.
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
I have a UIWebView control loading a URL that have AJAX on HTML. My concern is to stop AJAX request on the page. I can stop all further navigation by using delegate method as:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest: (NSURLRequest*)req navigationType:(UIWebViewNavigationType)navigationType {
Or I can use the simple method [webView stopLoading], but non of these able to stop internal AJAX request running on browser.
Please suggest if is there any way where we can stop AJAX
Ajax URLs are not passed to shouldStartLoadWithRequest, only page and iframe loads. To capture any other sort of network traffic you can use NSURLCache which is basically a cache layer between UIWebView and network. It can also capture requests based on some rules you defined and return an error or empty response for it as if that error was in the cache.
Here is a sample to replace some url content:
http://cocoawithlove.com/2010/09/substituting-local-data-for-remote.html
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
I have a Data: URL (see: http://en.wikipedia.org/wiki/Data_URI_scheme) (as a NSString) and I want to open it in Safari. How would you accomplish this (I tried openURL:.)
Example:
data:text/html;base64,(Some Base64 Encoded Data Here)
In iPhone OS 2.2.1 and 5.0.1, in both the simulator and on a device, opening a data: url works perfectly in a UIWebView but using openURL does precisely nothing.
And Safari will gladly, and properly, render such an URL if you are willing to type one into the navigation bar, so this is clearly a problem with sharedApplication openURL, not with Safari.
If the base64 string is short enough (less than 2K, probably) you could wrap it as a query parameter to an http URL that simply returns a redirect to the data url. Then you could use openURL to open the http URL. Yes, this means bouncing through some server, but it would work.
Alternatively, since Safari obviously hasn't done it, you could tell the iPhone that your app is the handler for the data: scheme and take responsibility for rendering the content in a UIWebView. This seems likely to fail in the future, though. :-)
Where is the data URL coming from in the first place? Perhaps you could construct a web page whose contents are nothing more than <iframe src="<the data url>"/> and again, use openURL on that URL.
This should do it:
NSURL *yourURL = [[NSURL alloc] initWithString:yourStr];
[[UIApplication sharedApplication] openURL:yourURL];
[yourURL release];
assuming "yourStr" is an NString with the URL where your data is located.