[IOS]UIWebView的使用
Demo:http://download.csdn.net/detail/u012881779/8663319
功能包括“返回上页”“在safari打开”“网页刷新”
#import "ViewController.h"
#define INNERURL @"http://www.ljcd.gov.cn"
@interface ViewController ()< UIWebViewDelegate >
@property (weak, nonatomic) IBOutlet UIView *titleView; //标题栏
@property (weak, nonatomic) IBOutlet UIWebView *cleanWebView; //WebView
@property (weak, nonatomic) IBOutlet UIView *cleanTitleView; //标题栏
@property (weak, nonatomic) IBOutlet UIView *returnCancelView;//返回、删除栏
@property (weak, nonatomic) IBOutlet UIScrollView *functionSV; //功能栏
@property (weak, nonatomic) IBOutlet UIButton *functionBut; //功能控件
@property (weak, nonatomic) IBOutlet UIView *backView; //玻璃纸
@property (strong, nonatomic) NSString* mURL; //外部url
@property (nonatomic) BOOL isSafari; //是否使用Safari打开
@end
@implementation ViewController
@synthesize isSafari = _isSafari;
-(void)viewDidDisappear:(BOOL)animated{
[_cleanWebView loadHTMLString:@"" baseURL:nil];
[_cleanWebView stopLoading];
[_cleanWebView setDelegate:nil];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
- (void)viewDidLoad{
[super viewDidLoad];
_cleanWebView.delegate = self;
_isSafari = NO;
[_cleanTitleView setHidden:NO];
[_returnCancelView setHidden:YES];
//webView
[self cleanChengDu];
}
//webView
-(void)cleanChengDu{
NSURL *url =[NSURL URLWithString:INNERURL];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[_cleanWebView loadRequest:request];
[_cleanWebView setScalesPageToFit:YES];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//点击链接
if(navigationType == UIWebViewNavigationTypeLinkClicked){
[_cleanTitleView setHidden:YES];
[_returnCancelView setHidden:NO];
//返回
}else if(navigationType == UIWebViewNavigationTypeBackForward){
//不能继续返回
if(![_cleanWebView canGoBack]){
[_cleanTitleView setHidden:NO];
[_returnCancelView setHidden:YES];
}
}
//是否使用safari打开
if(_isSafari){
_isSafari = NO;
NSURL *requestURL = [ request URL ];
if ( [ [ requestURL scheme ] isEqualToString: @"http" ] || [ [ requestURL scheme ] isEqualToString: @"https" ] || [ [ requestURL scheme ] isEqualToString: @"mailto" ]) {
return ![ [ UIApplication sharedApplication ] openURL: requestURL];
}
}
return YES;
}
//返回
- (IBAction)returnAction:(id)sender {
[_cleanWebView goBack];
}
//结束
- (IBAction)stopAction:(id)sender {
[_cleanTitleView setHidden:NO];
[_returnCancelView setHidden:YES];
[self cleanChengDu];
}
//功能
- (IBAction)functionAction:(id)sender {
UIButton *tempBut = (UIButton *)sender;
if(tempBut.isSelected){
tempBut.selected = NO;
//玻璃纸
[_backView setAlpha:0];
[_backView setHidden:NO];
//功能栏
CGRect startRect = _functionSV.frame;
startRect.size.height = 0;
[_functionSV setFrame:startRect];
[_functionSV setHidden:NO];
CGRect stopRect = _functionSV.frame;
stopRect.size.height = 156;
[UIView animateWithDuration:0.3 animations:^{
[_functionSV setFrame:stopRect];
[_backView setAlpha:0.3];
} completion:^(BOOL finished){}];
}else{
tempBut.selected = YES;
CGRect startRect = _functionSV.frame;
startRect.size.height = 0;
[UIView animateWithDuration:0.3 animations:^{
[_functionSV setFrame:startRect];
[_backView setAlpha:0];
} completion:^(BOOL finished){
[_functionSV setHidden:YES];
[_backView setHidden:YES];
}];
}
}
//刷新
- (IBAction)refreshAction:(id)sender {
//重新加载
[_cleanWebView reload];
[self cancelAction:nil];
}
//在safari打开
- (IBAction)safariAction:(id)sender {
_isSafari = YES;
[self cancelAction:nil];
//刷新
[self refreshAction:nil];
}
//取消
- (IBAction)cancelAction:(id)sender {
_functionBut.selected = NO;
[self functionAction:_functionBut];
}
//判断字符串不全为空
- (BOOL)judgeStringIsNull:(NSString *)string{
BOOL result = NO;
if(string != nil && string.length > 0){
for (int i = 0; i < string.length; i ++) {
NSString *subStr = [string substringWithRange:NSMakeRange(i, 1)];
if(![subStr isEqualToString:@" "] && ![subStr isEqualToString:@""]){
result = YES;
}
}
}
return result;
}
@end
示图: