我从Watch Face启动我的应用程序有简单的复杂功能。它不显示任何数据,只显示图像。我跟着Adding Complications to the Gallery指南,但我无法将我的并发症添加到iPhone Watch app中的Complications Gallery。
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"
}
项目设置。
故障排除。
gallery.ckcomplication
gallery.ckcomplication
位于根级别的项目中。与.xcodeproj
相同的级别。更新
我添加了更多代码。我还将ckcomplication.strings
添加到Watch Extension Target。
再次阅读Apple指南,后续行让我思考:
例如,MyBundleName.ckcomplication。
也许我们假设使用包名称重命名生成的文件夹名称 gallery 。我试过这样做。到目前为止没有运气。
答案 0 :(得分:3)
我不确定你的方法的其余部分是什么样的,但是在确定我为我们支持的所有复杂类型添加了足够的信息之前,我遇到了同样的问题。然后它生成了适当的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);
}
这很hacky,我认为正确的解决方案是提供真实的图像(和文本)数据,但这似乎有效。
答案 1 :(得分:1)
我再次关注this video并注意到ComplicationController
在目标偏好设置中有前缀$(PRODUCT_MODULE_NAME)
:
我的项目没有这个前缀,我很快发现ComplicationController方法从未调用过。因为那是我们设置tintColor
时的位置,所以显示了默认的复杂功能。
我通过删除手表应用并观看扩展目标并从头创建新内容来解决这个问题。前缀出现在Xcode中,我可以在模拟器中运行的Complications Gallery中看到我的应用程序图标。
答案 2 :(得分:0)
就我而言,在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>