Этот код может помочь у ...
UIWebView является UIView, который может загрузить веб-страницу, оставаясь при этом в приложении пользователя. Навигация на другие веб-страницы разрешается с помощью встроенных ссылок на самой веб-странице. Перемещение вперед и назад по истории можно настроить с помощью методов экземпляра goForward и goBack, но программист должен предоставить кнопки.
В следующем примере используется UIWebView и
1) добавляет вперед и назад кнопки. Кнопки включены и Мелированные с помощью UIWebViewDelegate дополнительные методы webViewDidStartLoad: и webViewDidFinishLoad:
2) добавляет UIActivityIndicatorView, который отображает в то время как веб-страница загружается
В файле .h для WebViewController:
Объявите 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
// В файле .m для WebViewController
@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