4

My project structure is in such a way that I have 2 projects named as ProjectA and ProjectB. Now for these 2 projects, I have created a framework named as ProjFramework.

I have added common files of these 2 projects inside the framework and then added framework in the projects separately. Till now I am able to access all the files and vars. Now I added the images also in the framework and when I am trying to display them in UI then I am getting an error saying unable to read this .png.

As I know I need to include the framework somehow but I don't know how. Please help me in sharing common images b/w 2 XCode projects.

Thanks in advance.

arunjos007
  • 4,105
  • 1
  • 28
  • 43

2 Answers2

5

An alternative to @arunjos007' method is to (a) create a bundle of your images, then (b) retrieve from the bundle.

Let's say you have a Framework called Kernel, and you wish to access two types of files - cikernel and png.

Creating a bundle:

  • Move all your files/images into a new folder on your desktop. Name it whatever you wish. In my example I named them cikernels and images.

  • Rename your folders, with a .bundle extension. In my example they became cikernels.bundle and images.bundle. You will see the warning below... choose "Add".

Bundle warning

  • Drag the bundle into your framework project. You can expand the bundle to see the contents. Also, you can add/delete/edit the contents of the bundle.

Retrieving files from the bundle:

I've created two public functions, one to retrieve files and one to retrieve images. They are pretty much the same, except for (a) the return type and (b) error handling. (I probably should add some to the UIImage function - but since I have total control on the code - it's not going to be used by anyone else - it's not critical.)

public func returnImage(_ named:String) -> UIImage {
    let myBundle = Bundle.init(identifier: "com.companyName.frameworkName")
    let imagePath = (myBundle?.path(forResource: "images", ofType: "bundle"))! + "/" + named
    let theImage = UIImage(contentsOfFile: imagePath)
    return theImage!
}

public func returnKernel(_ named:String) -> String {
    let myBundle = Bundle.init(identifier: "com.companyName.frameworkName")
    let kernelPath = (myBundle?.path(forResource: "cikernels", ofType: "bundle"))! + "/" + named + ".cikernel"
    do {
        return try String(contentsOfFile: kernelPath)
    }
    catch let error as NSError {
        return error.description
    }
}

One last note: The identifier is defined in the Framework target's General tab. Typically it's in the form com.companyframework*. Change that line to your's.

3

Before Building and distributing your framework, you should copy files that need to carry with your framework.

To copy files, Click your framework project target, go to "Build Phases" tab and you can see a section called "Copy Files". Add your files here(See below screenshot)

enter image description here

Then build your framework project, after a successful build your distributable framework will be generated in your Products folder. It will look like YourFrameworkProjectName.framework.

Add this file to other projects which need your framework.

Note: If you just want to run your project by connecting with your framework project, you can be done it by adding build dependency. See StackOverflow question here

arunjos007
  • 4,105
  • 1
  • 28
  • 43