Currently I'm still reading through some document and tutorial for NSTimer
. From my current understanding, We call the timer and give it a method so that it will repeat itself. Then an idea struck me.(im working on other app proj)
What I plan to do
- implement
UIWebView
- implement
NSTimer
(maybe 0.5sec) within theUIWebView
with a method that check theUIWebView
current url (absolutestring) and if it's not what I specified, it will redirect the url.
I have the code that I want to implement, the purpose is to rectify my knowledge on UIWebView
and NSTimer
and see if its plausible before I start working on it.( I'm still reading on documents. So would like to learn more and try out more before working on it)
The confusing part to me now is that, is my method compatible with NSTimer
? Will it be plausible to use NSTimer
within a UIWebView
viewDidLoad
method? Will there be a memory overload/crash due to the app constantly running the method every 0.5sec?
e.g of NSTimer
(correct me if I'm wrong. But since I want it to be running forever as long as the user are in the UIWebView
, I will only require this code set and not include NSRunLoop
)
NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 2.0
target: self
selector:@selector(onTick:)
userInfo: nil repeats:NO];
-(void)onTick:(NSTimer *)timer {
//do smth
}
EDIT2- @Robert Ryan, this is the code I'm using.(which work just now when i first changed to this) **(If i blocked out range2 section, then the error domain error-999 stopped. but the view still doesn't load)
- (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)
{
NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
return NO; //no to this if its not found.
}
if(range2.location==NSNotFound)
{
NSURL * url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
return NO; //no to this if its not found
}
return YES; //everything else ok
}
EDIT3 - if i combine the both NSRange range1 and range2 into 1 statement then it work again(hopefully it will still work after an hour. xD)
- (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 & range2.location==NSNotFound)
{
NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
return NO;
}
return YES;
}