Custom dynamic framework on iOS - ios

There is a number of questions (some of them with answers) about creating custom dynamic frameworks for iOS. My practical experiance shows that on iPad2 and iPad3 one can use custom dynamic frameworks and it works. On iPad1 it doesn't work. So my question is. Did it become possible to use dynamic frameworks on new versions of iOS?

Apple doesn't allow you to create your own dynamic frameworks for the iOS platform. There is however a way to package up your static library and using in a similar way as a framework.
Did you see this post?
I particular check out this GitHub repo, this is what I used to get up and running, very simple explanations: https://github.com/jverkoey/iOS-Framework
Basically you will create a static library and create multiple targets for each platform - simulator and device and one last target which I usually call "build and merge", this target has a script that takes the resulting libraries for device and simulator, and packages them up in a folder called xxxxxx.framework, inside is the standard structure and it contains your static libs, when you use this in other projects you import the framework to that project and include the header files like you would any other real dylib framework.

Related

How to add iOS framework in iOS project effectively

Recently started work on an iOS project, written in swift and objective-c. As of now, we have a monolithic repo. Now we are focusing on creating few frameworks so that we can reuse same framework across multiple apps. I would like to know your opinion on below points.
If I add framework.xcodeproj in my client app project, I'm able to access the framework's public entities after writing the import statement. My concern is every time I build by client app project, this framework.xcodeproj is also compiling though its has not changed since last build nor it is dependent on any other framework.
If I add framework by adding it as framework.framework and make its entry into embed framework, I can access the public entities of the framework. What's alarming in this case is that whenever I change the code of framework I need to update the framework in the client app project too.
Is there any way to include framework in client app project where I can access the public entities and it does not get build every time I build client app project ?
It's absolutely fine if framework get's build when its code is updated.
I have used Visual studio in past which let me build my client project without building dependent projects if there is not code change in dependent projects.
If the framework is build every time you build your app, depends on the type of the framework:
There are Cocoa Touch Static Libraries and Cocoa Touch Frameworks.
Cocoa Touch Frameworks
They are always open-source and will be built just like your app. (So Xcode will sometimes compile it, when you run your app and always after you cleaned the project.) Frameworks only support iOS 8 and newer, but you can use Swift and Objective-C in the framework.
Cocoa Touch Static Libraries
As the name says, they are static. So they are already compiled, when you import them to your project. You can share them with others without showing them your code. Note that Static Libraries currently don't support Swift. You will have to use Objective-C within the library. The app itself can still be written in Swift.
Conclusion
If you don't mind using Objective-C, Static Libraries seem to fit your requirement, that the framework should only be built once.
But since I love Swift, I would personally recommend you to use frameworks (if you don't mind that others that use the framework can see your code). My experience showed, that it's not a big problem, that the framework is built sometimes.
AddThis wrote a good blog post about deciding whether to use Static Libraries or Frameworks.
Cocoa Touch Static Library vs. Cocoa Touch Framework

IOS: One 'Cocoa Touch Framework' Library for both Simulator and Real-Device?

I am working with XCODE 6 and SWIFT 1.2 in my iphone application at the moment and loooking a way to share the common codes between projects.
Therefore thought of creating a 'Cocoa Touch Framework' as explains in following tutorials that contains the codes I want to re-use among projects.
1). How to reuse Swift code in other projects?
2). http://locomoviles.com/ios-tutorials/create-ios-cocoa-touch-framework-using-xcode/
3). https://www.youtube.com/watch?v=86cPaa3FrRg
All ok and I can use the shared codes that contains in the Library project successfully.
So my problem is, this is really annoying I have to integrate framwork that contains in 'Debug-iphonesimulator' folder when testing with simulator and again the library that contains in 'Debug-iphoneos' when testing with a real device...
I see Three folders as follows (Built libraries)
Just generated the release library (the content in 'Release-iphoneos' folder) thinking that this may work in both deice and simulator. But again this works only with real-devices, and not with simulator.
Question 1). Can't I generate one library that can build and use in both 'simulator' and on a 'real device'?
Question 2). Is there any other way I can link (reference) to Framework Project directly rather than integrating a build library? So Project reference to external Framwork and use resources in there (No library integration... Referencing to project like eclipse android libraries and projects)

Embedded Binaries with iOS Framework

I was able to create iOS Framework for Xcode as shown in that video:-
https://youtu.be/86cPaa3FrRg?t=4m55s
On 5:00 they say that framework must be added to Embeded Binaries.
If I add it to Embeded Binaries then it works.
It is not required to add GoogleAds.framework to Embeded Binaries.
Adding to Embeded Binaries looks a bit "hacky" comparing to Googles solution.
So I think this tutorial is missing some step.
My question is: How can I make a framework that works without adding it to Embed Binaries?
tl;dr They are both frameworks, but they are different types.
The term "framework" is ambiguous. On OSX it means a packaged dynamic library (i.e. .dylib, headers, other stuff), however before iOS 8 users could not create dynamic libraries so "static frameworks" were invented to provide packaged static libraries (i.e. .a, headers, other stuff).
If the framework you created needs to be packaged with the app as an embedded binary then it sounds like a dynamic framework, however if Google Ads doesn't then I suspect it's a static framework. It doesn't need to be embedded as it's already been linked into the app binary.
If you want to know how to create a static framework, then start here, or Google for "ios static framework".
Prior to iOS 8, developers shipped unsupported frameworks that were cobbled together with static libraries by mimicking the directory structure of Apple's frameworks. They worked, but they were a pain to build, and they were static—not dynamic—libraries.
As of iOS 8, Apple officially supports building third-party dynamic frameworks in Xcode. These types of frameworks are code-signed and must be placed in the Embedded Binaries for your app. If you link against them but fail to put them in Embedded Binaries, you will get an exception when attempting to run on device and your app will crash.
In the long-run, I would expect the hacked together frameworks like GoogleAds.framework to disappear now that official framework support is available. This means you'll have to get used to putting frameworks in Embedded Binaries.
Unless you need to support iOS 7 with your code (in which case official frameworks are not an option, because they only work on iOS 8), I would advise against creating an unsupported type of framework at this point.

Does iOS 8 support dynamic linking?

Up until iOS7, Apple did not support dynamic linking due to security concerns. Code reuse between developers usually relied on static libraries, which were built as part of the executable of the app.
Introducing extensions in iOS8 seems to change this a bit, because extensions are separate executables. Sharing code between an extension and its containing app is done via a framework. Apple is saying this in their release notes:
Frameworks for iOS. iOS developers can now create dynamic frameworks.
Frameworks are a collection of code and resources to encapsulate
functionality that is valuable across multiple projects. Frameworks
work perfectly with extensions, sharing logic that can be used by both
the main application, and the bundled extensions.
Emphasis is mine.
Source: https://developer.apple.com/library/content/documentation/Xcode/Conceptual/WhatsNewXcode-Archive/Articles/xcode_6_0.html#//apple_ref/doc/uid/TP40014509-SW14
Further, in the extension dev guide, they explain that you can share code between an extension and the containing app via a "embedded framework".
Source: https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1
My question is - what is an embedded framework, how does it differ from a dynamic framework, and will we really see proper dynamic linking in iOS8? All the documentation I've read seems ambiguous about this.
"Embedded" and "Dynamic" frameworks don't refer to the same aspect of frameworks. They are not the opposite of one another. First, let's define what's a framework: in Apple lingo, a framework refers to some compiled code + the public headers of said code.
Now a dynamic framework is a framework whose code was built as a dynamic library. It is the opposite of a "static" framework, where the code would be built as a static library. In case you're wondering, Wikipedia has a very nice explanation of what's the difference between a static and a dynamic library.
And finally, an embedded framework is a framework that is packaged within an app, as opposed to being installed system-wide, in "/System/Library/Frameworks" for example.
An embedded framework is simply one that's included in the app bundle, rather than a global framework which is installed in system directory.
Dynamic Frameworks doesn't means dynamically linked framework. Apps still just work in a sandbox environment. It's not like your custom frameworks are going to be installed with the iOS. Each one of your apps that uses the common framework that you've developed is going to be copied inside every app's bundle.
You can see this for yourself. Go to your Xcode's Derived Data directory and inside the app's data, you'll find all the embedded frameworks copied.
The only new thing with creating Frameworks with iOS 8 compared to static libraries is that the public header files and assets gets bundled within the Framework. If you've been doing this sort of things with earlier versions of Xcode, you'll remember the pain with supplying public headers and then adding the search path within Xcode settings and for assets we had to pass in a separate bundle.
This only downside of using Frameworks that I see is that now we can't simply use lipo to generate a fat static library for all kind of architectures.
IMO Apple is using the word 'dynamic' to suggest 'independent'.
Watch WWDC2014 - Building Modern Frameworks for more details.

Library? Static? Dynamic? Or Framework? Project inside another project

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.
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
Mach-O file format(Mach Object - .o)
In iOS world every source file is converted into object files - ABI[About] Mach-O file[About] which will be packaged into a final executable bundle(application, framework), file (library) and it's behavior is determined by Mach-O type[About]
Package is a directory which behavious itself as a file - opaque file. It is created for user experience to complicate making some changes into internal structure that can cause unpredictable program behaviour. Package is used in Document Package or with a Bundle. You can use Show Package Contents in a Finder
Bundle is a directory with a specific structure to organize a binary(executable code) and resources for that code(e.g. images, nibs... Assets.car file[About]).
Bundle contains Info.plist[About] file. Bundle was created for developer experience. Also it can be packaged. There are several types of bundle:
application bundle - Application target
framework bundle and versioned bundle as a subtype - Framework Target
loadable bundle(aka plug-in bundle) - '... Bundle' (UI Testing Bundle, Unit Testing Bundle) - can be loaded at runtime. .bundle extension for Mac OS
[Mac OS] XPC Service - Cross Process Communication is a kind of Inter Process Communication (IPC). It can be used as a module on a different process(managed by launchd root process)[About]
others(dSYM[About] bundle)
Application - .ipa, .app[About] - packaged application bundle - launchable program.
Application extension[About] - from iOS v8 - extends functionality of Application which are available when user interacts with other application. App extension as a bundle is a part of Containing app but it is run on their own sandbox(processor, memory...), app which try to use app extension is called Host App. Types of extension app:
Action
Share
Photo Editing
Today aka widget
...
to share common code and resources. It's available when Deployment target is iOS 8+.
Tests - packaged loadable bundle which is used to test a binary. Plug-in architecture allows us to add a new functionality(test cases) as a separate module into existing binary
Libraries and Frameworks
[Library vs Framework]
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. This phenomenon is Inversion of Control (also known as the Hollywood Principle - "Don't call us, we'll call you"
Libraries and Frameworks on iOS
They can help you to solve: modularity, reusing, encapsulation, improve build time
Library is a collection of Mach-O object files[check static or dynamic] compiled for one or more architectures.
Static library - .a(aka static archive library, static linked shared library[doc]) - When you add it into your application the static linker during compilation time will merge the object files from the library and package them along with the application object files into one single executable file. The disadvantage is a big output file
From Xcode 9.0, Swift static library is supported.
Dynamic library - .dylib(aka dynamic shared library, shared object, dynamically linked library[doc]) is dynamically linked with the app's executable at load or runtime, but not copied into it. On practice app's package will contain Frameworks folder with .dylib file. All iOS and macOS system libraries are dynamic. The disadvantage is a slow launch time since all dynamic libraries should be copied and linked.
[iOS static vs dynamic library]
[Static vs dynamic linking]
Text-based stub library - .tbd[About], it is a text stub of dynamic library which is located on a target device. As a result you should not package a dynamic library into your bundle. It has a size effect.
Framework aka binary framework - .framework is a not packaged framework bundle(to allow developers to easily take a look at headers and resources) which contains a compiled static or dynamic library, header files and resources.
Static framework contain a static library packaged with its resources.
Dynamic framework aka Embedded framework - from iOS v8 - contains the dynamic library and resources. In addition to that, dynamic framework can include different versions of the same dynamic library in a single bundle (versioned bundle). Also Embedded framework is used in App Extension
[Static vs dynamic framework]
Umbrella framework [Aggregate target] is a framework that contains other frameworks. It is not officially supported on iOS and that is why it is not recommended for developers to create them[Official doc]. In actuality it's a set of sub-frameworks(or Nested Frameworks). When you create a framework which has a dependency, a consumer (such as an app) is responsible for adding this dependency along with your framework into the project. As a developer, it's natural to try to find a way to transfer this duty from consumer to your's. As a result you think that Umbrella framework is the rescue but usually it leads to a serious issues with managing versions and complexity of creating and supporting it.
Fake Framework - is a result of specific operations under a static library to create a bundle with .framework extension that will behave yourself as a dynamic framework. This technic was used when Xcode did not support creating a framework since did not have a framework template. One of realisation of a fake framework. With Xcode 6, Apple has added iOS framework support.
Modular Framework[About] - #import it is a framework which contains a .modulemap file inside. Module can contains submodules. The main advantage is that you save a build time with Modular Framework.
Universal Library or Framework (aka Fat) [lipo] [Aggregate target] contains multiple architectures. For example your release build should support a some arch which you can regulate via Build Active Architecture Only [ONLY_ACTIVE_ARCH]
XCFramework[About] was introduced by Xcode 11 and it is a bundle which includes multiple architectures(arm, x86_64...) and platforms(iOS, MacOS...). It should replace a Universal Framework
Dependency[About] You are able to use third party code as a part of your target. It allows you to reuse a code from a lot of sources like - another project, project in the same workspace, another target, library, framework etc.
How to build and use a Static Library:
[Swift consumer -> Swift static library]
[Swift consumer -> Objective-C static library]
[Objective-C consumer -> Swift static library]
[Objective-C consumer -> Objective-C static library]
How to build and use a Dynamic Framework[change to static]
[Swift consumer -> Swift dynamic framework]
[Swift consumer -> Objective-C dynamic framework]
[Objective-C consumer -> Swift dynamic framework]
[Objective-C consumer -> Objective-C dynamic framework]
[Xcode Build System]
[Xcode components]
[Dynamic linker]
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).

Resources