In my app, i currently have all my code separated into a static library, to make it easier to set up the xcode project targets for the actual app and for unit tests for my code. The problem with this is that i want to put most of my xib files in the static library as well, but it seems that when i run my app and try to reference the xib it can't find it unless it is included in the actual app's target instead of the static library target. Is it possible to have xib files and other resources included in static libraries that can be referenced by code in that same library, and if so, how?
6 Answers
No it isn't possible, because a static library is not the same as a "bundle".
A bundle is a directory that may contain all manner of files including resource files (xib), executable files and static libraries. It exists on the filesystem as a group of individual files.
A static library is a single file that contains classes, code and variables that were linked together by the library creator. It does not "contain" other files, it is essentially a database of compiled code.
Although it would be possible to put the data for the xibs in there, Xcode would have no way of knowing that it was in there, as it looks for them as individual files on the filesystem.
In Mac OS, you may create a "Framework" which is essentially a bundle of code, resources, settings etc which may be reused by multiple projects. However, Apple does not seem to support custom framework creation for iPhone OS.
Static Libraries http://en.wikipedia.org/wiki/Static_library

- 14,761
- 17
- 70
- 94
-
Thanks for the links. Would it be possible to organize my xibs into their own bundle and have my library code be dependent on that? (Sorry if the answer is in that apple doc you provided; i don't have much time to peruse it at the moment.) – Kevlar Apr 28 '09 at 17:08
-
2Just found this, may be of use: http://landonf.bikemonkey.org/code/iphone/iPhone_Framework_Support.20081202.html – Justicle Apr 29 '09 at 06:36
-
1Unless the Xcode Wizards (or whatever we call them on OSX) are wrong, there is no "bundle" on iOS either, leaving me in a marginally suicidal state. – Dan Rosenstark Aug 25 '12 at 23:27
-
@Yar hopefully you didn't kill yourself, I think you can add frameworks to iOS projects in Xcode 6 – Alex Nolasco Sep 19 '14 at 18:04
-
Add bundle from OS X wizard than after change Based SDK "OS X xx.x" to "iOS x.xx or iOS Latest" in Build setting – Meghs Dhameliya Aug 13 '15 at 12:50
Reply to comment (won't fit in comment box)
No worries, I've been trying to do pretty much the same thing as you for the last week - I'd like to ship a "framework" of xibs, include files and .a libs to a client without giving them all the source code. I couldn't find a good way to do this with bundles either.
For whatever reason, Apple are being particularly obtuse about this - I can't see a reason for them to be so in the case of static libraries (dynamic libraries fair enough).
My solution for now is to manually create a package folder "Foo" that contains the following subfolders:
- "include" -> put .h files here
- "res" -> put .xib files here
- "lib" -> contains "iphoneos" & "iphonesimulator" subfolders each with libFoo.a
Then zip this up and send to the client. The client then:
- Unzips the package where ever they like.
- Adds the "res" folder under the resources group.
- Changes the following target settings:
Other Linker Flags = -Objc -lfoo
Header Search Paths = /include
Library Search Paths = /lib/$(PLATFORM_NAME)
I can probably automate the package creation with some build steps at my end, but the client is stuck with four slightly fiddly steps to get set up.

- 14,761
- 17
- 70
- 94
-
Can you please explain what the -lfoo linker flag does? I'm not sure how the compiler processes this. Thanks! – jpswain Dec 14 '11 at 19:40
-
2`-lfoo' is just the linker flag for "link to library foo'. See http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html. Please upvote if you found this answer useful. – Justicle Dec 15 '11 at 19:20
-
Ok, so it's just by convention? For example if my lib is in package "Thingy", and my output file is "libThingy.a", then my linker flag would be "-lthingy"? Is that correct? Also you have "-Objc"--isn't it supposed to be "-ObjC"? Thanks! +1 – jpswain Dec 15 '11 at 20:09
-
To answer my own question--sorry i didn't look at the link first--that is correct. :-) – jpswain Dec 15 '11 at 20:11
I found a perfect solution for this that does all the above automatically and more https://github.com/kstenerud/iOS-Universal-Framework Its an xCode plugin
It worked for me like a charm, It works only for XCode 4 and above

- 434
- 2
- 10
Yes You can. add a xib file in your library as you would do for any normal project. Then in library project target add the xib file in copy Files section along with .a file. In your main project where you are using the library, drag and drop the xib file where .a file for library is located.

- 7,649
- 8
- 33
- 48
-
1You are the only guy who is supporting that It can be done. I tried all the solution, but it is not working for me at all. And just because you are confident enough I am trying harder to do that. Can you please help me with the steps. I am tired of experimenting. – Kumar Aditya Jan 16 '14 at 12:23
-
1I am seconding @Rudeboy's request. Did you actually try this or are you just assuming that it should work? – Drux May 27 '14 at 01:44
Answer in including Xib files to your static library.
This time we have Xcode 11, you just create a bundle target in addition to your library target. The bundle template is available on macOS. Then from the library code, reference the bundle to be able to reference the nib. You distribute the library with the bundle.
A detailed video about using Xibs with static libraries below: https://www.youtube.com/watch?v=WQI02KR9kQw

- 1,478
- 2
- 21
- 32
When distributing you could also create an SDK. JSON.framework did this in their SVN, and I successfully replicated this. You can see how this was done in http://hltypes.svn.sf.net/ in the hltypes-ios.xcodeproj and the iOS folder in the project.
Primarily you need to "install" into your build folder, and then you need to copy the specially formatted SDKSettings.plist. Then add the path to the SDK into "Additional SDKs" list in application project. Downside of composite SDKs are the need to restart Xcode 3.x whenever this mini-SDK is updated, and Xcode's insistence on constructing a composite SDK created from Apple's base SDK and your mini-SDK (which means you need to wait quite a bit).
Application project still needs to have .xib and other resources manually added.

- 9,529
- 9
- 60
- 111