zoukankan      html  css  js  c++  java
  • UI控件(UIWebView)

    本文主要记录UIWebView三方面内容:

    1、基本的加载网页链接或文件;

    2、网页js调用原生,也就是Cordova混合架构的原理;

    3、原生调用js程序;

    • 原生部分主要代码:
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.title = @"WebView Test";
        
        webview_ = [[UIWebView alloc]init];
        webview_.frame = self.view.bounds;
        
        webview_.delegate = self;
        
        //自动缩放页面
        webview_.scalesPageToFit = YES;
        
        //webview提供的导航方法如下:
        //    [webView_ goBack];
        //    [webview_ goForward];
        //    [webview_ reload];
        //    [webview_ stopLoading];
     
        [self.view addSubview:webview_];
    }
    
    -(void)viewDidAppear:(BOOL)animated{
        NSLog(@"viewDidAppear");
        // 加载普通URL
        // NSURL* url;
        // http请求需要在info.plist信息中增加如下配置:
        //    <key>NSAppTransportSecurity</key>
        //    <dict>
        //    <key>NSAllowsArbitraryLoads</key>
        //    <true/>
        //    </dict>
        //    url = [[NSURL alloc]initWithString:@"http://www.baidu.com/"];
        //    [webview_ loadRequest:[NSURLRequest requestWithURL:url]];
        
        [self loadHTMLFile:@"hello.html"];
        
    }
    
    //加载一个本地html
    - (void)loadHTMLFile:(NSString*)filename {
        NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@""];
        NSData* data = [NSData dataWithContentsOfFile:path];
        [webview_ loadData:data MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:nil];
    }
    
    //javascript调用原生方法,也是Cordova混合架构的原理
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    {
        NSLog(@"shouldStartLoadWithRequest");
        NSString *url = request.URL.absoluteString;
        NSLog(@"%@", url);
        if([url hasSuffix:@"iostest"]){
            return NO;
        }
        return YES;
    }
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        //执行javascript语句
        NSLog(@"webViewDidFinishLoad");
        NSString *js = @"test2('hello')";
        [webview_ stringByEvaluatingJavaScriptFromString:js];
    }
    
    
    @end
    • 网页部分
    <html>
        <head>
            <script type="text/javascript">
                var iFrame;
                iFrame = document.createElement('iframe');
                iFrame.style.display = 'none';
                document.documentElement.appendChild(iFrame);
                //创建一个iFrame,并修改其src,此时IOS中的shouldStartLoadWithRequest会被回调
                
                function test1(){
                    iFrame.src = "iostest";
                }
            
                function test2(input){
                    alert(input);
                }
                
            </script>
        </head>
        
        <body>
            <p>This is Test Page.</p>
            <button onclick="test1()">hello</button>
        </body>
    </html>
  • 相关阅读:
    汽车行业如何利用大数据
    汽车行业如何利用大数据
    Linux下Gcc 的编译过程
    第一个GraphX程序
    VC++ 模拟&quot;CLICK事件&quot;关闭指定窗体
    基于Canvas的Char.js库使用
    VELT-0.1.6开发:载入根文件系统
    HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)
    JavaScript图片裁剪
    C++ string中的几个小陷阱,你掉进过吗?
  • 原文地址:https://www.cnblogs.com/Fredric-2013/p/5193959.html
Copyright © 2011-2022 走看看