Disable interaction on UIWebView, only link clickable? - ios

i am adding UIWebview in tableview for showing text and link , but not able to scroll tableview, Any way to disable user interaction in webview only click event enable.
Webview have static content not load from web
NSString * htmlString = [NSString stringWithFormat:#"<html><head><script> document.ontouchmove = function(event) { if (document.body.scrollHeight == document.body.clientHeight) event.preventDefault(); } </script><style type='text/css'>* { margin:0; padding:0;} p { color:white; font-family:HelveticaNeue-CondensedBold; font-size:24px;} a { color:#63B604; text-decoration:underline; }</style></head><body><p>%#</p></body></html>", #"search in http://google.com"];
Thanks

just disable scrolling or using this methods
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Disable user selection
[webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.style.webkitUserSelect='none';"];
// Disable callout
[webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.style.webkitTouchCallout='none';"];
}
and open link as follow using delagate methods
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
NSURL *url = [inRequest URL];
if ([[url absoluteString] rangeOfString:#"gmail"].location == NSNotFound) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
return YES;
}

i got a answer from the site
http://nickharris.wordpress.com/2010/06/17/fast-uitableviewcell-with-a-uiwebview/

Try to disable scrolling in your WebView
[myWebView.scrollView setScrollEnabled:NO];
[myWebView.scrollView setBounces:NO];

Related

How to load a link in safari from a pdf being displayed in a UIWebView ios

I am using the Page View Application to show a series of PDF's. The UIWebView is displaying the PDF's fine, however whenever there is a link that is embedded into the PDF and the user clicks it, it opens the link inside of the UIWebView. I want it to open said link in safari.
NSString *path = [[NSBundle mainBundle] pathForResource:page ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.webView loadRequest:request];
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
As You can see i have tried to insert the code to launch it but the BOOL never gets run when stepped through.
Use This:-
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked || inType ==UIWebViewNavigationTypeOther) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
UIWebViewNavigationTypeLinkClicked happens if the user taps a style link, if the change is done from within Javascript (onclick event for example) UIWebViewNavigationTypeOther is used.
Furthermore UIWebViewNavigationTypeOther is used if you first load a page or you'll be redirected by a meta refresh or something

How to Load Only .html page in WebView and All www Page load in Safari in iOS?

i am newbie in iOS Development I load my HTML Data Into WebView But Some time it Contain only href link as .html link and some time website link like as www.google.co.in so i want to load only html data in to Webview and any website are load in to Safari for that i write a code like as
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *link = [[request URL] relativeString];
if ([link isEqualToString:#"module1learningobjectives.html"])
{
return NO;
}
else
{
[[UIApplication sharedApplication] openURL:[request URL]];
return YES;
}
return YES;
}
then it load .html file in web view but site are open in safari and Webview both i want only site was open in safari please give me solution for that.
I'd do something like this:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog("URL is %#, and has an extension of %#", request.URL, [request.URL pathExtension]);
if ([[request.URL pathExtension] isEqualToString:#".html"])
return YES;
return NO;
}
Is this what you are asking?
If you want it to use Safari for non files and your own WebView for files, then try this:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ( ! ([request.URL isFileURL]) ) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
Just modify your code to below code.
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
NSString *strLink=request.URL.absoluteString;
if([strLink rangeOfString:#".html"].location!=NSNotFound)
{
[[UIApplication sharedApplication]openURL:[request URL]];
return NO;
}
else
{
return TRUE;
}
return NO;
}
return YES;
}
Above code check if the link which is going to open has .html extension or not? and it works accordingly.
One more thing in your question you haven't mentioned that the href are link of local pages or external links.
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString * temp = [NSString stringWithFormat:#"%#",url];
if ([temp rangeOfString:#"www"].location != NSNotFound)
{
// show alert view for go to safari
// i.e
[[UIApplication sharedApplication] openURL:url];
}
else
{
// your regular html page pushed
}
}

How can I link a local pdf file on html file - the html file is showing on UIWebView

I am showing a local html file on my UiWebView using following code.
NSString *path = [[NSBundle mainBundle] pathForResource:#"test.html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:bundlePath];
[_webView loadHTMLString:htmlString baseURL:baseURL];
[_webView setBackgroundColor:[UIColor clearColor]];
App is showing the html correctly. In the test.html there is a link to local pdf file hello.pdf. This pdf is added to the project. But when I click on the link nothing happens. I want to load pdf on my web view when user clicks on the link.
I want to use the UIWebView delegate to send requests for internet hyperlinks (e.g. http://www.google.com) to the safari app.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
NSLog(#"%#",inRequest.URL.absoluteString);
if([inRequest.URL.absoluteString rangeOfString:#"hello.pdf"].location == NSNotFound)
{
if ( inType == UIWebViewNavigationTypeLinkClicked )
{
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
else
{
return NO;
}
}
You should have stated more clearly that you wanted internet hyperlinks to load in Safari and local hyperlinks to load in your UIWebView.
I've just tested this and it does what you want:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
if (inType == UIWebViewNavigationTypeLinkClicked) {
if ([inRequest.URL.absoluteString rangeOfString:#"http://"].location == NSNotFound) {
return YES;
}
else {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
else {
return YES;
}
}
This works by only loading links in your UIWebView that don't contain the hyper text type protocol prefix (http://).
Tip: Make sure you've set the delegate for the UIWebView in the viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
webView.delegate = self;
}

get urls of 2 uiwebview's and put to their own adress bar

In my app I have 2 UIWebView's and 2 Adress Bar tat called WebView and WebView2, webAdress and webAdress2. I need to get url from WebView and put it to webAdress, and from WebView2 and put it to webAdress2.
When I use this code, the URL updates appears only in first webAdress, url from WebView2 apperas in first webAdress too. Moreover, all pages from WebView2 starts to be load in WebView.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:#"http"]) {
[webAdress setText:[URL absoluteString]];
[self gotoAddress:nil];
}
return NO;
}
return YES;
}
- (BOOL)webView2:(UIWebView*)webView2 shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:#"http"]) {
[webAdress2 setText:[URL absoluteString]];
[self gotoAddress2:nil];
}
return NO;
}
return YES;
}
I guess you are in need of only one delegate method. Check which webview has triggered this delegate method and perform actions depend on this:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:#"http"]) {
if (webView == webView1)
[webAdress setText:[URL absoluteString]];
if (webView == webView2)
[webAdress2 setText:[URL absoluteString]];
[self gotoAddress2:nil];
}
return NO;
}
return YES;
}
Just set for all web views delegate as self, and all you can handle all actions in this method.

Open links in Safari instead of UIWebVIew?

I have an app with a UIWebView inside a UIViewController. I load HTML from a web service as a string like this:
self.webView loadHTMLString:_string baseURL:nil
Is it possible for the HTML links in this string to be opened in the browser and not in the UIWebView in my app? How can I do this?
I have tried this in the UIViewController that "hosts" the UIWebVIew:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
It doesn't seem to be working....
Any ideas?
Have you set the delegate of the UIWebView to your UIViewController? There's nothing obviously wrong with your code as far as I can see, so it's likely to be something small like that.
Add this in class..
#interface yourViewController : UIViewController
<UIWebViewDelegate>
Add this in View did load
- (void)viewDidLoad
{
[description loadHTMLString:string baseURL:nil];
description.delegate = self;
}
Add this in your .m file
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
Note:
UIWebView *description;
#synthesize description;
Then It will work perfectly the way you deserve..!! :)
Set the delegate of the UIWebView to your UIViewController after that use this UIWebview method and check the condition, for example now webview current url is google.com, if suppose you clicked on gmail the url contains the string with gmail. Use the below method it opened a safari browser and automatically loaded that url.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
NSURL *url = [inRequest URL];
if ([[url absoluteString] rangeOfString:#"gmail"].location == NSNotFound) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
return YES;
}
If you already setup properly the UIWebViewDelegate, simply doing
self.webView loadHTMLString:_string baseURL:nil
self.webView.delegate = self;
should work
Add this line (
self.webview.delegate = self;
)
For Example in viewController.m
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[self.webview loadHTMLString:htmlString baseURL:nil];
self.webview.delegate = self;
Apple introduced a Safari View Controller on iOS 9. Safari View Controller brings all of the features user expect from Safari over to your app without ever leaving it.
First we need to import Safari Services
#import <SafariServices/SafariServices.h>
For Objective C:-
NSString *yourUrl = #"http://yourURL";
SFSafariViewController *svc= [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString: yourUrl]];
[self presentViewController:svc animated:YES completion:nil];
For Swift:-
let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)
self.presentViewController(svc, animated: true, completion: nil)
Yes, in your hyperlink tag add
target="blank"
And one question mark will be fine, thank you

Resources