Making Your Own iPhone Frameworks. In Xcode

Back in April Oliver wrote an excellent article entitled “Making Your Own iPhone Frameworks”, in which he explained how to do what many developers still proclaim as impossible: how to create custom frameworks that you can use in your iPhone apps! I would recommend that you read Oliver’s article first, especially if you’re wondering what a framework is… or, possibly, why you should consider making your own frameworks. I wont talk about that here :) .

To build frameworks Oliver wrote a simple shell script that first creates a typical framework bundle, and then copies resources into the appropriate directories. In this followup article, I’ll show you how to create the framework bundle entirely within Xcode, without any scripts, plugins, product-, or package-types. While this has a few advantages, I’m not claiming that my approach is in any way perfect, and there may be problems that I haven’t encountered yet. As always, this is a work in progress, so if you have any suggestions, please feel free to leave your comments below.

Enough talking. Lets get started –

The process is remarkably simple once you recognize that a framework is just a bundle like any other (whether we’re talking about Mac OS X or the iPhone OS). And since Xcode already knows how to work with bundles, all we  need to do configure one properly.

Step 1. Open Xcode and Create a new “Cocoa Touch Static Library” project, and delete the existing target.

Step 2. Add a new “Loadable Bundle” target from the Cocoa section by clicking “Projects > New Target…”.

Selecting the "Loadable Bundle" Target Template

The "Loadable Bundle" target template.

At this point you may be tempted to add a “Framework” target from the Cocoa section instead. Don’t. The iPhone SDK doesn’t support framework products, and it’s more than happy to tell you that if you ever try to build one. Our “Loadable Bundle” target just creates a generic (cfbundle ) bundle product, which is supported.

Step 3. Now comes the fun part: reconfiguring this bundle to produce a framework that Xcode can link against!

Bring up the “Build Settings” window for this new target selecting it and clicking “File > Get Info”.

The "Build Settings" window for "Loadable Bundles".

The "Build Settings" window for "Loadable Bundles" target.

Now we’re going to change a few build settings.

  1. Change the “Wrapper Extension” to “framework”.
  2. Change “Mach-O Type” to “Relocatable Object File”.
  3. Turn “Dead Code Stripping” off.
  4. Turn “Link With Standard Libraries” off.
  5. Remove all references to “AppKit” and “Foundation”.

Step 4. Change the bundles type from “BNDL” to “FMWK” in the frameworks Info.plist file. You can do this in the “Properties” section of the targets “Info” window (a bit right of the “Build Settings” section.)

The "Properties" window for the "Loadable Bundle".

The "Properties" window for the "Loadable Bundle" target.

Note: the Info.plist file isn’t required for linking in Xcode. In fact, if you check out Apples frameworks you’ll notice that they don’t actually contain the Info.plist file at all. If you’re creating a framework with a lot of resources however, it’s nice to have Info.plist setup for you, so I just leave it in.

Step 5. Add some code to the project if you haven’t already. I’ve added a HelloWorld class to the example.

The newly added "HelloWorld" class.

The newly added "HelloWorld" class.

Step 6. Add a new “Copy Headers” build phase with “Project > New Build Phase > New Copy Headers Build Phase”. This build phase is responsible for putting public and private headers into the right directory.

The newly added "Copy Headers" build phase.

The newly added "Copy Headers" build phase.

Step 7. Add your headers to the to the “Copy Headers” build phase and set their role appropriately. The easiest way that I’ve found to do this is to right click “Copy Headers” and hit “Set Role > Public”. This will make all of the headers public, copying them to the framework bundles “Headers” folder.

Note: you can also set the role of headers individually if that floats your boat :) .

Step 8. Add your source files to the “Compile Sources” build phase. These will be compiled to create a relocatable object file, which is then linked against automagically by Xcode.

The target with header files and source files added.

The target with header files and source files added.

Tangent: I haven’t had any problems with this approach yet, nor do I expect to, but if this does prove to be a problem you can just build a the “Static Library” in Xcode and add it to the frameworks “Copy Bundle Resources” build phase. This would also be useful when building a universal framework, as Oliver described in “Universal Static Libraries”.

Step 9. Build the framework bundle.

Step 10. Enjoy your new framework!

The finished framework, built and ready to go!

The finished framework, built and ready to go!

Now for some conclusions.

That’s really all there is to it, although it doesn’t have to end here. You can use this technique to build almost any kind of custom bundles from within Xcode! Personally I’ve used this approach to build plug-in bundles for a number of projects, and found it to be very convenient. Certainly much more convenient than building the bundles by hand!

转载于:https://www.cnblogs.com/ryanlaw/archive/2011/10/11/2207900.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值