The latest tutorial on making framework for iOS

Table of contents

Table of contents

Table of contents

1. Development environment

2. Framework production steps

1. Create a framework project

2. Add the files that need to be packaged

​edit

3. Configure project parameters

1. Set Mach-O Type to Static Library

 2. Set Dead Code Stripping to NO 

3. Set Link With Standard Libraries to NO

4. Set Excluded Architectures

4. Create Aggregate Target

1. Select Target, click + in the lower left corner

2. Select Other---Aggregate

3. Add script

4. The script is as follows:

5. Generate framework

 3. Test Framework

1. Create a test project

2. Add test code

4. Test Demo in the article 



foreword

         Recently, there is such a need: to package your own code into a framework for other languages ​​to call. I checked the information on the Internet and referred to many articles.


提示:以下是本篇文章正文内容,下面案例可供参考

1. Development environment

Xcode 14.3.1

MacOS: 13.5 (22G74)

2. Framework production steps

1. Create a framework project

        Taking HelloWorld as an example, the steps are as follows:

        Select File-project-frame in turn, give the project a name, the screenshot is as follows: 

                                Figure 1. Select framework as the project type

Figure 2. Two mandatory options for creating a project 

        It should be noted here that there are two options when creating a project: Include Test and Include Documentation, one is to write the test unit code, and the other is the framework documentation, which I did not check here.

        After the project is created, the project code structure is as follows:   

           

Figure 3. Project project directory

2. Add the files that need to be packaged

        Here I created a HelloWorldTools file, mainly for demonstration purposes, the code is as follows:

        Figure 4. HelloWorldTools.h

        Figure 5. HelloWorldTools.m file

        Select the Target of the project, and move the header file just now to public, refer to the figure below for details

 Figure 6. Moving files

        Then import the file into the HelloWorldFramework.h generated when creating the framework, as follows:

        Figure 7. Import header files that need to be exposed 


3. Configure project parameters

1. Set Mach-O Type to Static Library

Figure 8. Setting Mach-O Type

 2. Set Dead Code Stripping to NO 

 Figure 9. Setting Dead Code Stripping

3. Set Link With Standard Libraries to NO

 Figure 10. Setting Dead Code Stripping

4. Set Excluded Architectures

4. Set the minimum version number supported by the Framework 

Figure 10. Setting the minimum version number supported by the framework

4. Create Aggregate Target

1. Select Target, click + in the lower left corner

        Note here that you cannot see the Aggregate option when you select Project.

        Figure 11. Add Aggregate

2. Select Other---Aggregate

  Figure 12. Add Aggregate

3. Add script

        The function of this script is to automatically merge the frameworks of the real machine and the simulator.

 Figure 13. Add Run Script script

Paste the following script into Script Phase:

                 Figure 14. Add Run Script script

4. The script is as follows:

# Type a script or drag a script file from your workspace to insert its path.

    if [ "${ACTION}" = "build" ]

then

INSTALL_DIR=${SRCROOT}/Products/${PROJECT_NAME}.framework

DEVICE_DIR=${BUILD_ROOT}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework

SIMULATOR_DIR=${BUILD_ROOT}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework

if [ -d "${INSTALL_DIR}" ]

then

rm -rf "${INSTALL_DIR}"

fi

mkdir -p "${INSTALL_DIR}"

cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"

#ditto "${DEVICE_DIR}/Headers" "${INSTALL_DIR}/Headers"

lipo -create "${DEVICE_DIR}/${PROJECT_NAME}" "${SIMULATOR_DIR}/${PROJECT_NAME}" -output "${INSTALL_DIR}/${PROJECT_NAME}"

#open "${DEVICE_DIR}"

open "${SRCROOT}/Products"

fi

5. Generate framework

1. Select target and Aggregate respectively, and change Build Configuration to release.

15. Modify the build configuration of Target to release

 16. Modify the build configuration of Target to release

  17. Modify the build configuration of Target to release

2. Compile and generate framework

       1. Select Target, then select the simulator and the real device (or Any iOS Device) respectively, Command+B to compile:

18. Compile HelloWorldFramework

       2. Select Aggregate, then select Any iOS Device respectively, and compile with Command+B. After the compilation is successful, the system will automatically open the framework directory, as shown in the figure below:

19. Compile Aggregate

 3. Test Framework

1. Create a test project

        Create a test project named TestHelloWorldDemo to test the framework made above. Then drag the framework created above into the project.

20. Create a test project and drag it into the framework

2. Add test code

        Import the framework, add test code, and find that the console prints out Hello World!, which is successful.

Figure 21. Test code

4. Test Demo in the article 

       The example code above is a blog written with OC as an example. The steps to create a framework in Swift are the same as OC. The difference is that the header files that Swift needs to expose need to add the public keyword. I have passed it to the CSDN space for the specific project code. Download it and run it.

The demo address         in the article is as follows: demos address link

Guess you like

Origin blog.csdn.net/ZCC361571217/article/details/131912328