How to apply local styles / JS to UIWebview after loading a URL - iphone

I am loading a web page in my iPhone app. However the page is not mobile optimized. So, I would like to customize some elements from the page like remove sidebar etc.
For doing this, I guess, some CSS / JS need to be injected into the page to modify the page. Can this be done ? If yes, how to?
Any help appreciated.

For others having same question:
It can be done by using stringByEvaluatingJavaScriptFromString: method of UIWebView (upon webViewDidFinishLoad)
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('topBarBody').remove();"];
}

Related

UIWebView crashed when link with target="_blank" selected

I am using UIWebView to display web pages in my application. but when hyperlink with target="_blank" is get selected application terminate automatically.
is there any way to avoid this crash.
Thanx in advance.
After researching, I found that if we set target of an a tag to _self we can avoid the crash easily. But still there is on restriction to this, as we can apply the Javascript in this method:
- (void)webViewDidFinishLoad:(UIWebView *)webView
So before, selecting this link before loading the page crashed the application as no Javascript is applied.
If anybody knows solution, guide to me.
Thanks

getting image from UIWebView

I am loading a web page inside a UIWebView, clearly a web page has many images in it. I am loading it to the UIWebView via
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
the issue is that this doesn't trigger the NSURLCached which I have subclassed. I basically want to cache the data I get when the UIWebView loads the <img src = ""> tag so I can use it later on for faster loading or such. Is there any way to do this?
Yes, this should actually work. Did you set your cache globally via NSURLCache's
+ (void)setSharedURLCache:(NSURLCache *)cache
before loading the HTML?
Maybe you would like to have a look at Matt Gallagher's NSURLCache Tutorial, which shows an example of a subclassed NSURLCache which does pretty much what you're asking for (serving stored images for certain requests).

Easiest Way To Add In-App Internet Browser?

I'm looking at how to make a very simple browser in my app. Only need reload, back, forward, etc.
Does anyone have any simple code they can add here?
I have a tableviewcell that when clicked will launch this view.
Add a UIWebView.
You can use
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]]`
to open a website, -reload to reload, – goBack and -goForward to go back or forward.
Check out the UIWebView API, which is basically a WebKit rendering widget - the one that also powers iPhone's browser app.

Disable the Alert Box in UIWebView

I am loading an External web page in the UIWebView. When the web page loads, there is an Alert Box ( with OK and Cancel buttons) with some Suggestion/ Info about the web page. I want to block this Alert Box when the web page loads in the UIWebView component in my iphone app. How can I implement in my code?
Thanks for your reply. I am not in-charge for the external web page(but i could ask the concern web page owner to do the changes with respect to following requirement). The requirement is that the Alert Box (used to tell about my iphone app) could be shown when we view the web page in iPhone Safari browswer , but not in UIWebView in which I am using the same web page) of my iphone app. I am using the same web page url both in iPhone Safari and in my iPhone app with UIWebView. So requirement is show the Alert box when we view the web page in iPHone Safari and don't show the Alert box when we view the same web page in iPhone app (within in UIWebView). Hope I have clearly explained my requirement. Please give any solution for this.
using stringByEvaluatingJavaScriptFromString: method you can change anything you want on the page with any javascript you provide, so you can overload or replace the alert function to do what you want
Are you the one 'in charge' of this external web page? If that's the case, you could do something like this:
Make the UIWebView load externalpage.html?noalert instead of externalpage.html.
Then on externalpage.html you can check whether this query string variable exists, and only show the alert() if it doesn't:
if(!document.location.search || document.location.search.substring(1) != 'noalert') {
alert('Boo!');
}
duplicate: UIWebView: Can I disable the javascript alert() inside any web page?
Since a UIWebView translates all Javascript alerts into native UIAlertViews it is fairly simple to block it on the native end. Looking into UIAlertView.h there is only one public method for showing an alert which is conveniently called: - (void)show;.
#interface UIAlertView (Blocker)
#end
#import "UIAlertView+Blocker.h"
#implementation UIAlertView (Blocker)
- (void)show {
return;
}
#end
You can find the answer here: https://stackoverflow.com/a/21698251/2377378

Remove html tags from UIWebView

I am developing an application for iphone which needs to load some description to a UIWebView. But text that loads into the UIWebView shows some html tags like <p></p>.Is there any way to remove these type of tags?
Thanks in advance
I asked a similar question the other day and got some good responses. I imagine one of these could suit your needs.
anyway to delete all <a href=> tags with javascript after the page loads on an iPad?
Hope it is helpful!
This is the best answer and is exactly what you are looking for ...
Write the following script in the webView delegate method. ( UIWebviewdidfinishLoading)
NSString *myText = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.textContent"];