复杂功能未出现在图库中

Complication not appears in Gallery

我在从 Watch Face 启动我的应用程序时遇到了一些简单的问题。它不显示任何数据,只显示图像。我遵循了 Adding Complications to the Gallery 指南,但无法将我的复杂功能添加到 iPhone Watch 应用程序中的复杂功能库。

public func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {

    guard let tempalte = buildTemplate(for: complication) else {
        fatalError("Unsuported Complication Family.")
    }
    handler(tempalte)
}

// Helper function to build template.

fileprivate func buildTemplate(for complication: CLKComplication) -> CLKComplicationTemplate? {

    let textProvider = CLKSimpleTextProvider.localizableTextProvider(withStringsFileTextKey: "Caffeine")

    switch complication.family {
    case .modularSmall:

        let icon = #imageLiteral(resourceName: "Complication/Modular")

        let complicationTemplate = CLKComplicationTemplateModularSmallSimpleImage()
        complicationTemplate.imageProvider = CLKImageProvider(onePieceImage: icon)

        return complicationTemplate

    case .circularSmall:

        let icon = #imageLiteral(resourceName: "Complication/Circular")

        let complicationTemplate = CLKComplicationTemplateCircularSmallSimpleImage()
        complicationTemplate.imageProvider = CLKImageProvider(onePieceImage: icon)
        return complicationTemplate

    case .utilitarianSmall:

        let icon = #imageLiteral(resourceName: "Complication/Utilitarian")

        let complicationTemplate = CLKComplicationTemplateUtilitarianSmallSquare()
        complicationTemplate.imageProvider = CLKImageProvider(onePieceImage: icon)
        return complicationTemplate

    case .utilitarianLarge:

        let icon = #imageLiteral(resourceName: "Complication/Utilitarian")

        let complicationTemplate = CLKComplicationTemplateUtilitarianLargeFlat()
        complicationTemplate.imageProvider = CLKImageProvider(onePieceImage: icon)
        complicationTemplate.textProvider = textProvider
        return complicationTemplate

    case .utilitarianSmallFlat:

        let icon = #imageLiteral(resourceName: "Complication/Utilitarian")

        let complicationTemplate = CLKComplicationTemplateUtilitarianSmallFlat()
        complicationTemplate.imageProvider = CLKImageProvider(onePieceImage: icon)
        complicationTemplate.textProvider = textProvider
        return complicationTemplate

    case .extraLarge:

        let icon = #imageLiteral(resourceName: "Complication/Extra Large")

        let complicationTemplate = CLKComplicationTemplateExtraLargeSimpleImage()
        complicationTemplate.imageProvider = CLKImageProvider(onePieceImage: icon)
        return complicationTemplate

    default:
        return nil
    }
}

complicationManifest.json 文件.

{
  "supported complication families" : {
    "3" : "B82ADE2C-045D-491D-87DD-C24148804480.json",
    "6" : "A332202A-4B7B-49E0-83FB-DB398089E4FE.json",
    "4" : "817476CB-1683-4604-A76D-2977236448AD.json",
    "2" : "50AB27ED-0FA3-41AD-A3E7-CC9C62E48D82.json",
    "0" : "DBE31877-7BB0-4671-B065-6FE00333929E.json",
    "7" : "96966B70-AB8D-4DB3-AB64-AFDF1F09EB88.json"
  },
  "client ID" : "net.borisy.ProductDisplayName.watchkitapp.watchkitextension"
}

项目设置。

疑难解答。

更新.

我添加了更多代码。我还添加了 ckcomplication.strings 到 Watch Extension Target。

再次阅读 Apple 指南并看到以下内容让我开始思考:

For example, MyBundleName.ckcomplication.

也许我们假设将生成的文件夹名称 gallery 重命名为包名称。我试过了。到目前为止没有运气。

我不确定你的方法的其余部分是什么样子,但我遇到了同样的问题,直到我确定我为我们支持的所有并发症类型添加了足够的信息。然后生成适当的 json 和图像文件。

使用空白文本提供程序(并且模板图像与复杂图像集匹配)似乎很愚蠢,但这似乎是一种解决方法。

- (void)getLocalizableSampleTemplateForComplication:(CLKComplication *)complication
                                        withHandler:(void(^)(CLKComplicationTemplate *complicationTemplate))handler {
// This method will be called once per supported complication,
// and the results will be cached
  UIImage *templateImage = [UIImage imageNamed:@"template"];
  CLKImageProvider *image =
      [CLKImageProvider imageProviderWithOnePieceImage:templateImage];
  CLKTextProvider *text = [CLKTextProvider textProviderWithFormat:@""];
  CLKComplicationTemplate *complicationTemplate;

  switch (complication.family) {
    case CLKComplicationFamilyCircularSmall: {
      CLKComplicationTemplateCircularSmallSimpleImage *small =
          [[CLKComplicationTemplateCircularSmallSimpleImage alloc] init];
      small.imageProvider = image;
      complicationTemplate = small;
      break;
    }
    case CLKComplicationFamilyExtraLarge: {
      CLKComplicationTemplateExtraLargeSimpleImage *large =
          [[CLKComplicationTemplateExtraLargeSimpleImage alloc] init];
      large.imageProvider = image;
      complicationTemplate = large;
      break;
    }
    case CLKComplicationFamilyModularLarge: {
      // Unexpected complication type.
      break;
    }
    case CLKComplicationFamilyModularSmall: {
      CLKComplicationTemplateModularSmallSimpleImage *small =
          [[CLKComplicationTemplateModularSmallSimpleImage alloc] init];
      small.imageProvider = image;
      complicationTemplate = small;
      break;
    }
    case CLKComplicationFamilyUtilitarianLarge: {
      CLKComplicationTemplateUtilitarianLargeFlat *large =
          [[CLKComplicationTemplateUtilitarianLargeFlat alloc] init];
      large.imageProvider = image;
      large.textProvider = text;
      complicationTemplate = large;
      break;
    }
    case CLKComplicationFamilyUtilitarianSmall: {
      CLKComplicationTemplateUtilitarianSmallSquare *small =
          [[CLKComplicationTemplateUtilitarianSmallSquare alloc] init];
      small.imageProvider = image;
      complicationTemplate = small;
      break;
    }
    case CLKComplicationFamilyUtilitarianSmallFlat: {
      CLKComplicationTemplateUtilitarianSmallFlat *small =
          [[CLKComplicationTemplateUtilitarianSmallFlat alloc] init];
      small.imageProvider = image;
      small.textProvider = text;
      complicationTemplate = small;
      break;
    }
  }
  handler(complicationTemplate);
}

这很老套,我认为正确的解决方案是提供真实的图像(和文本)数据,但这似乎可行。

我再次观看 this video 并注意到 ComplicationController 在目标首选项中有前缀 $(PRODUCT_MODULE_NAME)

我的项目没有这个前缀,我很快发现从未调用过 ComplicationController 方法。因为那是我们设置tintColor的地方,默认显示的是complication

我通过删除我的手表应用程序和手表扩展目标并从头开始创建新的解决了这个问题。前缀出现在 Xcode 中,我可以在模拟器的并发症库 运行 中看到我的应用程序图标。

就我而言,在 2020 年,watchOS7 上的并发症很好,但我的应用程序也支持 WatchOS5,但并发症不存在,所以我将其添加到手表扩展 info.plist 文件中:

<key>CLKComplicationPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).ComplicationController</string>
<key>CLKComplicationSupportedFamilies</key>
<array>
    <string>CLKComplicationFamilyModularSmall</string>
    <string>CLKComplicationFamilyModularLarge</string>
    <string>CLKComplicationFamilyUtilitarianSmall</string>
    <string>CLKComplicationFamilyUtilitarianSmallFlat</string>
    <string>CLKComplicationFamilyUtilitarianLarge</string>
    <string>CLKComplicationFamilyCircularSmall</string>
    <string>CLKComplicationFamilyExtraLarge</string>
</array>