title: iOS - Download Dynamic Fonts
body: <p>I am trying to download fonts for my app dynamically, as I will frequently want to add new ones on the fly and don't want to re-submit my app each time.</p>
  
  <p>I have followed some other guides attempting to download the font file from a URL, save it to the app's Documents Directory, then load it using <strong>CGFontCreateWithDataProvider &amp; CTFontManagerRegisterGraphicsFont.</strong></p>
  
  <p><strong>However, the app keeps failing the 'writeToFile' function and spits out this line:</strong></p>
  
  <pre><code>println("Failed to save font: \(fontFile)")
  </code></pre>
  
  <p>I have verified that the 'fontUrl' is valid (copy and pasted from debugger right into my desktop browser).</p>
  
  <p>Also, here is what the value of 'fontPath' ends up being in the debugger and it looks right to me:</p>
  
  <pre><code>fontPath = "/Users/James/Library/Developer/CoreSimulator/Devices/E153E1F4-E3FD-4BCE-A40F-433EADF14591/data/Containers/Data/Application/969A248C-A7B3-4768-86E4-68A6C4E20ECF/Documents/Fonts/american_typewriter_medium_bt.ttf"
  </code></pre>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>// Download and save font file
          let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
          let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
          if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) {
              if paths.count &gt; 0 {
                  if let documentsDirectory = paths[0] as? String {
                      let fontPath = documentsDirectory.stringByAppendingPathComponent("Fonts/"+fontFile) as String
                      let fontUrl = NSURL(string: ApiUrlString + "fonts/" + fontFile)
                      if let fontData = NSData(contentsOfURL: fontUrl!)
                      {
                          if(!fontData.writeToFile(fontPath, atomically: true))
                          {
                              println("Failed to save font: \(fontFile)")
                              return
                          }
                      }
                      else
                      {
                          println("Failed to download font from URL: \(fontUrl)")
                          return
                      }
  
                      // Load Font
                      let data: NSData? = NSFileManager.defaultManager().contentsAtPath(fontPath)
                      if (data == nil)
                      {
                          println("Failed to load saved font: \(fontFile)")
                          return
                      }
  
                      var error: Unmanaged&lt;CFError&gt;?
                      let provider: CGDataProviderRef = CGDataProviderCreateWithCFData(data)
                      let font: CGFontRef = CGFontCreateWithDataProvider(provider)
                      if (!CTFontManagerRegisterGraphicsFont(font, &amp;error))
                      {
                          println("Failed to register font, error: \(error)")
                          return
                      }
  
                      println("Successfully saved and registered font: \(fontFile)")
                  }
              }
          }
          else
          {
              println("Failed to load documents directory: \(fontFile)")
          }
  </code></pre>