I have a main UIWebView and I am trying to open any URL using SVWebViewController. My code can be seen bellow.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
SVModalWebViewController *webViewController = [[SVModalWebViewController alloc] initWithAddress:#"http://google.com"];
[self presentViewController:webViewController animated:YES completion:NULL];
return NO;
}
return YES;
}
It seems like SVWebViewController has an initWithAddress parameter. I want to know if I can pass the requested URL in that parameter.
Thank you in advance.
Found a way out of this. It seems like I could work with
initWithURLRequest:(NSURLRequest *)request
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
SVModalWebViewController *webViewController = [[SVModalWebViewController alloc] initWithURLRequest:(NSURLRequest *)request];
[self presentViewController:webViewController animated:YES completion:NULL];
return NO;
}
return YES;
}
Related
I have a webView which has URLs link to KEYWORD. Upon clicking the URL, I would like to call the following method:
[self.db fetchDefinitionsWithKeyword:#"KEYWORD" callback:^(NSDictionary *response) {
NSString *HTML = [self.HTMLRenderer renderHTML:response];
[self.webView loadHTMLString:HTML baseURL:nil];
In Android, we can use shouldOverrideUrlLoading. How should I do that in Objective C?
Try this code may be its helpfull for you.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeLinkClicked )
{
// you can call any thing on click.
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
you can use this method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
if you want do at your click, you can judge the navigationType is UIWebViewNavigationTypeLinkClicked. otherwise you can ignore navigationType.
all webview request go through this method . good luck.
I am using the below to add a ProgressView while the UIWebView is loading and dismissing it when it's done.
The problem is the UIWebView is started twice and also the ProgressView, So when the page is done loading only one ProgressView is dismissed and the other one doesn't.
ViewController.h
#import <UIKit/UIKit.h>
#interface DirectionViewController : UIViewController <UIWebViewDelegate>
#property (weak, nonatomic) IBOutlet UIWebView *directionWebView;
#end
ViewController.m
#interface ViewController ()
#property UIColor *yellowColor;
#property MRProgressOverlayView *progressView;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.yellowColor =
[UIColor colorWithRed:244.0f/255.0f
green:208.0f/255.0f
blue:63.0f/255.0f
alpha:1.0f];
self.directionWebView.delegate = self;
NSString *urlMap = [NSString stringWithFormat:#"%#%f,%f&zoom=14", #"http://maps.google.com/maps?q=", self.coordLat, self.coordLong];
NSURL *url = [NSURL URLWithString:urlMap];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.directionWebView loadRequest:urlRequest];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"WebView start loading...");
self.progressView = [MRProgressOverlayView new];
self.progressView.mode = MRProgressOverlayViewModeIndeterminateSmall;
[self.view.window addSubview:self.progressView];
[self.progressView setTintColor:self.yellowColor];
[self.progressView setTitleLabelText:#"Loading ..."];
[self.progressView show:YES];
return YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[self.progressView dismiss:YES];
}
#end
Dont know why it is calling twice but you can do this.
if(!self.progressView){
self.progressView = [MRProgressOverlayView new];
self.progressView.mode = MRProgressOverlayViewModeIndeterminateSmall;
[self.view.window addSubview:self.progressView];
[self.progressView setTintColor:self.yellowColor];
[self.progressView setTitleLabelText:#"Loading ..."];
[self.progressView show:YES];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[self.progressView dismiss:YES];
self.progressView = nil;
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
it may be triggered more than once, e.g redirects, iframe loading and so on. It's very inconvenient to initialise objects here.
Use Lazy Initialization design pattern for it
- (MRProgressOverlayView *)progressView {
if (_progressView) {
_progressView = [... alloc] init];
// ... setup your progressView
}
return _progressView;
}
btw, you forgot about error handling:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
// You need to stop loading indicator here, right?!
}
Second thing is - (maybe for you case it's not a big deal) but usually it's tricky to exactly determine when UIWebView stopped loading (all the resources loaded, AJAX requests and so on)
Personally I'm using something like code below to manage activity indicator start/stop behaviour for UIWebView
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"Webview (%p) starting to load URL: %#", self, request.URL);
self.URL = request.URL;
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[self.spinner startAnimating];
self.webViewLoadingCount++;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
self.webViewLoadingCount--;
if (self.webViewLoadingCount > 0) return;
[self.spinner stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
self.webViewLoadingCount--;
[self.spinner stopAnimating];
// Ignore NSURLErrorDomain error (-999).
if (error.code == NSURLErrorCancelled) return;
// Ignore "Frame Load Interrupted" errors
if (error.code == 102 && [error.domain isEqual:#"WebKitErrorDomain"]) return;
NSLog(#"WebView (%p) experienced an error: %#.", self, [error localizedDescription]);
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
This delegate method can be called multiple times (in case of redirection for example). So I would recommend to move your Progress view init code to viewDidLoad method.
- (void)viewDidLoad {
[super viewDidLoad];
//...
self.progressView = [MRProgressOverlayView new];
self.progressView.mode = MRProgressOverlayViewModeIndeterminateSmall;
[self.view.window addSubview:self.progressView];
[self.progressView setTintColor:self.yellowColor];
[self.progressView setTitleLabelText:#"Loading ..."];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"WebView start loading...");
[self.progressView show:YES];
return YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[self.progressView dismiss:YES];
}
I want to perform some action when the user clicks any link on UIWebView. I am doing this for making this work :
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"request - %#", request.URL.absoluteString);
[webView.window makeKeyAndVisible];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
if(isFirstTimeLoaded)
{
[self shoToolbar];
[self updateButtons];
}
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
isFirstTimeLoaded = YES;
}
This usually works fine. But it doesn't when UIWebView's first load involves multiple redirection while loading the page. What I want is to fire this condition only when the user intentionally clicks on the link. Not on the url redirection of UIWebView.
You have to check navigationType
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType==UIWebViewNavigationTypeLinkClicked) {
// It was a link
} else {
// It wasn't a link
}
return YES;
}
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
}
}
I want to create an iPhone WebView App. But there is a problem, if I click on some buttons, it opens the normal and not the mobile version.
I did create the same WebView app on Android and there I solve the problem with this code
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(!url.toLowerCase().contains("http://www.example.com/"))
{
webView.loadUrl(url + "?mt=1");
return true;
}
return false;
}
});
How can I do this on xcode?
Thank you very much
Best wishes
Dominik
You can do the same using the UIWebViewDelegate protocol.
I think you can achieve the required result by changing the NSURLRequest within the delegate function
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request.......
//delegate methods
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//use NSURLRequest object request , to manage the request.
//use NSURLRequest object request , to manage the request.
NSURL *urL=request.URL;
NSString *urlStr=[urL absoluteString];
NSLog(#"URLL %#",urlStr);
if([urlStr isEqualToString:#"http://www.google.com/"]){
urL=[NSURL URLWithString:#"http://www.yahoo.com"];
request=[request initWithURL:urL];
[webView loadRequest:request];
}
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
//start of request
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//finished loading
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//error
}
This will work for sure. I am using above code to load www.yahoo.com when www.google.com is requested.