官方Ice Builder plugins:https://github.com/zeroc-ice/ice-builder-xcode
官方实例ice-demos-master:https://github.com/zeroc-ice/ice-demos
Ice网络请求使用介绍
1、安装Ice、IceTouch、IceBuilder
1-1、Ice的安装
使用brew(brew-http://brew.sh)安装(支持java则添加“—with-java”)
终端安装命令:brew install ice,或 brew install ice --with-java
1-2、IceTouch安装
使用brew(brew-http://brew.sh)安装
终端安装命令:
sudo xcode-select -s /Applications/Xcode-beta.app/Contents/Developer/(注意:Xcode-beta.app根据你自己安装的实际版本设置,如我自己的是Xcode.app)
brew tap zeroc-ice/tap
brew install icetouch36 --build-from-source
1-3、IceBuilder的安装
安装方法1
下载插件(下载地址:https://github.com/zeroc-ice/ice-builder-xcode),然后进行安装。
安装方法2
使用Alcatraz插件管理工具进行安装。
2、项目配置
2-1、添加framework
CFNetwork.framework
Security.framework
Foundation.framework
ExternalAccessory.framework
2-2、Additional SDKs的配置,即IceTouch的配置
根据安装的Ice Touch所在路径设置(TARGETS-Build Settings-Architectures - Additional SDKs)。
比如在我自己的电脑上安装Ice Touch后,其所在路径为:硬盘->usr->local->Cellar->icetouch36->3.6.2->lib->IceTouch->Objc->iphones.skd,则设置为:/usr/local/Cellar/icetouch36/3.6.2/lib/IceTouch/ObjC/$(PLATFORM_NAME).sdk。
2-3、Ice Home的配置,即IceBuilder的配置
根据安装的Ice所在路径设置(TARGETS-Build Settings-Ice Builder - General Options-Ice Home)。
比如在我自己的电脑上安装Ice后,其所在路径为:硬盘->usr->local->Cellar->ice->3.6.2,则设置为:/usr/local/Cellar/ice/3.6.2
注意:安装后,必须在项目中添加xxx.ice文件才会显示Ice Builder选项,xxx.ice文件来自服务端同事提供。
2-4、Code Signing Resource Rules Path的配置
配置(非必须 TARGETS-Build Settings-Code Signing - Code Signing Resource Rules Path):$(SDKROOT)/ResourceRules.plist
3、使用
3-1、特别说明,系统会在编译时自动根据xxx.ice文件生成对应的xxx.h、xxx.m文件,该文件包含相应的数据格式类型、接口名称等。如xxx.ice文件为userservice.ice,则生成文件为userservice.h、userservice.m。
3-2、导入相关头文件,及对象定义
- 头文件:
- #import <objc/Ice.h> // Ice
- @protocol ICECommunicator; // Ice连接器
- #import <userservice.h>
- @protocol usersUserServicePrx;
- 对象定义:
- id<ICECommunicator> communicator;
- id<usersUserServicePrx> userServicePrx;
3-3、根据服务器地址,及商品生成id<ICECommunicator>实例
- 代码如下:
- if (communicator == nil)
- {
- ICEInitializationData *initData = [ICEInitializationData initializationData];
- initData.properties = [ICEUtil createProperties];
- // 属性设置
- // 方法1 文件配置方法
- // NSString *propertiesString = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"IceConfig.client"];
- // [initData.properties load:propertiesString];
- // [initData.properties setProperty:@"Ice.ACM.Client.Timeout" value:@"0"];
- // [initData.properties setProperty:@"Ice.RetryIntervals" value:@"-1"];
- // 方法2 setvlue设置
- [initData.properties setProperty:@"Ice.Default.Locator" value:@"IceGrid/Locator:tcp -h 192.168.3.164 -p 4061"];
- initData.dispatcher = ^(id<ICEDispatcherCall> call, id<ICEConnection> con) {
- dispatch_sync(dispatch_get_main_queue(), ^ {
- [call run];
- });
- };
- NSAssert(communicator == nil, @"communicator == nil");
- communicator = [ICEUtil createCommunicator:initData];
- }
3-4、根据接口名称,实例化id<usersUserServicePrx>对象模型,并获取数据
- 代码如下:
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
- @try {
- ICEObjectPrx *base = [communicator stringToProxy:@"UserService"]; // UserService为接口名称
- base = [base ice_invocationTimeout:5000]; // 调用超时时间单位毫秒
- NSLog(@"base %@",[base ice_toString]);
- userServicePrx = [usersUserServicePrx checkedCast:base];
- NSLog(@"NSThread %@", [NSThread callStackSymbols]);
- usersMutableUserSeq *results = [userServicePrx queryUser:paraString]; // 返回结果数据
- NSLog(@"results %@", results);
- }
- @catch (ICEEncapsulationException *exception) {
- NSString *exceptionString = [NSString stringWithFormat:@"Invalid router: %@", exception.reason];
- dispatch_async(dispatch_get_main_queue(), ^ {
- [self exception:exceptionString];
- });
- }
- @catch (ICEException *exception) {
- dispatch_async(dispatch_get_main_queue(), ^ {
- NSLog(@"error=%@", [exception description]);
- });
- }
- @catch (NSException *exception) {
- dispatch_async(dispatch_get_main_queue(), ^ {
- NSLog(@"error=%@", exception.description);
- });
- }
- });
3-5、ice对象释放
- if (communicator)
- {
- __block id<ICECommunicator> releaseCommunicator = communicator;
- communicator = nil;
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- @try {
- [releaseCommunicator shutdown];
- }
- @catch (NSException *exception) {
- }
- @finally {
- [releaseCommunicator destroy];
- releaseCommunicator = nil;
- }
- });
- NSLog(@"释放 Ice 成功");
- }