天天看點

使用Objective-C開發AppleWatch(一)關于Complication使用Objective-C開發AppleWatch(一)關于Complication (持續更新)

使用Objective-C開發AppleWatch(一)關于Complication (持續更新)

現在用Swift開發appleWatch的教程比較多,而比起Swift,Objective-C仍然是Cocoa架構開發的主力語言,是以在這裡,介紹給大家如何用Objective-C來開發一個我們的appleWatch Complication(表盤)

什麼是Complication開發,AppleWatch表盤子產品有哪些種類

分類 子產品化表盤小元件 子產品化表盤大元件 實用表盤小元件 實用表盤大元件 多彩表盤元件
示意圖
使用Objective-C開發AppleWatch(一)關于Complication使用Objective-C開發AppleWatch(一)關于Complication (持續更新)
使用Objective-C開發AppleWatch(一)關于Complication使用Objective-C開發AppleWatch(一)關于Complication (持續更新)
使用Objective-C開發AppleWatch(一)關于Complication使用Objective-C開發AppleWatch(一)關于Complication (持續更新)
使用Objective-C開發AppleWatch(一)關于Complication使用Objective-C開發AppleWatch(一)關于Complication (持續更新)
使用Objective-C開發AppleWatch(一)關于Complication使用Objective-C開發AppleWatch(一)關于Complication (持續更新)

下面我們就要把我們的應用上的一些資訊,顯示到表盤中去,用這5種顯示的形态

如何開始我們的表盤的開發工作

一、建立一個帶有Complication的AppleWatch工程

使用Objective-C開發AppleWatch(一)關于Complication使用Objective-C開發AppleWatch(一)關于Complication (持續更新)

watchOSApp依附于 iOSApp,這次介紹的watchOSApp是建立在一個iOSApp上的。

二、Complication這個類中實作的方法簡介

complication這個類是表盤的資料源類,它繼承的是

在這個類中的方法主要分為三個部分

①timeLine的設定部分

//是否支援timeTravel,和支援timeTravel的方向(時間的前進和後退)
- (void)getSupportedTimeTravelDirectionsForComplication:(CLKComplication *)complication withHandler:(void (^)(CLKComplicationTimeTravelDirections directions))handler {
    handler(CLKComplicationTimeTravelDirectionForward | CLKComplicationTimeTravelDirectionBackward);
}
           
//支援timeTravel的開始時間
- (void)getTimelineStartDateForComplication:(CLKComplication *)complication withHandler:(void (^)(NSDate *__nullable date))handler {
    handler(nil);
}
           
//支援timeTravel的結束時間
- (void)getTimelineEndDateForComplication:(CLKComplication *)complication withHandler:(void (^)(NSDate *__nullable date))handler {
    handler(nil);
}
           
//是否支援在鎖屏期間檢視
- (void)getPrivacyBehaviorForComplication:(CLKComplication *)complication withHandler:(void (^)(CLKComplicationPrivacyBehavior privacyBehavior))handler {
    handler(CLKComplicationPrivacyBehaviorShowOnLockScreen);
}
           

②timeLine的資料源部分

//傳回目前時間的entry
- (void)getCurrentTimelineEntryForComplication:(CLKComplication *)complication withHandler:(void (^)(CLKComplicationTimelineEntry *__nullable))handler {
   handler(nil);
}
           
//傳回向過去TimeTravel時要展示的一個entry數組
- (void)getTimelineEntriesForComplication:(CLKComplication *)complication beforeDate:(NSDate *)date limit:(NSUInteger)limit withHandler:(void (^)(NSArray <CLKComplicationTimelineEntry *> *__nullable entries))handler {
   handler(nil);
}
           
//傳回向未來TimeTravel時要展示的一個entry數組
- (void)getTimelineEntriesForComplication:(CLKComplication *)complication afterDate:(NSDate *)date limit:(NSUInteger)limit withHandler:(void (^)(NSArray <CLKComplicationTimelineEntry *> *__nullable entries))handler {
   handler(nil);
}
           

③更新資料部分

//在你想更新你的資料的時候調用
- (void)getNextRequestedUpdateDateWithHandler:(void (^)(NSDate *__nullable updateDate))handler {
    // Call the handler with the date when you would next like to be given the opportunity to update your complication content
    handler(nil);
}
           

④placeHolder的顯示内容

//在選取小元件的時候的沒有内容的預覽(placeholder)
- (void)getPlaceholderTemplateForComplication:(CLKComplication *)complication withHandler:(void (^)(CLKComplicationTemplate *__nullable complicationTemplate))handler {
    handler(nil);
}
           

三、編寫placeholder要顯示的内容

//官方表示這個placeholder隻有在app被更新或重裝的時候才會更換
- (void)getPlaceholderTemplateForComplication:(CLKComplication *)complication withHandler:(void (^)(CLKComplicationTemplate *__nullable complicationTemplate))handler {
    // 首先判斷進入這個消息時表盤元件是什麼,根據不同的表盤,傳回不同的template
    if (complication.family == CLKComplicationFamilyUtilitarianLarge) {
    //截止到日前,template已經有了24個種類,點入檢視可以看到具體的枚舉類數量
        CLKComplicationTemplateUtilitarianLargeFlat *template = [[CLKComplicationTemplateUtilitarianLargeFlat alloc]init];
        template.textProvider = [CLKSimpleTextProvider textProviderWithText:@"AppLogo"];
        handler((CLKComplicationTemplate *)template);
    }
    handler(nil);
}
           

關于provider:provider是一個集合,一個種類,text provider就相當于label,image provider就相當于imageView,使用起來非常的便捷可靠,因為表盤的顯示也比較的簡潔和固定,是以我們隻需要選用某一種類型,然後賦上值就可以了

四、編寫目前時間要顯示的内容

這一塊可以說是最為重要的,就是目前時間,在你的表盤上,你的app需要在表盤中顯示的内容,需要傳回的是一個NSDate類型的日期變量和一個CLKComplicationTimelineEntry類型的變量,截止到日前,這個變量已經有24個種類供使用

- (void)getCurrentTimelineEntryForComplication:(CLKComplication *)complication withHandler:(void (^)(CLKComplicationTimelineEntry *__nullable))handler {
    // 用handler傳入參數entries來處理目前的表盤資訊
    if (complication.family == CLKComplicationFamilyUtilitarianLarge) {
        //把建立表盤的内容提取成一個方法(templateForUtilitarianLarge)
        CLKComplicationTimelineEntry *entries = [CLKComplicationTimelineEntry entryWithDate:[NSDate date] complicationTemplate:[self templateForUtilitarianLarge]];
        handler(entries);
    } else {
        handler(nil);
    }
}
           

建立一個CLKComplicationTimelineEntry類型的變量

- (CLKComplicationTemplate *) templateForUtilitarianLarge {
    CLKComplicationTemplateUtilitarianLargeFlat *template = [[CLKComplicationTemplateUtilitarianLargeFlat alloc]init];
    //判斷是否登入的方法,類方法,詳情如下
    if ([User sharedUser].isLocalLgin) {
        //在AppleWatch上進行網絡請求可能不是那麼快速,是以在iOS上把資料存入NSUserDefault,這裡取出會更好
        NSString *thisDayString = [[NSUserDefaults standardUserDefaults] objectForKey:@"thisDayString"];
        if(thisDayString==nil){
            template.textProvider = [CLKSimpleTextProvider textProviderWithText:@"正擷取資料"];
        }else{
            template.textProvider = [CLKSimpleTextProvider textProviderWithText:[NSString stringWithFormat:@"今日用電:%@ 度", thisDayString]];
        }
    } else {
        template.textProvider = [CLKSimpleTextProvider textProviderWithText:@"點我登入"];
    }
    return template;
}
           

這個方法放在User類中,标記了dispatch_once_t是以隻會被運作一次,是以作為類方法來擷取一個單例了的變量,在這個方法中做一個網絡的互動,和網絡請求擷取登入情況,來判斷是否登入

+ (instancetype)sharedUser {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _user = [[self alloc] init];
        _user.userInfoDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"privateKey"];
        [_user registListenForLogin];
    });
    return _user;
}
           

五、關于流程

以上就完成了一個簡單的基于iOSapp的Watch OS 表盤元件的建立