iOS 在 WidgetKit 小部件扩展中动态加载的自定义字体在真实设备上不起作用?(模拟器没问题)

roh*_*tst 6 ios swift widgetkit

<TLDR;> ; 尝试在小部件中通过 CTFontManagerRegisterFontsForURL 使用下载的字体在真实设备上不起作用。(适用于模拟器)。看起来注册字体可以,但是加载字体时出现权限问题

error   14:39:07.672799-0800    chronod [com.x.y.widgetx:widgetx:small:1536744920620481560@148.0/148.0/20.2] reload: could not decode view
error   14:39:07.674984-0800    kernel  Sandbox: chronod(2128) deny(1) file-read-metadata /private/var/mobile/Containers/Shared/AppGroup/9B524570-1765-4C24-9E0C-15BC3982F0DC/downloadedFonts/Chewy/Chewy-Regular.ttf
error   14:39:07.675762-0800    kernel  Sandbox: chronod(2128) deny(1) file-read-data /private/var/mobile/Containers/Shared/AppGroup/9B524570-1765-4C24-9E0C-15BC3982F0DC/downloadedFonts/Chewy/Chewy-Regular.ttf
Run Code Online (Sandbox Code Playgroud)

</TLDR;>

项目结构是:App目标+小部件扩展+小部件意图扩展

所有共享一个公共应用程序组 group.com.xyz并且所有文件处理都是使用

FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.x.y.z")
Run Code Online (Sandbox Code Playgroud)

这样就只使用共享容器。

使用主应用程序目标,下载字体“Chewy-Regular.ttf”并将其保存到共享 AppGroup 容器中。

现在可以通过以下方式加载字体:

CTFontManagerRegisterFontsForURL()
Run Code Online (Sandbox Code Playgroud)

并显示在主应用程序文本视图中:

Text("Testing...").font(Font.custom("Chewy-Regular", size: 20))
Run Code Online (Sandbox Code Playgroud)

现在添加一个使用此字体的 Widgetkit 小部件实例。

在 IntentTimelineProvider 的 getTimeLine() 和 getSnapShot() 中,我们通过 CTFontManagerRegisterFontsForURL 再次加载字体(这可能需要再次发生,因为小部件在与主应用程序不同的进程中运行?)。

在模拟器上,小部件将显示正确的字体。但在 iPhone7 真实设备上,小部件将显示“编辑的占位符视图”。

我在设备控制台中看到:

error   14:39:07.567120-0800    chronod No configuration found for configured widget identifier: D9BF75EE-4A04-441A-8C85-1507F7ECE379
fault   14:39:07.625600-0800    widgetxExtension    -[EXSwiftUI_Subsystem beginUsing:withBundle:] unexpectedly called multiple times.
error   14:39:07.672733-0800    chronod Encountered an error reading the view archive for <private>; error: <private>
error   14:39:07.672799-0800    chronod [com.x.y.widgetx:widgetx:small:1536744920620481560@148.0/148.0/20.2] reload: could not decode view
error   14:39:07.674984-0800    kernel  Sandbox: chronod(2128) deny(1) file-read-metadata /private/var/mobile/Containers/Shared/AppGroup/9B524570-1765-4C24-9E0C-15BC3982F0DC/downloadedFonts/Chewy/Chewy-Regular.ttf
error   14:39:07.675762-0800    kernel  Sandbox: chronod(2128) deny(1) file-read-data /private/var/mobile/Containers/Shared/AppGroup/9B524570-1765-4C24-9E0C-15BC3982F0DC/downloadedFonts/Chewy/Chewy-Regular.ttf
error   14:39:07.708914-0800    chronod [u 8D2C83B3-A6CB-432E-A9D4-9BC8F7056B10:m (null)] [<private>(<private>)] Connection to plugin invalidated while in use.
fault   14:39:07.710284-0800    widgetxExtension    -[EXSwiftUI_Subsystem beginUsing:withBundle:] unexpectedly called multiple times.
error   14:39:07.803468-0800    chronod Encountered an error reading the view archive for <private>; error: <private>
Run Code Online (Sandbox Code Playgroud)

看来是权限问题,textview在widget渲染时无法访问它需要的字体文件。

笔记:

  1. 字体肯定已注册,因为我可以在其中看到它们

    for fontFamily in UIFont.familyNames { for fontName in UIFont.fontNames(forFamilyName: fontFamily) { print(fontName) ...

在主应用程序目标和小部件扩展目标中

  1. 如果我将字体作为应用程序包的一部分并添加到“应用程序提供的字体”中,则它们在模拟器和 iPhone 7 真实设备上的主应用程序和小部件中加载得绝对正确。

  2. 我有时确实在小部件扩展目标日志中看到此错误,不知道它是否相关。

    widgetxExtension[1385:254599] [用户默认值] 无法读取 CFPrefsPlistSource<0x28375b880> 中的值(域:group.xyz,用户:kCFPreferencesAnyUser,ByHost:是,容器:(null),内容需要刷新:是):使用 kCFPreferencesAnyUser仅允许系统容器使用容器,与 cfprefsd 分离

  3. 我怀疑与应用程序组有关,因此我尝试将字体复制到小部件扩展容器中,并使用 FileManager.default() 从那里加载,但得到了相同的结果。

请帮忙!谢谢。