title: How To Use Shared Container/App Groups To Access Files From Other Targets in Swift
body: <p>I am new to Swift.</p>
  
  <p>I am trying to make a two-app project, where one app contains some data files and the other accesses those files. </p>
  
  <p>The solution, I think, has been to use the App Groups entitlements to allow for this, and access the files through those means. I have been able to follow along with the example here: <a href="https://stackoverflow.com/questions/24015506/communicating-and-persisting-data-between-apps-with-app-groups">Communicating and persisting data between apps with App Groups</a>. In particular, the 2nd answer, which is Swift-ish (maybe an older version I guess?). It does seem to work with the right entitlements. So, now the question is how can I access the file from one app, with it being apart of the another? I'm not familiar with the API's and correct functions that I can use (new to Swift, as I said). </p>
  
  <p>The apps are basic. Setup as single view applications, with everything defaulted except the <code>ViewController</code>'s, entitlements, and one has the test data. The Xcode project structure is:</p>
  
  <pre><code>testingData/
   testingData/
    testingData.entitlements
    TestData/
     testdata.txt
    AppDelegate.swift
    ViewController.swift
    Main.storyboard
    Assets.xcassets
    LaunchScreen.storyboard
    Info.plist
   sharedContainerTest/
    sharedContainerTest.entitlements
    AppDelegate.swift
    ViewController.swift
    Main.storyboard
    Assets.xcassets
    LaunchScreen.storyboard
    Info.plist
   Products/
    testingData.app
    sharedContainerTest.app
  </code></pre>
  
  <p>The entitlements are both the same. They each have App Groups enabled, with the same string: <code>group.com.example.name</code>. On the <code>testingData</code> target, the <code>ViewController</code> has the following chunk in the <code>viewDidLoad</code> function from that example (modified for Swift 4.x):</p>
  
  <pre><code>var userDefaults = UserDefaults(suiteName: "group.com.example.name")!
  userDefaults.set("user12345", forKey: "userId")
  userDefaults.synchronize()
  </code></pre>
  
  <p>On the <code>sharedContainerTest</code> target, its <code>ViewContoller</code> has </p>
  
  <pre><code>var userDefaults = UserDefaults(suiteName: "group.com.example.name")
  if let testUserId = userDefaults?.object(forKey: "userId") as? String {
      print("User Id: \(testUserId)")
  }
  </code></pre>
  
  <p>in its <code>viewDidLoad</code> function. As I said, this seems to work, but now what do I need to add to this to access the <code>testdata.txt</code> file from the <code>testingData</code> app? Does it need to be stored as a variable, perhaps? Or is there a <code>Bundle</code> object that can do this?</p>
  
  <p>If this has been answered elsewhere, please kindly point me to it and I'll take this down. </p>