关闭

ios中webview的高级用法

61人阅读 评论(0) 收藏 举报
分类:
1.隐藏上下滚动时出边界的后面的黑色的阴影
- (void) hideGradientBackground:(UIView*)theView
{
  for (UIView * subview in theView.subviews)
  {
    if ([subview isKindOfClass:[UIImageView class]])
      subview.hidden = YES;

    [self hideGradientBackground:subview];
  }
}

2. 禁用拖拽时的反弹效果
[(UIScrollView *)[[webView subviews] objectAtIndex:0] setBounces:NO];  

3. 判断用户点击类型

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    switch (navigationType) 
    {
        //点击连接
        case UIWebViewNavigationTypeLinkClicked:
        {
            NSLog(@"clicked");
        }
            break;
        //提交表单
        case UIWebViewNavigationTypeFormSubmitted:
        {
            NSLog(@"submitted");
        }
        default:
            break;
    }
    return YES;
}


#import "BaseViewController.h"

@interface BaseViewController ()
{
    UIWebView *webview;
}

@end

@implementation BaseViewController

#pragma  mark -life cicry


- (void)viewDidLoad
{
    [super viewDidLoad];
    webview=[[UIWebView alloc] initWithFrame:self.view.bounds];
    webview.delegate=self;//因为这个代理设置的self
    [self.view addSubview:webview];
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestUrl]]];
    
    [webview release];
    UIScrollView *scollview=(UIScrollView *)[[webview subviews]objectAtIndex:0];
    scollview.bounces=NO;
    
    // Do any additional setup after loading the view.
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)viewDidUnload{
    [super viewDidUnload];
    self.requestUrl=nil;
}

- (void)dealloc
{
    [_requestUrl release];
    [super dealloc];
}








#import "MJViewController.h"
#import "MBProgressHUD+Add.h"

@interface MJViewController ()

@end

@implementation MJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self loadRemoteHTML];
}

#pragma mark 加载远程的HTML页面
- (void)loadRemoteHTML {
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.106:8080/MJServer/"];
    
    self.webView.delegate = self;
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}

#pragma mark 加载其他文件
- (void)loadRemoteTxt {
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.106:8080/MJServer/abc.txt"];
    
    self.webView.delegate = self;
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}

#pragma mark 加载PDF文件
- (void)loadLocalPDF {
    //[self.webView loadData:<#(NSData *)#> MIMEType:<#(NSString *)#> textEncodingName:<#(NSString *)#> baseURL:<#(NSURL *)#>];
    
    NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/第10章-网络.pdf"];
    
    self.webView.delegate = self;
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}

#pragma mark 加载HTML字符串
- (void)loadHTMLString {
    NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Pictures"];
    [self.webView loadHTMLString:@"<html><body><img src='lufy.png'></body></html>" baseURL:url];
}

#pragma mark - webview代理方法
- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"开始加载页面");
    [MBProgressHUD showMessag:@"哥正在帮你加载..." toView:self.view];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"页面加载完毕");
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    self.backItem.enabled = self.webView.canGoBack;
    self.forawrdItem.enabled = self.webView.canGoForward;
    
    self.titleItem.title = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    
    //[self.webView stringByEvaluatingJavaScriptFromString:@"fn();"];
}
#pragma mark webview每次加载之前都会调用这个方法
// 如果返回NO,代表不允许加载这个请求
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    // 说明协议头是ios
    if ([@"ios" isEqualToString:request.URL.scheme]) {
        NSString *url = request.URL.absoluteString;
        NSRange range = [url rangeOfString:@":"];
        NSString *method = [request.URL.absoluteString substringFromIndex:range.location + 1];
        
        SEL selector = NSSelectorFromString(method);
        
        if ([self respondsToSelector:selector]) {
            [self performSelector:selector];
        }
        
        return NO;
    }
    
    return YES;
}

#pragma mark - 提供一个接口方法给JS调用
- (void)openCamera {
    NSLog(@"打开了照相机");
}

- (void)call {
    NSLog(@"打电话");
}

#pragma mark - 按钮方法
#pragma mark 前进
- (void)forawrd {
    [self.webView goForward];
}
#pragma mark 后退
- (void)back {
    [self.webView goBack];
}
@end
复制代码

服务端的用法


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1614次
    • 积分:57
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:8篇
    • 译文:0篇
    • 评论:0条
    文章分类