Composing Text Providers with  CLKTextProvider.​localizableTextProvider(​withStringsFileFormatKey:, textProviders:)

Are there any official examples of setting up a complication using localizableTextProvider(withStringsFileFormatKey:, textProviders:)? I can get the text provider to populate when producing a SampleTemplate, but whenever I try to generate a template using getTimelineEntries the text provider generated by localizableTextProvider the result is always blank, no text.


Example (only supporting .utilitarianLarge):

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
        // Call the handler with the current timeline entry
        let template = CLKComplicationTemplateUtilitarianLargeFlat()
        
        template.textProvider = CLKTextProvider.localizableTextProvider(
            withStringsFileFormatKey: "testComplication",
            textProviders: [
                CLKSimpleTextProvider(text: "Hello"),
                CLKSimpleTextProvider(text: "World")
            ]
        )
        
        handler(CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template))
        
    }

and sampleTemplate as

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
        switch complication.family {
        case .utilitarianLarge:
            let template = CLKComplicationTemplateUtilitarianLargeFlat()
            
            template.textProvider = CLKTextProvider.localizableTextProvider(
                withStringsFileFormatKey: "testComplication",
                textProviders: [
                    CLKSimpleTextProvider(text: "Hi"),
                    CLKSimpleTextProvider(text: "World")
                ]
            )
            
            handler(template)
            
        default:
            handler(nil)
        }
        
    }


with ckcomplication.strings as

"testComplication" = "%@ %@";


The sample template will always display the text "Hi World", whereas the result from getCurrentTimelineEntry will always display an empty complication.


Has anyone had luck composing text providers in this way?

Answered by emmaf in 354077022

I worked around this with an Objective C category:


https://gist.github.com/SlaunchaMan/815b3e7214ceb2000e64

Accepted Answer

I worked around this with an Objective C category:


https://gist.github.com/SlaunchaMan/815b3e7214ceb2000e64

I fought this issue as well, and came to the conclusion that using CLKTextProvider.localizableTextProvider with the ckcomplication.strings file is only intended for use when called via CLKComplicationDataSource.getLocalizableSampleTemplate(for:withHandler:)

For all other uses, just utilize your normal Localizable.strings

I wish the documentation made it clear where this is expected, and not expected, to be used
Composing Text Providers with  CLKTextProvider.​localizableTextProvider(​withStringsFileFormatKey:, textProviders:)
 
 
Q