zeroc-ice网络请求使用介绍

来源:互联网 发布:手机直播完整源码 编辑:程序博客网 时间:2024/05/20 23:37

官网:https://zeroc.com

官方Ice Builder pluginshttps://github.com/zeroc-ice/ice-builder-xcode

官方实例ice-demos-masterhttps://github.com/zeroc-ice/ice-demos

 

 

Demo实例下载


Ice网络请求使用介绍

1、安装IceIceTouchIceBuilder

1-1Ice的安装

使用brewbrew-http://brew.sh)安装(支持java则添加“—with-java”

终端安装命令:brew install ice,或 brew install ice --with-java


1-2IceTouch安装

使用brewbrew-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-3IceBuilder的安装

安装方法1

下载插件(下载地址:https://github.com/zeroc-ice/ice-builder-xcode),然后进行安装。

安装方法2

使用Alcatraz插件管理工具进行安装。


2、项目配置

2-1、添加framework

CFNetwork.framework

Security.framework

Foundation.framework

ExternalAccessory.framework

 

2-2Additional 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-3Ice 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-4Code Signing Resource Rules Path的配置

配置(非必须 TARGETS-Build Settings-Code Signing - Code Signing Resource Rules Path):$(SDKROOT)/ResourceRules.plist



3、使用

3-1、特别说明,系统会在编译时自动根据xxx.ice文件生成对应的xxx.hxxx.m文件,该文件包含相应的数据格式类型、接口名称等。如xxx.ice文件为userservice.ice,则生成文件为userservice.huserservice.m

 

3-2、导入相关头文件,及对象定义

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 头文件:  
  2. #import <objc/Ice.h>       // Ice  
  3. @protocol ICECommunicator; // Ice连接器  
  4. #import <userservice.h>  
  5. @protocol usersUserServicePrx;  
  6.   
  7. 对象定义:  
  8. id<ICECommunicator> communicator;  
  9. id<usersUserServicePrx> userServicePrx;  

3-3、根据服务器地址,及商品生成id<ICECommunicator>实例

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 代码如下:  
  2. if (communicator == nil)  
  3. {  
  4.    ICEInitializationData *initData = [ICEInitializationData initializationData];  
  5.    initData.properties = [ICEUtil createProperties];  
  6.    // 属性设置  
  7.    // 方法1 文件配置方法  
  8.    // NSString *propertiesString = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"IceConfig.client"];  
  9.    // [initData.properties load:propertiesString];  
  10.    // [initData.properties setProperty:@"Ice.ACM.Client.Timeout" value:@"0"];  
  11.    // [initData.properties setProperty:@"Ice.RetryIntervals" value:@"-1"];  
  12.    // 方法2 setvlue设置  
  13.    [initData.properties setProperty:@"Ice.Default.Locator" value:@"IceGrid/Locator:tcp -h 192.168.3.164 -p 4061"];  
  14.    initData.dispatcher = ^(id<ICEDispatcherCall> call, id<ICEConnection> con) {  
  15.        dispatch_sync(dispatch_get_main_queue(), ^ {  
  16.            [call run];  
  17.        });  
  18.    };  
  19.    NSAssert(communicator == nil@"communicator == nil");  
  20.    communicator = [ICEUtil createCommunicator:initData];  
  21. }  


3-4、根据接口名称,实例化id<usersUserServicePrx>对象模型,并获取数据

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 代码如下:  
  2.  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {  
  3.     @try {  
  4.         ICEObjectPrx *base = [communicator stringToProxy:@"UserService"]; // UserService为接口名称  
  5.         base = [base ice_invocationTimeout:5000]; // 调用超时时间单位毫秒  
  6.         NSLog(@"base %@",[base ice_toString]);  
  7.         userServicePrx = [usersUserServicePrx checkedCast:base];  
  8.         NSLog(@"NSThread %@", [NSThread callStackSymbols]);  
  9.         usersMutableUserSeq *results = [userServicePrx queryUser:paraString]; // 返回结果数据  
  10.         NSLog(@"results %@", results);  
  11.     }  
  12.     @catch (ICEEncapsulationException *exception) {  
  13.         NSString *exceptionString = [NSString stringWithFormat:@"Invalid router: %@", exception.reason];  
  14.         dispatch_async(dispatch_get_main_queue(), ^ {  
  15.             [self exception:exceptionString];  
  16.         });  
  17.     }  
  18.     @catch (ICEException *exception) {  
  19.         dispatch_async(dispatch_get_main_queue(), ^ {  
  20.             NSLog(@"error=%@", [exception description]);  
  21.         });  
  22.     }  
  23.     @catch (NSException *exception) {  
  24.         dispatch_async(dispatch_get_main_queue(), ^ {  
  25.         NSLog(@"error=%@", exception.description);  
  26.         });  
  27.     }  
  28.  });  

3-5ice对象释放

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. if (communicator)  
  2.  {  
  3.     __block id<ICECommunicator> releaseCommunicator = communicator;  
  4.     communicator = nil;  
  5.     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
  6.         @try {  
  7.             [releaseCommunicator shutdown];  
  8.         }  
  9.         @catch (NSException *exception) {  
  10.    
  11.         }  
  12.         @finally {  
  13.             [releaseCommunicator destroy];  
  14.             releaseCommunicator = nil;  
  15.         }  
  16.     });  
  17.    
  18.     NSLog(@"释放 Ice 成功");  
  19.  }  
0 0
原创粉丝点击