苹果开发

Nepenthes对于苹果开发的支持包括三种系统:iOS, macOS和tvOS,分别有不同的Nepenthes.framework对应,请选择合适的版本下载使用。在苹果开发的培训课程中,将使用macOS进行。本部分的开发培训,需要开发者精通Objective-C语言和Cocoa开发,有开发iOS, macOS或tvOS的经验,并且熟悉开发工具Xcode的使用。


工程Framework开发

  1. 打开Xcode创建工程,选择macOS系统下的Cocoa Framework,如下图所示:
  2. 点击Next后,输入工程名称Test,如下图所示:
  3. 选择项目存储位置
  4. 在下载页面下载macOS版本的Nepenthes.framework,解压缩后,拖入工程节点Test之下
  5. 确保Targets下的Test在选中状态,点击Build Settings
  6. 在Build Settings的搜索框输入mach,将Mach-O Type值选择Static Library
  7. 继续在Build Setting的搜索框输入non-modular,将Allow Non-modular Includes In Framework Modules的值选择为YES
  8. 选中左侧的Test工程节点,右键从快捷菜单中选择New Group
  9. 创建两个Group:Json和BLSync
  10. 在Json Group节点上右键,从快捷菜单中选择New File...,选择macOS系统下的Cocoa Class
  11. 在Class中输入UserxJson,Subclass of输入AbstractJson,Language选择Objective-C
  12. 选择Json,点击Create,生成UserxJson.h和UserxJson.m

  13. 编辑UserxJson.h,使其源代码如下:
    //
    //  UserxJson.h
    //
    //  Created by Everdow at 2017-10-12 10:5:24
    //  Copyright © 2017 Everdow Ltd. All rights reserved.
    //
    
    #import 
    
    @interface UserxJson : AbstractJson
    {
        NSString *ID;  //Auto Increment Unique ID
        NSString *FirstName;  //名(MaxLen=20)
        NSString *LastName;  //姓(MaxLen=20)
        NSString *Gender;  //性别
        NSString *Birthdate;  //出生日期
        NSString *Salary;  //薪水(MaxLen=10)
        NSString *RegisteredTime;  //注册时间
        NSString *Memo;  //备注
    }
    @property(nonatomic,retain) NSString *ID;
    @property(nonatomic,retain) NSString *FirstName;
    @property(nonatomic,retain) NSString *LastName;
    @property(nonatomic,retain) NSString *Gender;
    @property(nonatomic,retain) NSString *Birthdate;
    @property(nonatomic,retain) NSString *Salary;
    @property(nonatomic,retain) NSString *RegisteredTime;
    @property(nonatomic,retain) NSString *Memo;
    
    @end
  14. 编辑UserxJson.m,使其源代码如下:
    //
    //  UserxJson.m
    //
    //  Created by Everdow at 2017-10-12 10:5:24
    //  Copyright © 2017 Everdow Ltd. All rights reserved.
    //
    
    #import "UserxJson.h"
    
    @implementation UserxJson
    @synthesize ID;
    @synthesize FirstName;
    @synthesize LastName;
    @synthesize Gender;
    @synthesize Birthdate;
    @synthesize Salary;
    @synthesize RegisteredTime;
    @synthesize Memo;
    
    @end
  15. 选中BLSync节点,创建BLSyncUserx.h和BLSyncUserx.m
  16. 编辑BLSyncUserx.h,使其源代码如下:
    //
    //  BLSyncUserx.h
    //
    //  Created by Everdow at 2017-10-12 10:5:24
    //  Copyright © 2017 Everdow Ltd. All rights reserved.
    //
    
    #import 
    #import "UserxJson.h"
    
    @interface BLSyncUserx : BLSyncAbstract
    
    //insert UserxJson to server database
    -(BOOL)insert:(UserxJson *)json;
    
    //delete records that in condition with whereClause,whereClause should SQL WHERE latter part,if there will the symbol of ',please use ~ to replace,like: @"Name LIKE ~'John'~"
    -(BOOL)delete:(NSString *)whereClause;
    
    //delete record by its ID,it's the extension of -(BOOL)delete:(NSString *)whereClause
    -(BOOL)deleteByID:(NSString *)ID;
    
    //update UserxJson to server database
    -(BOOL)update:(UserxJson *)json;
    
    // get count from the records that in condition with whereClause
    -(NSInteger)getCount:(NSString *)whereClause;
    
    //query database without no paging
    -(NSArray *)select:(NSString *)whereClause;
    
    //query database with paging,index is the cursor start position,step is the paging number
    -(NSArray *)select:(NSString *)whereClause index:(NSString *)index step:(NSString *)step;
    
    //get UserxJson by it's ID
    -(UserxJson *)getJson:(NSString *)ID;
    
    @end
  17. 编辑BLSyncUser.m,使其源代码如下:
    //
    //  BLSyncUserx.m
    //
    //  Created by Everdow at 2017-10-12 10:5:24
    //  Copyright © 2017 Everdow Ltd. All rights reserved.
    //
    
    #import "BLSyncUserx.h"
    
    @implementation BLSyncUserx
    
    -(BOOL)insert:(UserxJson *)json
    {
        return [super insert:json];
    }
    
    -(BOOL)delete:(NSString *)whereClause
    {
        return [super delete:whereClause];
    }
    
    -(BOOL)deleteByID:(NSString *)ID
    {
        return [self delete:[@"ID=" stringByAppendingString:ID]];
    }
    
    -(BOOL)update:(UserxJson *)json
    {
        return [super update:json];
    }
    
    -(NSInteger)getCount:(NSString *)whereClause
    {
        return [super getCount:whereClause];
    }
    
    -(NSArray *)select:(NSString *)whereClause
    {
        return [super select:whereClause];
    }
    
    -(NSArray *)select:(NSString *)whereClause index:(NSString *)index step:(NSString *)step
    {
        return  [super select:whereClause index:index step:step];
    }
    
    -(UserxJson *)getJson:(NSString *)ID
    {
        UserxJson *json = nil;
        
        NSArray *result = [self select:[@"ID=" stringByAppendingString:ID]];
        if(result)
        {
            json = [UserxJson getJson:[result objectAtIndex:0]];
        }
        
        return json;
    }
    
    @end
  18. 选择Build Phases选项卡,展开Headers,选中Project节点下的BLSyncUserx.h和UserxJson.h
  19. 右键从快捷菜单中选择Move to Public Group
  20. 检视确保Test.h, BLSyncUserx.h和UserxJson.h在Public组中
  21. 展开左侧的Test节点,编辑Test.h,使其源代码如下:
    //
    //  Test.h
    //  Test
    //
    //  Created by XIAOHUI LI on 2017/10/12.
    //  Copyright © 2017年 Everdow Ltd. All rights reserved.
    //
    
    #import 
    
    //! Project version number for Test.
    FOUNDATION_EXPORT double TestVersionNumber;
    
    //! Project version string for Test.
    FOUNDATION_EXPORT const unsigned char TestVersionString[];
    
    // In this header, you should import all the public headers of your framework using statements like #import 
    
    #import 
    #import 
  22. 从Xcode的Product菜单中选择Build,以执行编译。
  23. 这是会在节点Products下生成Test.framework
  24. 选中Test.framework右键从快捷菜单中选择Show in Finder,把此文件复制出来以备在Xcode的应用工程中使用。


工程开发:调用上面Build生成的Test.framework

  1. 创建工程TestClient,类型为Coaco App。

  2. 设置Build Setting中的Allow Non-modular Include In Framework Modules的值为YES
  3. 将Nepenthes.framework和Test.framework拖到TestClient项目节点下
  4. 将工程的Bundle Identifier修改为org.everdow.nepenthes.test以正确使用Nepenthes的macOS许可。
  5. 在下截页面下载Nepenthes的macOS试用许可Config.plist拖放至TestClient工程。
  6. 在Build Phases中的Copy Bundle Resource加入Config.plist
  7. 由于Xcode对于安全通讯的要求,请在Info.plist增加下图中所示的选项。
  8. 打开ViewController.h,编辑源代码如下:
  9. licenseTest:检测Nepenthes的macOS许可状态。
    - (void)licenseTest
    {
        NSInteger licenseCode = [MiscTool hasLicensed];
        NSString *licenseMsg = [MiscTool getLicenseMessage:licenseCode];
        NSLog(@"License:%@",licenseMsg);
    }
  10. insertTest:调用BLSyncUserx对象的insert方法将UserxJson对象远程插入服务器的数据库中。
    - (void)insertTest
    {
        //Insert Test
        UserxJson *userxJson = [UserxJson new];
        userxJson.FirstName = @"晓辉";
        userxJson.LastName = @"李";
        userxJson.Gender = @"1";
        userxJson.Birthdate = @"1975-11-18";
        userxJson.Salary = @"90000";
        userxJson.RegisteredTime = [MiscTool getServerNow];
        userxJson.Memo = @"李晓辉是Nepenthes的作者。";
        
        BLSyncUserx *blSyncUserx = [BLSyncUserx new];
        BOOL flag = [blSyncUserx insert:userxJson];
        NSLog(@"Insert:%d",flag);
    }
  11. updateTest:调用BLSyncUserx对象的update方法,入参3请以数据库中的实际ID字段值为准。
    - (void)updateTest
    {
        BLSyncUserx *blSyncUserx = [BLSyncUserx new];
        UserxJson *userxJson = [blSyncUserx getJson:@"3"];
        userxJson.Birthdate = @"1975-10-16";
        BOOL flag = [blSyncUserx update:userxJson];
        NSLog(@"Update:%d",flag);
    }
  12. getCountTest:根据SQL的条件语句查询符合条件的数据库记录数量。
    - (void)getCountTest
    {
        BLSyncUserx *blSyncUserx = [BLSyncUserx new];
        NSString *whereClause = @"1=1";
        NSInteger count = [blSyncUserx getCount:whereClause];
        NSLog(@"getCount:%ld",(long)count);
    }
  13. selectTest:根据SQL的条件语句查询数据库,并返回数组。select还有一个方法实现,即可以分页显示的,具体请参阅BLSyncUserx头文件。
    - (void)selectTest
    {
        BLSyncUserx *blSyncUserx = [BLSyncUserx new];
        NSString *whereClause = @"1=1";
        NSArray *result = [blSyncUserx select:whereClause];
        for(NSDictionary *record in result)
        {
            UserxJson *userxJson = [UserxJson getJson:record];
            NSLog(@"%@",userxJson.FirstName);
        }
    }
  14. deleteTest:根据ID删除指定的数据库记录,返回BOOL。delete还有一个实现,即根据SQL条件语句删除一批数据库记录,具体请参阅BLSyncUserx头文件。
    - (void)deleteTest
    {
        BLSyncUserx *blSyncUserx = [BLSyncUserx new];
        BOOL flag = [blSyncUserx deleteByID:@"3"];
        NSLog(@"Delete:%d",flag);
    }
  15. 基于Nepenthes的实现在iOS, macOS和tvOS中是一样的,不同的是在创建Framework时要选择不同的平台即可,对于Json对象和BLSync对象的实现方式并无两样。