目前我还在阅读NSTimer
的一些文档和教程。根据我目前的理解,我们调用计时器并给它一个方法,以便它自己重复。然后一个想法让我感到震惊。(我正在研究其他应用程序项目)
我打算做什么
UIWebView
NSTimer
内使用检查UIWebView
当前网址(absolutestring)的方法实现UIWebView
(可能为0.5秒),如果不是我指定的方法,则会重定向网址我有我想要实现的代码,目的是纠正我对UIWebView
和NSTimer
的了解,看看它在我开始研究之前是否合理。(我还在阅读在文档上。所以想要了解更多并在开始之前尝试更多)
现在令人困惑的部分是,我的方法是否与NSTimer
兼容?在NSTimer
UIWebView
方法中使用viewDidLoad
是否合理?由于应用程序每隔0.5秒不断运行该方法,是否会出现内存过载/崩溃?
例如NSTimer
(如果我错了,请纠正我。但是因为只要用户在UIWebView
中我希望它永远运行,我只需要这个代码集和不包括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,这是我正在使用的代码。(当我第一次改为此时它才起作用)**(如果我阻止了range2部分,那么错误域错误-999停止了。但是视图仍然无法加载)
- (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 - 如果我将NSRange range1和range2两者合并为1个语句,那么它再次起作用(希望它在一小时后仍能正常工作.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;
}
答案 0 :(得分:2)
如果你想要的是确保用户没有导航到其他网址,你可以添加以下内容
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[request.URL absoluteString] isEqualToString:@"http://YourAllowableURL1.com"]) {
return YES;
}
if ([[request.URL absoluteString] isEqualToString:@"http://YourAllowableURL2.com"]) {
return YES;
}
return NO;// user will not be able to go to any other urls
}
您还可以指定允许的网址列表,并确保仅针对这些网址返回“是”
答案 1 :(得分:1)
使用NSTimer确定尝试访问“未经批准”的网站并不是您想要的。如果网址不可接受,shouldStartLoadWithRequest
将允许您取消当前请求。但是,它不允许您更改URL以实现重定向。如果您真的想要重定向,可以在稍微延迟之后发起一个全新的请求,例如,
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *currentURL = [[request URL] absoluteString];
NSRange range = [currentURL rangeOfString:@"imc.jhmi.edu"];
if (range.location == NSNotFound)
{
double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
});
return NO;
}
return YES;
}
实际上看起来你不必等待,你实际上可以立即发起新请求,但在上一个shouldStartLoadWithRequest
完成之前这样做是不对的。但如果你想这样做,那显然会是:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *currentURL = [[request URL] absoluteString];
NSRange range = [currentURL rangeOfString:@"imc.jhmi.edu"];
if (range.location == NSNotFound)
{
NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
return NO;
}
return YES;
}
就个人而言,我根本不会重定向用户,但最多会弹出一个UIAlertView,告诉他们必须留在这个网站上,但这取决于你:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"%s %@", __FUNCTION__, request);
NSString *currentURL = [[request URL] absoluteString];
NSRange range = [currentURL rangeOfString:@"imc.jhmi.edu"];
if (range.location == NSNotFound)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"This app only follows links within the www.imc.jhmi.edu site."
message:nil
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
// [alert release]; // use in non-ARC
return NO;
}
return YES;
}