我已经通过使用在我的应用程序中实现了一个浏览器UIWebView
,默认情况下我在我的浏览器中加载谷歌页面。
当我在 google 页面中搜索某些内容时,会调用UIWebViewDelegate
'swebView:shouldStartLoadWithRequest:navigationType:
方法。
问题是当我从这个搜索页面点击后退按钮时,没有代表被调用,所以我在禁用我的后退按钮时遇到问题。
此问题仅发生在 iPad 应用程序中,而不发生在 iPhone 应用程序中。
我已经通过使用在我的应用程序中实现了一个浏览器UIWebView
,默认情况下我在我的浏览器中加载谷歌页面。
当我在 google 页面中搜索某些内容时,会调用UIWebViewDelegate
'swebView:shouldStartLoadWithRequest:navigationType:
方法。
问题是当我从这个搜索页面点击后退按钮时,没有代表被调用,所以我在禁用我的后退按钮时遇到问题。
此问题仅发生在 iPad 应用程序中,而不发生在 iPhone 应用程序中。
这段代码可以帮助你...
UIWebView 是一个 UIView,它可以加载网页,同时保留在用户的应用程序中。通过使用网页本身中的嵌入链接,可以导航到其他网页。可以使用实例方法 goForward 和 goBack 设置通过历史的向前和向后导航,但程序员必须提供按钮。
以下示例使用 UIWebView,并且
1)添加前进和后退按钮。使用 UIWebViewDelegate 可选方法 webViewDidStartLoad: 和 webViewDidFinishLoad: 启用和突出显示按钮
2) 添加一个 UIActivityIndicatorView 在网页加载时显示
在 WebViewController 的 .h 文件中:
声明 UIWebView,可选:添加按钮以控制通过浏览历史和 IBAction 向前和向后移动以按下按钮,可选再次:添加 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
在您的情况下,您必须忽略/避免“缓存数据”。以下代码行可能会有所帮助。
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"] cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
[webView loadRequest:requestObj];