简体   繁体   中英

WatchOS4 display complication for a second then replace it by blank display

I'm a beginner in iOS programming. I'm trying to make a sample display on the complication on the apple watch but it only shows for a second and becomes blank after.

Here is my code in the ComplicationController

func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
    // This method will be called once per supported complication, and the results will be cached
    var template: CLKComplicationTemplate?
    switch complication.family {
    case .modularSmall:
        let modularSmallTemplate =
            CLKComplicationTemplateModularSmallRingText()
        modularSmallTemplate.textProvider =
            CLKSimpleTextProvider(text: "R")
        modularSmallTemplate.fillFraction = 0.75
        modularSmallTemplate.ringStyle = CLKComplicationRingStyle.closed
        template = modularSmallTemplate
    case .modularLarge:
        let modularLargeTemplate =
            CLKComplicationTemplateModularLargeStandardBody()
        modularLargeTemplate.headerTextProvider = CLKSimpleTextProvider(text: "Sample title", shortText: "Title")
        modularLargeTemplate.body1TextProvider =
            CLKSimpleTextProvider(text: "Sample Text",
                                  shortText: "Text")
        modularLargeTemplate.body2TextProvider =
            CLKSimpleTextProvider(text: "Tap to open",
                                  shortText: "Tap")
        template = modularLargeTemplate
    case .utilitarianSmall:
        template = nil
    case .utilitarianLarge:
        template = nil
    case .circularSmall:
        template = nil

    case .utilitarianSmallFlat:
        template = nil

    case .extraLarge:
        template = nil
    }
    handler(template)

}

I've also read an answer about this question to manage also in the func getTimelineEntries, I've tried to paste the same code as on the template but still the result is the same, it display blank.

Can someone help me please? Thank you for your time.

The code you have only provides a sample template for the complication. This is only used in the list of complications, hence being referred to as a sample. If you want to display data once your complication is selected you need to utilize the getCurrentTimelineEntry function.

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
     // Example Switch Statement
     switch complication.family {
    case .modularSmall:
        let template = CLKComplicationTemplateModularSmallSimpleImage()
        template.imageProvider.tintColor = UIColor.color(fromHexString: "#81BE41")
        template.imageProvider = CLKImageProvider(onePieceImage: backImage, twoPieceImageBackground: backImage, twoPieceImageForeground: transImage)
        handler(template)
    case .utilitarianLarge:
        let template = CLKComplicationTemplateUtilitarianLargeFlat()

        template.textProvider = CLKSimpleTextProvider(text: "Header Text")

        handler(template)

    case .modularLarge:
        let template = CLKComplicationTemplateModularLargeStandardBody()
        template.headerTextProvider = CLKSimpleTextProvider(text: "Header Text")
        template.body1TextProvider = CLKSimpleTextProvider(text: "----", shortText: "----")
        template.body2TextProvider = CLKSimpleTextProvider(text: "----", shortText: "----")

        handler(template)
    default:
        handler(nil)
    }
}

Also make sure that you are calling getTimelineStartDate() and getTimeLineEndDate()

func getTimelineStartDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {

    let date = Calendar.current.startOfDay(for: Date())
    handler(date)
}

func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {

    var  date = Calendar.current.startOfDay(for: Date())
    date = Calendar.current.date(byAdding: .day, value: 2, to: date)!
    handler(date)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM