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>
  • 相关阅读:
    总结C# 调用c++ 开源代码使用问题
    nodejs v14使用await async
    一个简单的js文件,在ts中使用的方法
    ts项目+webpack+juuery 插件的引入
    js 立即执行的多种写法
    在webgl2上使用fabric做标记
    comobox 绑定datatable ,无法获取选择值问题
    axios 请求拦截并在 token 过期后自动续订后重调当前请求
    javascript hook 一个函数(不定参数个数)
    java Date 大坑
  • 原文地址:https://www.cnblogs.com/Fredric-2013/p/5193959.html
Copyright © 2011-2022 走看看