Working with a Static Library/Framework vs Embedded Framework


Terminology to simplify discussion —
Static Library = Static Framework
Embedded frameworks = Dynamic frameworks
You can find standard defnitions and details about Static library vs Dynamic linker here.

Putting forward my exprience with embedded frameworks and then to support iOS7 having to move back to static library sharing all the useful links and resources.

Recently for a project I was fluctuating between using a static library or an embedded framework for a project which supported iOS7+ users.I had not worked on a static library till then and there was Dynamic/Embedded frameworks in iOS8.Dynamic or embedded frameworks were introduced in mac but in iOS, only Apple could create dynamic frameworks (UIKit.framework etc).Finally they allowed 3rd party developers to create and ship embedded frameworks in iOS8. Also with the advent of extensions need for code sharing across targets/apps is essential now.

To start sharing code between app and extension using embedded frameworks is fairly easy.

  1. Click on ‘+’ button to add a new target.
  2. Choose “Cocoa Touch Framework”
  3. Your framework is automagically connected to main app target.You don’t need to do anything there.
  4. To connect it to extension targets – 1) Select the extension target.
    2) Go to general tab and click the ‘+’ button in “Linked Frameworks and Libraries” and select the framework.Done.
  5. Also to limit your embedded framework to use APIs not available to extensions you can tick mark “Allo app extension API only” in embedded framework target under Generals tab.This will prevent any APIs (like UIApplication) not available in extensions to come in embedded frameworks.

Following images below depict the steps —

Thats it!! to start sharing your code between App and extensions using embedded frameworks.

  1. To import files from extension use normal import statements with double quotes.
  2. To import storyboard use storyboardWithName:bundle: API.
  3. To import localized strings use NSLocalizedStingFromTableInBundle.
  4. To import images from xcassets use (new iOS8 API) in UIImage — imageNamed:inBundle:compatibleTraitCollection:

Everything looks good but bummer!!

Although the app works fine in iOS7 and above but with a warning “dylibs work with iOS8 and above”.Apple document titled “Deploying a Containing App to Older Versions of iOS” raised my hopes but I didnt understand how to make it work.Most of the blogs, stackoverflow pointed that it wont be accepted by itunes connect or it will be rejected in review.Finally came the reply from Apple folks “Due to some underlying changes in OS embedded frameworks cant work in iOS7.You can support iOS7 for containing app and load embedded framework conditionally in iOS8 or go for a static library.

That means I had to move back to static library.Here is the Raywenderlich tutorial to get started wth static library. Here are some of the things which turned out to be challening while working with static library —

Usually your static library would consist of following parts —

  1. Code— All import statements needs to be done using angular bracket syntax.For instance #import <>.
  2. Assets — Storyboard,Xibs, Assets, Strings — You can’t put resources in a static library.So the only option is to attach a separate bundle containing assets, storyboards, strings with the static library.Here is the articlewhich explains it properly.

All strings,storyboards in static library bundle can be accessed just like in a embedded framework but for UIImage there is no method of “imageNamed:” variant to load image from bundle in iOS7.(iOS8 provides one as mentioned above.) So you have to use “imageWithContentsOfFile” loading appropriate image file in iOS7 to load images from xcassets of bundle.

There you go your static library is up and running and you can successfully share code among your apps and extensions.

References —

  1. http://www.raywenderlich.com/41377/creating-a-static-library-in-ios-tutorial#comments
  2. Static library with resources — http://www.galloway.me.uk/tutorials/ios-library-with-resources/
  3. http://www.raywenderlich.com/65964/create-a-framework-for-ios
  4. http://www.cocoanetics.com/2011/12/sub-projects-in-xcode/
  5. http://www.cocoanetics.com/2012/01/helping-xcode-find-library-headers/
  6. http://stackoverflow.com/questions/5543854/xcode-4-cant-locate-public-header-files-from-static-library-dependency

Do put forward your views about the article.My Email is cocoagarage.co@gmail.com and twitter handle @cocoagarage.

Advertisements

Về haipro912
Đời rất dở nhưng anh vẫn phải niềm nở =))

Trả lời

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Đăng xuất /  Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất /  Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất /  Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất /  Thay đổi )

w

Connecting to %s

%d bloggers like this: