iOS笔记----Hybrid应用基本核心

来源:互联网 发布:乐视手机数据恢复 编辑:程序博客网 时间:2024/06/10 17:15

What is this?

Hybrid App同时使用网页语言与程序语言开发,通过应用商店区分移动操作系统分发,用户需要安装使用的移动应用。

注意

WebView是实现Hybrid应用的核心,也是实现本地技术和Web技术融 合的核心。

示例

HTML代码

  • index.html
<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalabel=no;" />    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>    <script>    // 从iOS对象中调用    function helloWorld(msg) {        document.getElementById('message').innerHTML = msg;    }    // 调用iOS对象    function showiOSDialog(msg){        var myJsonObject = new Object();        myJsonObject.title = "HelloWorld";        myJsonObject.message = msg;        var JSONString = JSON.stringify(myJsonObject);        var uri = "gap://XXXClass.XXXmethod#" + JSONString;        window.location = uri;    }    </script></head><body><h2>iOS本地与Web 相调用</h2>    <button type="button" onclick="showiOSDialog('JS Call iOS 对象')">调用iOS对象</button>    <br><br>    <div id='message'></div></body></html>

iOS本地代码

  • ViewController.m
////  ViewController.m//  HybridSample////  Created by 王巍 on 16/4/29.//  Copyright © 2016年 ww. All rights reserved.//#import "ViewController.h"#import "NSString+URLEncoding.h"@interface ViewController ()<UIWebViewDelegate>@property (weak, nonatomic) IBOutlet UIWebView *webView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    self.webView.delegate = self;    // 异步加载本地HTML    NSString *path = [[NSBundle mainBundle] pathForResource:@"www/index" ofType:@"html"];    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark - ******** UIWebViewDelegate// iOS对象调用JS函数-(void)webViewDidFinishLoad:(UIWebView *)webView{    // 调用页面中JS函数    [self.webView stringByEvaluatingJavaScriptFromString:@"helloWorld('从IOS对象中调用的JS!')"];}// js调用iOS方法-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{    // js请求路径 gap://XXXClass.XXXmethod#传参(fragment)    NSString *actionType = request.URL.host;        // XXXClass.XXXmethod    NSString *scheme = request.URL.scheme;          // gap    NSString *fragment = [request.URL.fragment URLDecodedString];       // 传参    NSData *responseData = [fragment dataUsingEncoding:NSUTF8StringEncoding];    if([scheme isEqualToString:@"gap"]){        if([actionType isEqualToString:@"XXXClass.XXXmethod"]){            NSError *error;            // 解析json字符串            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&error];            NSLog(@"title: %@ message: %@", [json objectForKey:@"title"], [json objectForKey:@"message"]);        }    }    return YES;}@end

上述代码中用到了NSString的分类类,代码如下:

NSString分类类代码

  • NSString+URLEncoding.h
////  NSString+URLEncoding.h//  HybridSample////  Created by 王巍 on 16/4/29.//  Copyright © 2016年 ww. All rights reserved.//#import <Foundation/Foundation.h>@interface NSString (URLEncoding)-(NSString *)URLEncodedString;-(NSString*)URLDecodedString;@end
  • NSString+URLEncoding.m
////  NSString+URLEncoding.m//  HybridSample////  Created by 王巍 on 16/4/29.//  Copyright © 2016年 ww. All rights reserved.//#import "NSString+URLEncoding.h"@implementation NSString(URLEncoding)-(NSString *)URLEncodedString{    NSString *result = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR("!*'();:@&amp;=+$,/?%#[] "), kCFStringEncodingUTF8));    return result;}- (NSString *)URLDecodedString{    NSString *result = (NSString *)CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8));    return result;}@end

关于iOS分类类,在下面这篇笔记也有简单的介绍
iOS笔记—- object-c 分类

0 0
原创粉丝点击