building static libraries for ios with cocoapods
DESCRIPTION
If you ever wondered how to easily develop and manage your own libraries for iOS and OS X seek no further! Watch the presentation and read our blog to find a solution. If you have any questions just drop me an email or tweet :)TRANSCRIPT
• What is CocoaPods? • Local CocoaPods dependencies. • Building a distribution library. • One library - 5 different architectures. • Avoiding collisions in other projects. • Final thoughts.
Roadmap:
CocoaPods is the dependency manager for Objective-C projects. It has thousands of
libraries and can help you scale your projects elegantly. [http://cocoapods.org]
Podfile:pod 'CocoaLumberjack', '~> 1.7.0' pod 'AFNetworking', '~> 1.3.3' pod 'UIView+AutoLayout', '~> 1.1.0' pod 'MagicalRecord', '~> 2.2' pod 'Reachability', '~> 3.1.1' pod 'UIAlertView-Blocks', '~> 0.0.1'
Terminal:$ pod install
Semantic versioning: !
• Major.Minor.Patch -> 2.0.1 • Major - incompatible API changes • Minor - added features but backward compatible • Patch - backward compatible bug fixes
int add(int a, int b) { return a-b; } // 1.0.0
double add(int a) { return 3.14+a; }int add(int a, int b, int c=0) { return a+b+c; }int add(int a, int b) { return a+b; } // x.y.z
CocoaPods project structure
Podspec file
Pod::Spec.new do |s| s.name = "NetworkLib" s.version = “1.0.0” s.platform = :ios, '7.0' s.source_files = 'NetworkLib', 'NetworkLib/**/*.{h,m}' s.public_header_files = 'NetworkLib/**/*.h' s.resources = "NetworkLib/*.png" s.framework = 'SystemConfiguration' s.requires_arc = true s.dependency 'AFNetworking' end
Local CocoaPods dependency
$ pod 'NetworkLib', :path => '../NetworkLib'
git fork CocoaPods dependency$ pod 'WebViewJavascriptBridge', :git => ‘https://github.com/burczyk/WebViewJavascriptBridge/', :commit => '5183c4c'
Demo #1
Building distribution library
xcodebuild -workspace NetworkLib.xcworkspace -scheme NetworkLib -sdk iphonesimulator -configuration Debug
lipo -create lib1.a lib2.a -output libMultipleArch.a
Aggregate Lib script
Build all supported architectures
$ lipo —info libNetworkingLib-debug-0.1.a !!Architectures in the fat file: libNetworkLib-debug-0.1.a are: armv7 armv7s i386 x86_64 arm64
Copy files phase & results
Demo #2
Symbol collisions
What happens if user adds AFNetworking to his app?
Symbols investigation$ nm libNetworkLib-debug-0.1.a | grep AFN !!000000000000bea0 s -[UIWebView(_AFNetworking) af_HTTPRequestOperation].eh !0000000000000030 t -[UIWebView(_AFNetworking) af_setHTTPRequestOperation:] !000000000000bec8 s -[UIWebView(_AFNetworking) af_setHTTPRequestOperation:].eh !0000000000000190 t ___44-[UIWebView(AFNetworking) requestSerializer]_block_invoke !000000000000bf18 s ___44-[UIWebView(AFNetworking) requestSerializer]_block_invoke.eh
Symbol collisions solutions
• Pretend they don’t exist ;)
• Write in docs which dependencies are required
• Export all dependencies’ headers
• Build library with prefixed symbols
Exporting all symbols
http://pdx.esri.com/blog/2013/12/13/namespacing-dependencies/
nm $CODESIGNING_FOLDER_PATH -j | sort | uniq | grep "_OBJC_CLASS_\$_" | grep -v "\$_NS" | sed …
//NamespacedDependencies.h: !#ifndef AFHTTPSessionManager #define AFHTTPSessionManager __NS_SYMBOL(AFHTTPSessionManager) #endif
AFHTTPSessionManager —> SIGMA_AFHTTPSessionManager
Configuring Pods & lib to use prefixed symbols
Symbols re-investigation
$ nm libNetworkLib-debug-0.1.a | grep SIGMA !!000000000000b720 s l_OBJC_$_CATEGORY_SIGMA_AFURLConnectionOperation_$__UIProgressView !000000000000b6d8 s l_OBJC_$_PROP_LIST_SIGMA_AFURLConnectionOperation_$__UIProgressView !U _OBJC_CLASS_$_SIGMA_AFHTTPRequestOperation !U _OBJC_CLASS_$_SIGMA_AFHTTPRequestSerializer !U _OBJC_CLASS_$_SIGMA_AFHTTPResponseSerializer
Demo #3
• Easy development • Clean git repo • Easy switching from local podspec to static
library • Shell & classic build script sometimes
necessary • Only one step to put your lib to global repo
Summary
Contact: [email protected] @KamilBurczyk
Thank you!
Links: http://cocoapods.org/
http://pdx.esri.com/blog/2013/12/13/namespacing-dependencies/ http://blog.sigmapoint.pl/