IOS创建动态链接库Framework经验

本文详细介绍了如何在iOS8及以上版本创建和使用动态链接库(Framework)。包括创建动态库的步骤、配置Header Search Path和Framework Search Path、添加依赖框架到应用程序,以及动态加载和卸载库的方法。通过示例代码展示了如何在项目中使用动态库,强调了动态库在插件化和模块升级方面的潜力。此外,还讨论了iOS上动态库的限制和可能性。
摘要由CSDN通过智能技术生成

以下几点是我在开发中发现整理的

 

1.对于系统头文件,如<openssl/rsa.h>,文件存放路径和Header Search Path要一起对应:

如:Path配置在$(PROJECT_DIR)/Kingjoy,则存放路径需要Finder中对应此目录,不能放在文件跟目录

 

2.Framework Search Path也一样,配置需要与Finder中的路径一致,而非直接放跟目录(配置跟目录也行)

 

3.libssl.a ,这个静态类库,在alipay的sdk中存在,另外还需要引入SystemConfiguration.framework的类库

 

4.bundle资源文件,如Kingjoy.bundle,存放路径在项目根目录。IOS8打的framework如果有bundle的,直接加入Copy Resource也行。这个路径是否根目录待测试,可能和Framework search path对应的。

 

5.Build静态类库时,选中IOS device会在项目里面的Product中生成.framework或.a文件,但是对应simulator的没看到,找了好久说是否改i386配置,后来发现不是,其实已经声称了,点击Product在Finder中显示,对应的路径的上级路径中会有simulator的文件。

 

6.xcode中创建的group,不会对应Finder中的目录,而需要在Finder中创建好后,在Add file进来。

 

7.新建的项目居然说找不到NSObject类,后来发现有一个Prefix Compile Header 文件,导入通用的h文件即可。

 

8.webview中使用本地图片,把sdkbundle存在了根目录,这个可能与Framework search path有关系。放在根目录能找到相对路径的图片。 

 

以下内容转载自: 

https://kodmunki.wordpress.com/2015/03/04/cocoa-touch-frameworks-for-ios8-remix/

http://foggry.com/blog/2014/06/12/wwdc2014zhi-iosshi-yong-dong-tai-ku/ 

 

Universal Cocoa Touch Frameworks for iOS8 – (Remix)

So, it appears that Apple® has changed their App validation process; fat binaries are no longer passing validation, and, per our support inbox, this is a very real and present problem for a lot of people.

Well, fear not! After some nifty hacking and finagling we have come up with a process that is simple and straight-forward, continues to support universality in development, and archives artifacts that pass validation.

If you implement this method, please, let us know about it in the comments, and, if it is working for you, be sure to flood your social media with links back to this article. No, this is not a shameless attempt to get traffic, though traffic sure is sweet. This is evidently a very real problem for many developers, and it is certain that if they are currently sitting at their keyboard, slamming their face into it because they can’t resolve this issue, they will be thankful you gave them a heads up!

Alright, enough bloviating. Here is a “step by step” of the new process.

Creating a Framework

  1. Fire up Xcode and start a new Cocoa Touch Framework project (Fig. 1)
    CocoaTouchFrameworks_001
    (Figure 1)
  2. Go to the Build Settings of your project target and confirm or set the “Architectures” to “Standard Architectures.” These are arm64 and armv7, and are likely the default. (Fig. 2)
  3. Set “Build Active Architecture” to “NO” – Yes your build times may be a little longer, but this is what is going to allow your Frameworks to be “universal”. So, if you don’t do this, running on your sim will be a problem. Besides, if your builds are debilitatingly slow in a framework because you set “Build Active Architecture” to “NO,” you probably aren’t maximizing modularity. (Fig. 2)
  4. Set or confirm “Valid Architectures” to arm64, armv7 and armv7s. This is likely the default. (Fig. 2)
    CocoaTouchFrameworks_002
    (Figure 2)

     

     

  5. Go to the Build Phases section of your project target and append a new Run Script phase. Paste this script in in it:

    UPDATE: Due to the latest version of Xcode changing its compilation process this script can no longer be placed in the Build Phases, as this causes the script to run too early, not giving the build process enough time to generate the module.modulemap files for your framework. If you previously used this process be sure to follow these new instructions for this step and remove the Build Phase Run Script that this step previously requested.

    NEW INSTRUCTIONS: Add the script below to your project’s build Scheme as an Archive Post-action:

     

    1. Edit your projects build Scheme by selecting “Edit Scheme” from the Scheme drop down.

      EditScheme

       

    2. Add the script below as an Archive Post-action, select your project from the “Provide build settings from” drop down, and click “Close.”

      UpdateScheme

Archive Post-action script

set -e

DEVICE_BIN="${OBJROOT}/UninstalledProducts/${TARGET_NAME}.framework"
SIMULATOR_BIN="${SYMROOT}/../../../../Products/Debug-iphonesimulator/${TARGET_NAME}.framework"

ARCHIVE_PATH="${SRCROOT}/_Archive"
rm -rf "${ARCHIVE_PATH}"
mkdir "${ARCHIVE_PATH}"

if [ "${CONFIGURATION}" = "Release" ]; then

if [ -d "${DEVICE_BIN}" ]; then
DEVICE_PATH="${ARCHIVE_PATH}/Release"
mkdir "${DEVICE_PATH}"
cp -r "${DEVICE_BIN}" "${DEVICE_PATH}"
fi
if [ -d "${SIMULATOR_BIN}" ]; then
SIMULATOR_PATH="${ARCHIVE_PATH}/Debug"
mkdir "${SIMULATOR_PATH}"
cp -r "${DEVICE_BIN}" "${SIMULATOR_PATH}"
lipo -create "${DEVICE_BIN}/${TARGET_NAME}" "${SIMULATOR_BIN}/${TARGET_NAME}" -output "${SIMULATOR_PATH}/${TARGET_NAME}.framework/${TARGET_NAME}"

fi
fi
exit 0;

 

There you go. Now you will simply develop.

Assuming that you are using your simulators and devices to test regularly during development before archiving, you will not need to take special note of this, BUT do know that to get your framework artifact you will have to Archive your project. Doing so will add an _Archive/ directory (Fig. 3) to the root of your project in Finder in which you will find your artifact. Know that you will have to build the project with a simulator and archive with a device to get a universal. If someone out there wants to contribute an xcodebuild or xctool script to build both of these on the fly, please, feel free. It would be a great contribution and a welcome addition for may, I’m sure!

003a
(Figure 3)

(An example of a working framework template can be found on here on kodmunki™ Github.)

That’s it! Now, you have a Framework. But, if you are anything like me, you have likely built a kernel or some other low level Framework and will expect other higher level Frameworks to depend on this Framework. Like, say, persistence, or data. To do this you will do the following:

Creating Dependent Frameworks

  1. Perform steps 1-6 from above to create the Framework.
  2. In the Build Settings of both the project Target and the test Target, set “Always Search User Paths” to “YES”. (Fig. 4)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值