蘋果開發

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對象的實現方式並無兩樣。