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>
  • 相关阅读:
    成功交付离岸项目
    利用CSP探测网站登陆状态
    Web NFC API
    HTML/W3C-WHATWG-Differences
    MIT教授将网页开发整合为完整独立的程式语言Ur/Web
    移动端前端开发调试
    从0到100——知乎架构变迁史
    C++之再续前缘(一)——C++基础(与C语言的差异)(上)
    又爱又恨系列之枚举enum
    数据结构之队列(三)——循环队列
  • 原文地址:https://www.cnblogs.com/Fredric-2013/p/5193959.html
Copyright © 2011-2022 走看看