Reputation: 685
I'm trying to do a UIWebView
, but not allowing the user to visit other part of the given URL. Something like they could only go specific pages where i allow them to go, and not others.
I'm not too sure about the - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
method, so I may have made a mistake there. The problem now is that if I enable this method, I am loaded into the webpage
but am unable to browse. Not even the section i allow.
I would like to know if 1)Am I missing out any important code?, 2)Am I missing anything in the code, or why is my code wrong?
Am new to Xcode, so I will require all the guidance that you guys can give/teach me. Below is my code from the view controller.
EDIT1: Is there any prebuilt method by Xcode, or do I have to create my own method, such that when they are browsing the UIWebView
, and when they click on another link, if its something other than what I specify, the request get rejected? Is it possible to create a method such that it will constantly check the absolute url and if it change to something else other than what i specify, it will return to my specified url?
in my.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIWebViewDelegate>
{
IBOutlet UIWebView *webView;
}
@property (nonatomic, retain) UIWebView *webView;
@end
in my .m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize webView;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *currentURL = [[request URL]absoluteString];
NSRange range1 = [currentURL rangeOfString:@"news"];
NSRange range2 = [currentURL rangeOfString:@"pdf"];
if (range1.location ==NSNotFound){
currentURL = @"http://www.imc.jhmi.edu/news.html";
[webView reload];
return YES;
}else if (range2.location ==NSNotFound){
currentURL = @"http://www.imc.jhmi.edu/news.html";
[webView reload];
return YES;
}
return NO;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
webView.scalesPageToFit=YES;
webView.delegate = self;
NSString *urlAddress = @"http://www.imc.jhmi.edu/news.html";
NSURL *url =[NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
Upvotes: 1
Views: 1297
Reputation: 38239
This delegate method webView:shouldStartLoadWithRequest:navigationType: is Sent before a web view begins loading a frame. and it Return Value YES if the web view should begin loading content; otherwise, NO.
Your method returns NO when currentURL with range1 and range2 not found.
Upvotes: 3