iOS - handling redirects in UIWebView - ios

I have this bool function in web view that checks for the keyword "youtube" and if there is a keyword "youtube" in a link it will open in app web view instead of safari.This works correctly, but there is a problem that I ran into. If the youtube link is shortened by https://bitly.com/ it will open in safari. Is there a way to prevent this. I still need the https://bitly.com/ to open in safari for every other keyword except youtube.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
if ([[inRequest.URL absoluteString] rangeOfString:#"youtube"].location==NSNotFound){
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
return YES;
}

You can use knowurl service to expand original URL from tiny shortened link
- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
#try
{
BOOL _returnVal = YES;
//convert your bitly url to KowUrl if its contains `bit.ly`
if ( inType == UIWebViewNavigationTypeLinkClicked )
{
if ([[inRequest.URL absoluteString] rangeOfString:#"youtube"].location==NSNotFound)
{
[[UIApplication sharedApplication] openURL:[inRequest URL]];
_returnVal = NO;
}
}
}
#catch (NSException *exception)
{
NSLog(#"%s\n exception: Name- %# Reason->%#", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}
#finally
{
return loadedImages;
}
}

Related

SFSafariViewController does not reopen a webview inline URL link

I have a Webview with inline URL links which are opened with SFSafariViewController, as shown below:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
if ([SFSafariViewController class] != nil) {
NSString *inR = [[inRequest URL] absoluteString];
NSURL *inReq = [NSURL URLWithString:inR];
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:inReq entersReaderIfAvailable:YES];
safariVC.delegate = self;
[self presentViewController:safariVC animated:YES completion:nil];
} else {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
return YES;
}
#pragma mark - SFSafariViewController delegate methods
-(void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
// Load finished
}
-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
// Done button pressed
NSLog(#"DONE PRESSED!!!");
}
When I press the DONE button correctly returns to my Webview. The problem is that if I will press again on the same inline link, it does not open with SFSafariViewController but in Webview which is not what i desire. I tried to force Webview reload in safariViewControllerDidFinish but without success.
Could you please help? Thanks!
The code corrected as below (following proposal of beyowulf) and now is working OK:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
if ([SFSafariViewController class] != nil) {
NSString *inR = [[inRequest URL] absoluteString];
NSURL *inReq = [NSURL URLWithString:inR];
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:inReq entersReaderIfAvailable:YES];
safariVC.delegate = self;
[self presentViewController:safariVC animated:YES completion:nil];
return NO;
} else {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
} else {
return YES;
}
}
#pragma mark - SFSafariViewController delegate methods
-(void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
// Load finished
}
-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
// Done button pressed
NSLog(#"DONE PRESSED!!!");
}

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
}
}

Disable interaction on UIWebView, only link clickable?

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];

Webview excluding php files on request in Phonegap

Ok so I am working on a cordova app and I am using this bit of code below to prevent external urls from opening in the web view. But I have exceptions in there to allow the iframe from google to load. My problem is that my form script is not handled properly now, so how would I add a exception to prevent php files from being sent to the external app.
I would also like the mail app to handle mailto: urls.
Thanks for the help
- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
NSString *host = [request.URL host];
if(host != NULL || host != nil){
if ([host rangeOfString:#"google.com"].location != NSNotFound) {
return YES;
}else{
if ([[url scheme] isEqualToString:#"https"] || [[url scheme] isEqualToString:#"https"] || [[url scheme] isEqualToString:#"mailto:"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
}
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}

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.

Resources