Core Text Font Manager failing to register font with certain scope throwing undocumented error code -2

1k views Asked by At

I am downloading certain font file and storing it in documents directory. After downloading, I try to register this font file using following code:

@objc func registerAndSetFontTapped(_ sender: UIButton) {
  var error: Unmanaged<CFError>?
  let fontURL = fontsFolder.appendingPathComponent("Muli-Bold.ttf")

  if CTFontManagerRegisterFontsForURL(fontURL as CFURL, .user, &error) {
    self.showFontListVC()
  }else if let thisError = error?.takeRetainedValue() {
    print(thisError)
    if let fontError = CTFontManagerError(rawValue: CFErrorGetCode(thisError)) {
      print(fontError.rawValue)
    }
  }
}

This code prints error in console as follows:

Error Domain=com.apple.CoreText.CTFontManagerErrorDomain Code=-2 "Could not register the font file(s)" UserInfo={NSDescription=Could not register the font file(s), CTFontManagerErrorFontURLs=(
    "file:///Users/rohansanap/Library/Developer/CoreSimulator/Devices/8A875331-5ADB-4DD0-986F-074338F96CC8/data/Containers/Data/Application/035078D5-3E92-4917-99E3-A20A61433F87/Documents/fonts/Muli-Bold.ttf"
)}
-2

I am getting same error for scope .persistent as well. This above code successfully registers font for only two scope, viz., .none and .process.

Also, I am trying to find what error is this but according to documentation of CTFontManagerError, no case corresponds to rawValue -2. What is exactly happening here?

My end goal is to register a font and keep it registered across app launches.

0

There are 0 answers