Search code examples
iosobjective-cios-frameworks

Creating iOS framework with Objective-C


I have some trouble creating a framework for iOS.

I created a new project (called Logger) and selected the Framework option. Then, Xcode created the folder structure and it had a few files including Logger.h, but no Logger.m was created.

Then, I have created new Cocoa Touch file called AppLogger and added some basic logic to it.

I imported the AppLoger.h file in my Logger.h like this:

#import <Logger/AppLoger>

I also tried it like this:

#import "Logger/AppLoger"

But it gives me an error:

'Logger/AppLogger.h' file not found

I don't understand what the problem is with my setup. Attached is also an image of my project structure.

enter image description here


Solution

  • I just went through this myself a few days ago. The header files that you add to the main framework header are meant to be the public headers of your framework. This means you need to mark the header as being public (versus project or private).

    The following screenshot shows what you need to do:

    enter image description here

    1. Select the header that should be public.
    2. Ensure the Inspectors pane is being shown on the right side of Xcode.
    3. Select the File Inspector tab.
    4. Under Target Membership, ensure the header is marked as Public. It defaults to Project.

    With those steps complete you can add the #import <Logger/AppLogger.h> line to the Logger.h file.


    For other .h files in the project, you need to follow the same steps but select "Project" (not Public or Private). And use a "normal" import. For example, in AppLogger.m you might need to import BaseLog.h. Use:

    #import "BaseLog.h"
    

    after setting BaseLog.h was a target membership of "Project".