kerkee在iOS上的快速上手指南
AmbroseAdki
8年前
<p>这篇文章将介绍kerkee如何在iOS上快速上手!</p> <p>kerkee是一个多主体共存型Hybrid框架,具有跨平台、用户体验好、性能高、扩展性好、灵活性强、易维护、规范化、集成云服务、具有Debug环境、彻底解决跨域问题。</p> <p>Github上的地址: <a href="/misc/goto?guid=4959671875458410324" rel="nofollow,noindex">https://github.com/kercer/kerkee_ios</a></p> <p>OSChina上的地址: <a href="/misc/goto?guid=4959671875555818062" rel="nofollow,noindex">http://git.oschina.net/zihong/kerkee_ios</a></p> <p>官网地址: <a href="/misc/goto?guid=4959671875638736470" rel="nofollow,noindex">http://www.kerkee.com</a></p> <p>kerkee的native部分目前支持Android和iOS平台,两个平台的架构设计及接口设计保持一致,大大降低了跨平台的成本,下面介绍一下如何集成kerkee以及如何上手快速做出一款自己的demo</p> <p>kerkee的集成有:包的方式集成和源码集成两种方式,你可以选择其中的一种方式集成</p> <h2>包方式集成</h2> <p>iOS的kerkee使用CocoaPods进行管理,在集成的时候很方便。</p> <p>简述:如果你已知道CocoaPods如何使用,你只需在Podfile中添加 pod ‘kerkee’, ’~> 1.0.1’ 或者 pod ‘kerkee’ ,然后即可走起,此时你可不用看以下的详细说明了</p> <p>CocoaPods的安装及深入使用:请点击这里</p> <p><em>前提:</em> cocopods安装,安装非常简单,本来想略去这一部分的,但还是贴一下命令吧,如果不明白网络上搜索一搜一大把。</p> <pre> <code class="language-objectivec">sudo gem install cocoapods pod setup </code></pre> <p>1、 创建Podfile使用也比较简单,cd到你的工程的根目录$projectPath</p> <pre> <code class="language-objectivec">cd $projectPath pod init </code></pre> <p>这个命令就会再工程根目录下面生成一个Podfile文件,podfile用文本编辑就行了</p> <p>2、 配置Podfile在你工程的 <strong> <em>Podfile</em> </strong> 文件添加</p> <pre> <code class="language-objectivec">pod ‘kerkee’, ’~> 1.0.1’ </code></pre> <p>或者你想直接使用最新版可把版本号去掉,使用以下这句</p> <pre> <code class="language-objectivec">pod ‘kerkee’ </code></pre> <p>我帖出个podfile文件配置的例子</p> <pre> <code class="language-objectivec"># Uncomment this line to define a global platform for your project # platform :ios, '8.0' # Uncomment this line if you're using Swift # use_frameworks! target 'TestKerkeePod' do #or this way,If it is a team work,please do not use this way. #because it use last version of kerkee,it is a bad way #pod ‘kerkee’ pod ‘kerkee’, ’~> 1.0.1’ end target 'TestKerkeePodTests' do end target 'TestKerkeePodUITests' do end </code></pre> <p>3、执行导入命令</p> <p>使用终端cd 到你的Podfile所在的目录,然后再使用 pod install 就可以把这些开源库导入,CocoaPods就开始为我们做下载源码、配置依赖关系、引入需要的framework等一些列工作。</p> <pre> <code class="language-objectivec">cd $projectPath pod install --verbose </code></pre> <p>安装完,这些库都放在一个Pods的工程中,然后会用xcode的workspace来管理这个工程和你自己的工程。以后就打开 yourproject.xcworkspace 就行了。</p> <p>至此,基本集成已完成!!!恭喜你已经集成了kerkee,接下来就代码走起!!!</p> <h2>源码集成</h2> <ul> <li>1、拉取源码 git clone <a href="/misc/goto?guid=4959671875722103425" rel="nofollow,noindex">https://github.com/kercer/kerkee_ios.git</a></li> <li>2、拷贝根目录下的kerkee目录到你的工程目录(这步也可以忽略)</li> <li>3、打开你的工程,把kerkee.xcodeproj文件拉到你的工程里进行lib依赖</li> <li>4、找到你工程的TARGETS,在 Embedded Binaried 添加kerkee.framework</li> </ul> <h2>iOS快速上手指南</h2> <p><em>简述:</em> 只需三步即可代码快速上手</p> <ol> <li>注册js与native对应的类</li> <li>实现native部分与js对应的方法</li> <li>创建KCWebview进行调用</li> </ol> <p>以下详情说明一下如何快速上手</p> <ul> <li><strong>注册对应的接口类或模块</strong></li> </ul> <p>在使用之前,你必须先注册对应的接口类或模块,使得js对象能与Native类进行映射,全局只需要注册一次就够了</p> <pre> <code class="language-objectivec"> //have rewritten jsBridgeClient in kerkee //you can use this way, first you can import "KCJSDefine.h" //[KCJSBridge registClass:[KCApiOverrideJSBridgeClient class] jsObjName:kJS_jsBridgeClient]; [KCJSBridge registJSBridgeClient:[KCApiOverrideJSBridgeClient class]]; [KCJSBridge registClass:[KCApiTest class] jsObjName:kJS_TestModule]; //you can regist class which inherit from KCJSObject,js call static function //[KCJSBridge registClass:[KCApiJSObjExample class] jsObjName:kJS_JSObjExampleModule]; [KCJSBridge registObject:[[KCApiJSObjExample alloc]init] ]; </code></pre> <ul> <li><strong>实现JS对应的Native接口</strong></li> </ul> <p>KCApiOverrideJSBridgeClient</p> <pre> <code class="language-objectivec">#import "KCApiOverrideJSBridgeClient.h" #import "KCBaseDefine.h" #import "KCJSBridge.h" @implementation KCApiOverrideJSBridgeClient +(void)testJSBrige:(KCWebView*)aWebView argList:(KCArgList*)args { NSString* jsonInfo = [args getString:@"info"]; KCLog(@"%@", jsonInfo); } +(void)commonApi:(KCWebView*)aWebView argList:(KCArgList*)args { NSString* jsonInfo = [args getString:@"info"]; KCLog(@"%@", jsonInfo); NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setObject:@"OK!" forKey:@"info"]; NSString *json = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dic options:0 error:nil] encoding:NSUTF8StringEncoding]; KCAutorelease(json); //回调 [KCJSBridge callbackJS:aWebView callBackID:[args getObject:@"callbackId"] jsonString:json]; } @end </code></pre> <p>KCApiTest</p> <pre> <code class="language-objectivec">#import "KCApiTest.h" #import "KCBaseDefine.h" #import "KCJSBridge.h" @implementation KCApiTest +(void)testInfo:(KCWebView*)aWebView argList:(KCArgList*)args { NSString* jsonInfo = [args getString:@"testInfo"]; KCLog(@"%@", jsonInfo); KCJSCallback* callback = [args getCallback]; if (callback) { [callback callbackJS:aWebView jsonString:@"{}"]; } //也可以这么回调 //[KCJSBridge callbackJS:aWebView callBackID:[args getString:@"callbackId"] string:@"This is testInfo callball"]; } @end </code></pre> <p>KCApiJSObjExample</p> <pre> <code class="language-objectivec">#import "KCApiJSObjExample.h" #import "KCJSObjDefine.h" @implementation KCApiJSObjExample - (NSString*)getJSObjectName { return kJS_JSObjExampleModule; } -(void)objExampleNotStaticFunction:(KCWebView*)aWebView argList:(KCArgList*)args { KCLog(@"objExampleNotStaticFunction"); } +(void)objExampleStaticFunction:(KCWebView*)aWebView argList:(KCArgList*)args { KCLog(@"objExampleStaticFunction"); } @end </code></pre> <ul> <li><strong>创建KCWebView</strong></li> </ul> <p>在ViewController中,添加成员变量</p> <pre> <code class="language-objectivec">@interface ViewController () { KCWebView* m_webView; KCJSBridge* m_jsBridge; } </code></pre> <p>初始化KCWebView和KCJSBridge对象</p> <pre> <code class="language-objectivec"> m_webView = [[KCWebView alloc] initWithFrame:self.view.bounds]; //add webview in your view [self.view addSubview:m_webView]; //you can implement webview delegate m_jsBridge = [[KCJSBridge alloc] initWithWebView:m_webView delegate:self]; </code></pre> <ul> <li><strong>实现KCWebView代理</strong></li> </ul> <pre> <code class="language-objectivec">#pragma mark -- #pragma mark KCWebViewProgressDelegate -(void)webView:(KCWebView*)webView identifierForInitialRequest:(NSURLRequest*)initialRequest { } #pragma mark - UIWebView Delegate - (void)webViewDidFinishLoad:(UIWebView *)aWebView { NSString *scrollHeight = [aWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"]; NSLog(@"scrollHeight: %@", scrollHeight); NSLog(@"webview.contentSize.height %f", aWebView.scrollView.contentSize.height); NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:aWebView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[scrollHeight floatValue]]; // [aWebView addConstraint:heightConstraint]; NSLog(@"webview frame %@", NSStringFromCGRect(aWebView.frame)); } - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { } - (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { return YES; } </code></pre> <p>至此,简单Demo即可运行起来</p> <p> </p> <p>来自: <a href="/misc/goto?guid=4959671875799265366" rel="nofollow">http://blog.linzihong.com/kerkeezai-iosshang-de-kuai-su-shang-shou-zhi-nan/</a></p>