I have an existing iOS app and want to add a large chunk of code that I've been developing as another project just for ease of testing. The new chunk basically deals with saving an image to various sharing services, etc.. Because that sharing code needs a lot of testing and future updating, I was wondering what the best way to incorporate that code chunk into my existing app.
I don't know if it should be a static library, dynamic library or a framework, and honestly, I'm not really sure what the difference is, or how I should go about it and get it set up in Xcode.
All I know is that I need/want to keep a separate testing and updating app for the sharing code and have the main app use it.
If you can't already tell, I'm not a git wiz. I'm just a simple one man developer.
Any help or direction would be appreciated. Thanks!
First, some general definitions (specific to iOS):
Static library - a unit of code linked at compile time, which does not change.
However, iOS static libraries are not allowed to contain images/assets (only code). You can get around this challenge by using a media bundle though.
A better, more formal definition can be found on Wikipedia here.
Dynamic library - a unit of code and/or assets linked at runtime that may change.
However, only Apple is allowed to create dynamic libraries for iOS . You're not allowed to create these, as this will get your app rejected. (See this other SO post for confirmation and reasoning on such).
Software Framework - a compiled set of code that accomplishes a task... hence, you can actually have a static framework or a dynamic framework, which are typically just the compiled versions of the above.
See the Wiki on Software Framework for more details.
Hence on iOS, your only option is basically to use a static library or static framework (the main difference being that a static framework is distributed as a compiled .a
file most often, whereas a static library may simply be included as a subproject - you can see all of the code - which is compiled first and its resulting .a
file used as a dependency by the project).
Now that we're clear(er) on these terms, setting up a static library and supporting media bundle for iOS isn't too difficult, and there are many tutorials on how to do such. I personally would recommend this one:
https://github.com/jverkoey/iOS-Framework
This is a pretty straight-forward guide and doesn't have the disadvantage of dealing with "fake static libraries"... check it out for more info...
Once you've created your static library, it's as easy as including it as a submodule within Git for use across different projects.
Good Luck.
EDIT
Regarding a subproject within a project, as far as I know, to get this to work/compile correctly, you essentially have to set up a compile chain where the subproject is compiled first, which creates a static framework .a
file that is used as a dependency by the project.
Here's another useful tutorial which talks about this:
http://www.cocoanetics.com/2011/12/sub-projects-in-xcode/
EDIT 2
As of iOS 8, Apple now permits developers to create dynamic frameworks! (Note: your app must have a minimum target of iOS 8 to include a dynamic framework... back porting isn't allowed.)
This has been added as a new project template. In Xcode 6.1, this can be found at:
New Project -> iOS -> Framework & Library -> Cocoa Touch Framework
Libraries and Frameworks
Martin Fowler on InversionOfControl
A library
is essentially a set of functions that you can call, these days usually organized into classes. Each call does some work and returns control to the client.
A framework
embodies some abstract design, with more behavior built in. In order to use it you need to insert your behavior into various places in the framework either by subclassing or by plugging in your own classes. The framework's code then calls your code at these points. The main control of the program is inverted, moved away from you to the framework. (Inversion of Control)
Libraries and Frameworks on iOS
A library
is a collection of resources and the code itself, compiled for one or more architectures. It consists of *.o
files (Mach-O object files).
In the case of static libraries (*.a)
, the code that the app uses is copied to the generated executable file by a static linker
during compilation time
.
As of Xcode 9.0, static Swift libraries are now supported
✓ Pros:
- Static libraries are guaranteed to be present in the app and have correct version.
- No need to keep an app up to date with library updates.
- Better performance of library calls.
✕ Cons:
- Inflated app size.
- Launch time degrades because of bloated app executable.
- Must copy whole library even if using single function.
Dynamic libraries (*.dylib)
are different from static libraries
in the sense that they are dynamically linked with the app’s executable at load
or runtime
, but not copied into it. As a result, the executable is smaller and, because the code is loaded only when it is needed, the startup time is typically faster.
All iOS and macOS system libraries are dynamic. Hence our apps will benefit from the future improvements that Apple makes to standard library frameworks without creating and shipping new builds.
✓ Pros:
- Can benefit from library improvements without app re-compile. Especially useful with system libraries.
- Takes less disk space, since it is shared between applications.
- Faster startup time, as it is loaded on-demand during runtime.
- Loaded by pieces: no need to load whole library if using single function.
✕ Cons:
- Can potentially break the program if anything changes in the library.
- Slower calls to library functions, as it is located outside application executable.
A bundle
is a file directory with subdirectories inside. On iOS, bundles
serve to conveniently ship related files together in one package – for instance, images, nibs, or compiled code. The system treats it as one file and you can access bundle resources without knowing its internal structure.
The library
may also have additional resources: headers, localization files, images, documentation and examples of usage. We can bundle all of this together in one bundle
– and the name of this is framework
.
A framework (*.framework)
is a specific kind of a bundle
. With small changes to its structure, it can even contain other frameworks. Such aggregate is known as umbrella framework
.
Static frameworks
contain a static library
packaged with its resources.
Dynamic frameworks
contain the dynamic library
with its resources. In addition to that, dynamic frameworks
may conveniently include different versions of the same dynamic library
in the same framework!
If your Deployment target is iOS8+ also you are able to create embedded framework
.
Embedded framework
is a Dynamic framework
and is placed within an app’s sandbox and are only available to that app. This type was created first of all for extension to share common code and resources.
How to build and use read here:
Swift consumer -> Swift static library
Swift consumer -> Objective-C static library
Objective-C consumer -> Swift static library
Objective-C consumer -> Objective-C static library
Links
The source and one more. Also read more here, here, here, here, here
You can also create .podspec file for CocoaPods( http://guides.cocoapods.org/making/private-cocoapods.html#1.-create-a-private-spec-repo ) and use it like any other pod with the only difference that it's your private pod and is not visible to outside world(I'm not sure what will happen if your pod should create CoreData model, but that's not the case, as I understand).
来源:https://stackoverflow.com/questions/15331056/library-static-dynamic-or-framework-project-inside-another-project