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];
}
Related
I am using PhoneGap 2.3 - Cleaver for iOS.
How can I override shouldStartLoadWithRequest, webViewDidStartLoad, webViewDidFinishLoad function?
If I add "viewController.webView.delegate = self" to viewDidLoad, the functions above can be called but the PhoneGap API cannot be invoked.
Thanks.
MyViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
CDVViewController* viewController = [CDVViewController new];
viewController.view.frame = self.view.bounds;
//viewController.webView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:landingURL]];
[viewController.webView loadRequest:request];
[self.view addSubview:viewController.view];
[self addChildViewController:viewController];
}
MyViewController.h:
#interface MyViewController : UIViewController <UIWebViewDelegate>
#end
You shouldn't override shouldStartLoadWithRequest because there is where they manage the javascript urls that call the native functions.
Or at least copy their code from the CDVViewController into your view controller and add the things you need.
example from phonegap 2.9.1
- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* url = [request URL];
/*
* Execute any commands queued with cordova.exec() on the JS side.
* The part of the URL after gap:// is irrelevant.
*/
if ([[url scheme] isEqualToString:#"gap"]) {
[_commandQueue fetchCommandsFromJs];
return NO;
}
/*
* If a URL is being loaded that's a file/http/https URL, just load it internally
*/
else if ([url isFileURL]) {
return YES;
}
/*
* If we loaded the HTML from a string, we let the app handle it
*/
else if (self.loadFromString == YES) {
self.loadFromString = NO;
return YES;
}
/*
* all tel: scheme urls we let the UIWebview handle it using the default behavior
*/
else if ([[url scheme] isEqualToString:#"tel"]) {
return YES;
}
/*
* all about: scheme urls are not handled
*/
else if ([[url scheme] isEqualToString:#"about"]) {
return NO;
}
/*
* all data: scheme urls are handled
*/
else if ([[url scheme] isEqualToString:#"data"]) {
return YES;
}
/*
* Handle all other types of urls (tel:, sms:), and requests to load a url in the main webview.
*/
else {
if ([self.whitelist schemeIsAllowed:[url scheme]]) {
return [self.whitelist URLIsAllowed:url];
} else {
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
} else { // handle any custom schemes to plugins
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
}
}
return NO;
}
return YES;
}
the if ([[url scheme] isEqualToString:#"gap"]) manages phonegap calls
BTW, you shouldn't use phonegap 2.3, you need at least 2.5 to pass apple store guidelines added in may, versions previous to 2.5 used the UDID and it's forbidden by apple
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;
}
}
I have a webview in my mainviewcontroller and i am laoding a webpage in viewdidload method as below:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_loginWebView loadRequest:requestObj];
}
and in the shouldstartloadwithrequest method, i check if the url contains "itunes" and i have the following code for it:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSRange textRange4;
textRange4 =[[[request URL] absoluteString] rangeOfString:#"itunes.apple.com"];
if(textRange4.location != NSNotFound)
{
UIApplication *app = [UIApplication sharedApplication];
NSString *newPath = [[request URL] absoluteString] ;
if ([newPath rangeOfString:#"insider"].location != NSNotFound) {
NSURL *myURL = [NSURL URLWithString:#"insider://"];
if ([app canOpenURL:myURL]) {
[app openURL:myURL];
NSLog(#"insider");
}
else
{
[app openURL:[NSURL URLWithString:newPath]];
}
}
else if ([newPath rangeOfString:#"reuters-news-pro-for-ipad"].location != NSNotFound) {
[app openURL:[NSURL URLWithString:newPath]];
}
else if ([newPath rangeOfString:#"thomson-reuters-marketboard"].location != NSNotFound) {
NSURL *myURL = [NSURL URLWithString:#"marketboard://"];
if ([app canOpenURL:myURL]) {
[app openURL:myURL];
NSLog(#"marketboard");
}
else
{
[app openURL:[NSURL URLWithString:newPath]];
}
}
else
{
[app openURL:[NSURL URLWithString:newPath]];
}
return NO;
}
return YES;
}
the above code works it opens the apps i desire but when I go back to my app from ipad, instead of going to the mainviewcontroller, it reopens the previous opened app.
for example if I opened marketboard app, it reopens it when i tap the app icon from ipad home.
But the above only happens in ios5.0, it does not happen in 6.0 which is really weird
It is the default behaviour when you have webview loadrequest in viewdidload method in ios5.0
and they have fixed it in ios6.0
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;
}
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.