Sunday, April 26, 2015

How to use NIB files from library

Create re-usable libraries in iOS


Let's start by creating a simple application
Note: All of my samples are done with ObjC in XCode 6.3

We have 2 parts for this solution.
An application that displays a tableview of data fetched from a library. When you tap on a cell, we get a detailed view in the form of a webview that was added as a bundle to library.

Now that the objective is set, let's ensure we have the application ready to do what we need


  1. Open XCode
  2. File -> New Project. Select "Single View Application". Give the application a suitable name. I named it "MyTableViewApplication"
  3. Make sure you have valid entries for Organization name, identifier. Select language to be "ObjectiveC". I created this application for iPhone. You cna choose any device of your choice
  4. Select a directory location
  5. Here's what you'd see as the structure for your application 
  6. We want a simple tableview, but what the template created was a UIViewController based view. Let's quickly change it to a UITableViewController
  7. Delete existing ViewController.m and ViewController.h
  8. Create a new class derived from UITableViewController say "MyTableViewController". You can as well change the ViewController base class from UIViewController to UITableViewController, if you want.
  9. Open up Main.storyboard. Delete existing ViewController
  10. Drag and drop a UITableViewController. Associate this new Controller to "MyTableViewController"
  11. Select the new TableViewController, go over to AttributesInspector pane and check "Is initial view controller", if it is not already checked.
  12. New folder structure should be like this
  13. When executed, here's what you should see


We now have a basic application, lets go create a re-usable library


  1. File -> New Project, select "Framework and Library" -> "Cocoa Touch Static Library". Give it a name "MySampleLibrary"
  2. Basic project will have nothing more than a class - "MySampleLibrary" in the form of .m and .h
  3. I prefer to reserve this class for generic stuff and I create custom classes for all purposes. To start with lets create a simple class MyTableViewCellData.
  4. Contents of MyTableViewCellData.h and MyTableViewCellData.m
  5. I want to create a collection that will be consumed by my application, hence I created "MyTableViewCellDataCollection".
  6. Contents of MyTableViewCellDataCollection.h" and MyTableViewCellDataCollection.m
  7. In order for the 2 classes to be visible in our application, we have to ensure we make them available along with the libraries header files. We do that by opening up the build phases for the target, expand "copy files" section, tap on the small "+" sign at the bottom and add "MyTableViewCellDataCollection.h" and "MyTableViewCellData.h". 
  8. Compile code. If all is well, We need to copy over our library and header file for our application. To locate them. From Window menu select "Projects", select "MySampleLibrary", tap on the small arrow, displayed right after location of "Derived Data". This will open up finder.
  9. Browse into "MySampleLibrary-XXXX" -> Build -> Products -> Debug-iphonesimulator. You'll see "libMySampleLibrary.a", "include" directory. Copy both of them to a temporary folder or to a more accessible location


Consuming library


  1. Open up "MyTableViewApplication", create a "group" say "CustomExternalLibraries" (I prefer creating physical directories, over groups. For convenience sake, lets create a group), add "libMySampleLibrary.a" and "include" directory. Project structure should be like the following 
  2. Add needed header files into "MyTableViewController"
  3. Code for MyTableViewController.m and MyTableViewController.h
  4. When the code is compiled and executed, here's what you should see


Bundle nib files with in library


  1. Add a new ViewController "MyTableViewDetailedWebViewController" with class and nib file
  2. Add a webview into ViewController. Connect IBOutlet. Code for MyTableViewDetailedWebViewController.h and MyTableViewDetailedWebViewController.m
  3. Select "MySampleLibrary" target, select "Build Phases", Tap "+" and add "MyTableViewDetailedWebViewController" to build phases 
  4. File -> New -> Target, Select "Framework and Library" from OSX, select "Bundle", tap on Next. Give it a name "MySampleLibraryBundle".
  5. Select the bundle target, Add nib file for "MyTableViewDetailedWebViewController" in "Copy Bundle Resources" 
  6. Select bundle target, select "Bundle Settings", Change "Base SDK" from OSX to "ios X.x" 
  7. Select the bundle target, "MySampleLibraryBundle" from schemes above and build.
  8. Switch back to "MySampleLibrary", compile. All should be good
  9. Go into the DerivedData folder of library, like previously, pick up .a file, include directory and .bundle file. Move it to a more accessible directory.


Consuming nib files in main application


  1. Add .a, headers and bundle file
  2. Make changes to didSelect on tablecell, to present the new viewcontroller added in bundle. Updated MyTableViewController code
  3. When executed, user taps on a cell, we show a detailed webview, like the following 



No comments: