IOS
1 需要提供给我们的信息
应用id IOS Bundle ID
, 例如 com.example.test
2 代码编写
引入必要framework
Security.framewrok
UIKit.framework
WebKit.framework
初始化SDK
//引入 CiShuParaphraseKit.framework
#import <CiShuParaphraseKit/CiShuParaphraseKit.h>
网络请求超时时间设置,SDK默认5s
[CiShuDictionaryManager setTimeoutIntervalForRequest:timeoutInterval];
无需获取书本权限初始
[CiShuDictionaryManager authWithAppKey:@"AppKey" appSecret:@"AppSecret"];
获取书本权限
[CiShuDictionaryManager authWithAppKey:@"AppKey" appSecret:@"AppSecret" resultBlock:^(NSArray<CiShuBookObject *> * _Nonnull container) {
初始化成功返回权限书本数组
}];
[CiShuDictionaryManager authWithAppKey:@"AppKey" appSecret:@"AppSecret" successBlock:^(NSArray<CiShuBookObject *> * _Nonnull container) {
初始化成功返回权限书本数组
} failureBlock:^(CiShuAuthCodeType failureCode){
初始化失败返回授权状态码
}];
建议在AppDelegate
中引入 必须在查词前完成注册激活
AppKey
:查词平台后台注册给到的AppKey
AppSecret
:后台给到的对应AppKey的AppSecret
初始化失败
网络不佳或用 户手机使用本地的自定义时间与标准UTC时间相差过大,可能导致初始化失败。
定义了枚举值CiShuAuthCodeType
如使用场景中有需要,在调用查词前,可使用
[CiShuDictionaryManager cheackInitStatus]
查看SDK是否激活成功,失败也可以通过对应状态做出处理修改
调用查词方法一
[CiShuDictionaryManager showExplainViewWithWord:text viewDirection:direction origin:origin viewWidth:Width viewMaxHeight:300];
查词方法 text
:查询的词组 direction
:框的朝向,向上或向下绘制 origin
:结果显示框的绘制初始点 Width
:框的宽度 Height
:绘制框的高度
如果查询词汇可以获取到历史纪年、古今地名数据、英汉、同义词等,可以同步渲染
调用查词方法二
首先获取单词是否包含释义、历史纪年、古今地名、英译中、中译英、同义词
[CiShuDictionaryManager cheackWordContainer: text resultBlock:^(CiShuContainerResult * _Nonnull container) {
返回CiShuContainerResult 对象
}];
其中释义、古今地名、英译中、中译英等xml使用字体加密的方式对数据进行了加密,在获得数据解析成功之后,请使用返回对象中的fontInfo
获取对应的字体包,使用线上字体包数据进行渲染。
fontInfo:
{
fontId = 字体包ID;
fontName = 字体包FontName;
ttf = 字体包 .ttf下载地址;
woff = 字体包 .woff 下载地址;
}
CFErrorRef error;
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fontUrl);
CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
bool isSuccess = CTFontManagerRegisterGraphicsFont(fontRef, &error);
if(!isSuccess){
//如果注册失败,则不使用
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to load font: %@", errorDescription);
CFRelease(errorDescription);
}
CGFontRelease(fontRef);
单独获取释义
@param word 查询的词条
@param bookId 当查询具体哪本书时,传入激活时书籍对象数 组,传空数组则不限制,全局查找
@param result 返回结果的回调
[CiShuDictionaryManager getExplainWithWord:text bookId:bookIdArray resultBlock:^(CiShuResultObject * _Nonnull response) {
}];
查询单字笔顺
@param word 查询的单字
@param strokeFilter 笔顺过滤器数组、目前仅支持black white
@param result 返回结果的回调
[CiShuDictionaryManager getStrokeOrderForWord:text strokeFilter:@[@"black",@"white"]
resultBlock:^(CiShuWordStrokeObject * _Nonnull response) {
}];
单独获取历史纪年
[CiShuDictionaryManager getHistoricalYearWithWord:
text resultBlock:^(CiShuHistoricalResultObject
* _Nonnull response) {
返回CiShuHistoricalResultObject对象
}];
单独获取古今地名
[CiShuDictionaryManager getAncientPlaceWithWord:text resultBlock:^(CiShuPlaceResultObject * _Nonnull response) {
返回CiShuPlaceResultObject对象
}];
英译汉
传入bookid查询特定书籍解释
[CiShuDictionaryManager getEnglishExplainWithWord:text bookId:bookIdArray resultBlock:^(CiShuEnToCn * _Nonnull response) {
返回CiShuEnToCn对象
}];
[CiShuDictionaryManager getEnglishExplainWithWord:text resultBlock:^(CiShuEnToCn * _Nonnull response) {
返回CiShuEnToCn对象
}];
汉译英
传入bookid查询特定书籍解释
[CiShuDictionaryManager ChineseToEnglishWithWord:text bookId:bookIdArray resultBlock:^(CiShuCnToEn * _Nonnull response) {
返回CiShuCnToEn对象
}];
[CiShuDictionaryManager ChineseToEnglishWithWord:text resultBlock:^(CiShuCnToEn * _Nonnull response) {
返回CiShuCnToEn对象
}];
3 显示样式设置
CiShuShowConfig
可设置显示框中的字体样式、背景颜色、字体颜色等
CiShuShowConfig * config = [CiShuShowConfig defaultConfig];
config.titleFont = [MyTool regularFontWithSize:17];
config.listFont = [MyTool regularFontWithSize:15];
config.spellFont = [MyTool regularFontWithSize:15];
config.sourceFont = [MyTool regularFontWithSize:10];
config.showBG = [[UIColor alloc]initWithRed:1.0 green:0.7 blue:0.2 alpha:0.8];
config.titleColor = [UIColor redColor];
config.listColor = [UIColor brownColor];
如果不自定义设置,系统将采用默认字体、颜色、背景。详情参见CiShuShowConfig.h
文件
//-----字体-----
//查询词的字体大小 默认:17
@property (nonatomic , assign) NSInteger titleFontSize;
//释义、历史纪年 默认:15
@property (nonatomic , assign) NSInteger segFontSize;
//查询词拼音字体 默认:15
@property (nonatomic , assign) NSInteger spellFontSize;
//结果释义条目 默认:16
@property (nonatomic , assign) NSInteger listFontSize;
//释义条目引申 默认:14 已弃用,与listFont相同
@property (nonatomic , assign) NSInteger listCiteFontSize;
//来源介绍字体 默认:12
@property (nonatomic , assign) NSInteger sourceFontSize;
4 错误码说明
SDK返回错误
typedef NS_ENUM(NSInteger, CiShuResultCodeType) {
CiShuResultCodeTypeOK = 0,
CiShuResultCodeTypeNetError = -1,//网络错误
CiShuResultCodeTypeNullResp = -2,//无相关释义
CiShuResultCodeTypeUnauthorized = -3,//权限验证错误
CiShuResultCodeTypeOtherLimit = -4,//查询次数过多,受限
CiShuResultCodeTypeOtherError = -5,//其他错误
CiShuResultCodeTypeTimeout = -6,//网络请求超时
CiShuResultCodeTypelengthError = -7,//查词长度过长
};
typedef NS_ENUM(NSInteger, CiShuAuthCodeType) {
CiShuAuthCodeTypeSucess = 1,//初始化成功,可以正常使用查词功能
CiShuAuthCodeTypeInit = 0,//初始化中
CiShuAuthCodeTypeTimeError = -1,//初始化失败,用户使用的自定义时间与标准UTC时间偏差过大
CiShuAuthCodeTypeAuthError = -2,//权限不正确,请检查SDK appKey appSecret
CiShuAuthCodeTypeNetError = -3,//网络异常
CiShuAuthCodeTypeLimitError = -4,//查询次数异常,限制查询
};
@interface CiShuResultObject : NSObject
//返回错误错误码,正常为 0
@property (nonatomic , assign) CiShuResultCodeType errorCode;
//错误描述
@property (nonatomic , strong) NSString * errorDescription;
//服务器返回的XML数据
@property (nonatomic , strong) NSString * docXml;
//词头
@property (nonatomic , strong) NSString * headword;
//数据来源
@property (nonatomic , strong) NSString * docSource;
@end