iOS project function module package SDK usage summary

1. Function module SDK packaging steps:

1. Create IOS Framework project

First, we need to create an iOS CocoaTouch project, click Next, and enter the name of our Framework. Below we temporarily name the framework "CreateLoginSDKFramework". The operation is as follows:

Xcode -> File\New\Project - > iOS\Framework and Library\Cocoa Touch Framework -> Name the project project -> Save directory

 

2. Set the compatible version:

After creating the project, we need to select "Deployment Target", here we choose 8.0. That is to say, the iOS system version supported by the SDK we packaged here is iOS8.0+. The operation is shown as:

Xcode - > Project - > Targets -> General -> Deployment Info -> ios8.0 (minimum supported compatible version)

 

3. Configure the static library:

Since the created framework is a dynamic library by default, we have to set the Mach-O Type to the static library "Static Library". The operation is as follows:

Xcode ——》Project ——》Targets ——》Build Settings ——》Mach-O Type——》Static Library

 

4. Import the source code file and compile it:

The SDK source code prepared in advance is introduced into our Framework project for compilation. Before compiling, we need to select the files that SDK users can see. Set in the Headers under Build Phases. Put the header files that users can see in Public, and those that users can't see in Project. The operation is as follows:

Xcode ——》Project ——》Targets ——》Build Phases ——》Headers——》Public/Private/Project

 

5. Compile the project:

After setting and configuring, we need to compile our Framework project . First select the simulator to compile, and then select the real machine to compile. After compiling, the corresponding Framework will be generated under Products, and then you can view it through Show in Finder. When viewing, if you want to see the "emulator" and "real machine" frameworks, after Show in finder, you need to go to the upper folder to view.

 

6. Merge the Framework library :

Because compiling under the simulator will generate the Framework used under the simulator, and compiling under the real machine will generate the Framework used by the real machine. If we want to generate a Framework that can be used on both the real machine and the simulator, then we need to merge the two Frameworks. Use the terminal command to merge the above two files. The following is the execution command to merge the above two files. After executing the following command, a new merged file will be generated. The operation is as follows:

Open the terminal - > lipo -create simulator framework path real machine framework path - output new file

 

7. Notes:

1. Before compiling the Framework project, you need to set the Edit Scheme, select run->change the Debug mode to Release mode, and select Close.

2. An error occurs when the Framework is merged : can't map input file: xxxFramework.framework/ (Invalid argument), the reason is that if the project name is different from the Target name of the Framework, you need to customize the FrameworkName.

lipo -info xxxFramework.framework/xxxFramework 或者

cd xxxFramework.framework

lipo -info xxxFramework

The complete command is as follows:

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

 

 

2. Bundle packaging steps for resource files:

1. Creation of Bundle project:

First create a Bundle project like a Framework project, because there is no Bundle type project under the iOS project, so we need to create our Bundle project under OS X -> Framework & Library -> Bundle. The operation is as follows:

Xcode - > File\New\Project - > macOS\Framework and Library\Bundle -> Name the project project -> Save the directory

 

2. Configure the Bundle project:

创建完Bundle工程后,我们要对其进行相应的配置。因为我们是选择OS X创建的Bundle,默认的Bundle是不能在iOS中使用的,所以我们得将Base SDK进行设置,选择相应的iOS版本即可,如下所示。选择完Base SDK后,我们还要像上面Framework的封装一样,设置一下要兼容的iOS版本(iOS Deployment Target)。操作如下所示:

A、Xcode ——》Project ——》Targets ——》Build Settings ——》Base SDK ——》ios8.0

B、Xcode ——》Project ——》Targets ——》General ——》Deployment Info ——》ios8.0(最低支持的兼容版本)

 

3、导入资源文件进行编译:

进行上述配置完后,接下来就是引入资源文件进行编译了,下方引入的资源文件就是我们的LoginSDK.storyboard。引入资源后,进行编译,编译后会在Products下面生成相应的Bundle资源文件,该文件就可以和我们的Framework进行使用了。

 

4、项目中Bundle资源的加载:

生成完Bundle资源文件后,我们在SDK的源代码中,要从Bundle资源文件中进行资源的加载。下方代码就是加载相应Bundle的代码。通过下方的宏定义,就可以通过“Bundle”的名字来加载Bundle。

#define LOGIN_SDK_BUNDLE_NAME   @"LoginSDKResource.bundle"

#define LOGIN_SDK_BUNDLE_PATH   [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: LOGIN_SDK_BUNDLE_NAME]

#define LOGIN_SDK_BUNDLE        [NSBundle bundleWithPath: LOGIN_SDK_BUNDLE_PATH]

 

5、注意事项:

如果Bundle工程中中引用了资源文件,工程编译过后会出现.storyboardc或者.xibc文件才算真正封装成功。

 

三、封装后的SDK文件的使用步骤:

1、导入SDK,进行路径配置

导入SDK到我们的App工程后,我们要对其进行相应的配置。首先我们要对Framework Search Paths进行配置,也就是说告诉编译器我们的第三方SDK所在的位置。下方这个配置项在引入SDK后就默认存在的,如果没有的话就进行配置即可。操作如下所示:

Xcode ——》Project ——》Targets ——》Build Settings ——》Search Paths\Framework Search Paths ——》$(PROJECT_DIR)/LoginSDK

 

2、进行编译配置

配置完路径后,接下来我们要在Other Linker Flags添加上-Objc和-all_load选项。-Objc这个flag告诉链接器把库中定义的Objective-C类和Category都加载进来。而-all_load会强制链接器把目标文件都加载进来,即使没有objc代码。操作如下所示:

Xcode ——》Project ——》Targets ——》Build Settings ——》Linking\Other Link Flags——》-Objc和-all_load

 

3、SDK的使用:

配置完毕后,接下来就是在我们App中使用该SDK了。下方代码就是我们上述LoginSDK的使用方式,首先获取单例,然后检查是否登录,登录成功后根据Block回调跳转到首页,如果未登录,就通过LoginAPI获取登录页面进行登录。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324937995&siteId=291194637