我已使用UIWebView
在我的应用程序中实现了一个浏览器,默认情况下我在浏览器中加载了google页面。
当我在google页面中搜索某些内容时,会调用UIWebViewDelegate
的{{1}}方法。
问题是,当我点击此搜索页面上的后退按钮时,没有代表被调用,所以我在禁用后退按钮时出现问题。
此问题仅发生在不在iPhone应用程序中的iPad应用程序中。
答案 0 :(得分:5)
此代码可能对您有帮助......
UIWebView是一个UIView,可以在用户的应用程序中加载网页。 通过在网页本身中使用嵌入链接,允许导航到其他网页。可以使用实例方法goForward和goBack设置历史记录的前进和后退导航,但程序员必须提供按钮。
以下示例使用UIWebView和
1)添加前进和后退按钮。使用UIWebViewDelegate可选方法webViewDidStartLoad:和webViewDidFinishLoad启用并突出显示按钮:
2)添加一个UIActivityIndicatorView,在网页加载时显示
在WebViewController的.h文件中:
声明UIWebView,(可选):添加按钮以控制前进和后退浏览历史记录和按下按钮的IBActions,可选再次:添加UIActivityIndicatorView。
@interface WebViewController : UIViewController <UIWebViewDelegate>
{
UIWebView *webView;
UIButton *back;
UIButton *forward;
UIActivityIndicatorView *activityIndicator;
}
@property(nonatomic,retain)IBOutlet UIWebView *webView;
@property(nonatomic,retain)IBOutlet UIButton *back;
@property(nonatomic,retain)IBOutlet UIButton *forward;
@property(nonatomic,retain)IBOutlet UIActivityIndicatorView *activityIndicator;
-(IBAction)backButtonPressed: (id)sender;
-(IBAction)forwardButtonPressed: (id)sender;
@end
//在WebViewController的.m文件中
@implementation WebViewController
@synthesize webView;
@synthesize back;
@synthesize forward;
@synthesize activityIndicator;
//method for going backwards in the webpage history
-(IBAction)backButtonPressed:(id)sender {
[webView goBack];
}
//method for going forward in the webpage history
-(IBAction)forwardButtonPressed:(id)sender
{
[webView goForward];
}
//programmer defined method to load the webpage
-(void)startWebViewLoad
{
//NSString *urlAddress = @"http://www.google.com";
NSString *urlAddress = @"http://cagt.bu.edu/page/IPhone-summer2010-wiki_problemsandsolutions";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
// acivityIndicator is set up here
- (void)viewDidLoad
{
//start an animator symbol for the webpage loading to follow
UIActivityIndicatorView *progressWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//makes activity indicator disappear when it is stopped
progressWheel.hidesWhenStopped = YES;
//used to locate position of activity indicator
progressWheel.center = CGPointMake(160, 160);
self.activityIndicator = progressWheel;
[self.view addSubview: self.activityIndicator];
[self.activityIndicator startAnimating];
[progressWheel release];
[super viewDidLoad];
//call another method to do the webpage loading
[self performSelector:@selector(startWebViewLoad) withObject:nil afterDelay:0];
}
- (void)dealloc
{
[webView release];
[back release];
[forward release];
[activityIndicator release];
[super dealloc];
}
#pragma mark UIWebViewDelegate methods
//only used here to enable or disable the back and forward buttons
- (void)webViewDidStartLoad:(UIWebView *)thisWebView
{
back.enabled = NO;
forward.enabled = NO;
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView
{
//stop the activity indicator when done loading
[self.activityIndicator stopAnimating];
//canGoBack and canGoForward are properties which indicate if there is
//any forward or backward history
if(thisWebView.canGoBack == YES)
{
back.enabled = YES;
back.highlighted = YES;
}
if(thisWebView.canGoForward == YES)
{
forward.enabled = YES;
forward.highlighted = YES;
}
}
@end
/ <强> * ** * ** * ** * ** * ** * ** * ** * ** * **** < /强> /
//In viewDidLoad for the class which adds the WebViewController:
WebViewController *ourWebVC = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
ourWebVC.title = @"WebView";
[self.view addSubview:ourWebVC];
//release ourWebVC somewhere else
答案 1 :(得分:4)
在您的情况下,您必须忽略/避免“缓存数据”。以下代码行可能有所帮助。
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"] cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
[webView loadRequest:requestObj];