Monday, 9 December 2013

Creating static library & adding views to it


   Creating static library & adding views to it 

 

What is Static Library?

Static library are  custom users library which contain user specific classes bundled together same as iOS created frameworks. 


Why Use Static Libraries?

You might want to create a static library for different reasons. For example:
  • You want to bundle a number of classes that you and/or your colleagues in your team use regularly and share those easily around.
  • You want to be able to keep some common code centralized so you can easily add bugfixes or updates.
  • You’d like to share a library with a number of people, but not allow them to see your code.
  • You’d like to make a version snapshot of a library that develops over time.

 

 

How to create Static Libraries?

Following are the steps to create static library:
  • Create a new project and select static library( New->iOS->Frameworks & Library) in it you will get two files .h & .m file in it, either you can add your own methods in it or you can delete these files to add your own custom code in it.The methods written in it can be accessed in your application after integrating in it.  
  • You will find that in place of .app or ipa the static library file with extension .a is created in the products folder.Build your application & make sure you can locate your .a file {static library} in finder.
Note: The binary file create here might not work on simulator but when you check it on device it will work properly because of the difference in architecture of library & simulator. 
Question: why it happens???
By default, the library file will be built only for the current architecture. If you build for the simulator, the library will contain object code for the i386 architecture; and if you build for the device you’ll get code for the ARM architecture. You would need to build two versions of the library and pick which one to link as you change targets from simulator to device.
Solution:
Create a universal binary that contains the object code for both architectures.

Adding Views to Static Library

  • From the build settings of your library project click the “Add Target” button and add a target to the library project of type “Bundle”.
  • When the resources bundle target is added, default it will be set to  Mac OS X build target.Change its iOS target to be (probably “Latest iOS”).
  • In order to get resources to be put into the bundle all you have to do is add them to the “Copy Bundle Resources” build phase of the target.

    Your library will get created SUCCESSFULLY


How to create Universal binary?

  • Tap on File->New->Add target->OSX->Other->Aggregate -YourBinaryName - Finish.
  • Click on the project in the project navigator, then select the aggregate(Your binary Name) target. Switch to the Build Phases tab; this is where you will set up the actions that will run when the target is built.
  • Click on the Add Build Phase button, and select Add Run Script in the menu that pops up, as shown

 The code is not so complicated, this is how it goes line-by-line:  

UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
# Step 1. Build Device and Simulator versions
xcodebuild -target TestLibrary ONLY_ACTIVE_ARCH=NO -configuration $ {CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="$ {BUILD_ROOT}"
xcodebuild -target TestLibrary -configuration ${CONFIGURATION} -sdk iphonesimulator -arch i386 BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"
# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
# Step 2. Create universal binary file using lipo
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/lib${PROJECT_NAME}.a" "$ {BUILD_DIR}/${CONFIGURATION}-iphoneos/lib${PROJECT_NAME}.a" "${BUILD_DIR}/$ {CONFIGURATION}-iphonesimulator/lib${PROJECT_NAME}.a"
# Last touch. copy the header files. Just for convenience
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/include" "$ {UNIVERSAL_OUTPUTFOLDER}/"
  
  • Select the aggregate target UniversalLib in the Scheme Selection drop down, as so (unlike on the screenshot below instead of “iOS Device” you might see your device’s name):Press the Play button to build the target for the aggregate scheme. 
  • To see the result, use the Show in Finder option on the libTestLibrary.a product again. Switch to Finder’s column view to see the folders in the hierarchy and you’ll see a new folder called Debug-Universal (or Release-Universal if you built the Release version), which contains the Universal version of the library, as shown below:
Note: If you have made any changes to your storyboard/nib & you observe that your resource bundle is visible in red color as shown below(fig 1.12) or you are not able to locate it in finder, then you need to select your bundle resources from the edit scheme(fig 1.13) & build the bundle resources. 


How to use this Static library in App?

  • Create NewProject->TestProject(You Project Name).Add library resource files in the test project following the below folder structure
  1. Note: The typical convention is an include folder for the header files, and a lib folder for the library files (.a). This folder structure is only a convention; it’s not mandatory in any way. You don’t need to follow this structure or copy the files inside your project’s folder. In your own apps, you can have the header and library files in any location of your choice, as long as you set the proper directories when you configure the Xcode project later on.

 

Linking your static library to the XCode

  • Click on the project root node in the project navigator 
  • Select the TestProject target.
  • Select Build Settings.
  • Locate the Header Search Paths setting in the list. You can type “header search” in the search box to filter the big list of settings if necessary.
  • Double click on the Header Search Paths item, and a popover will appear. Click the + button, and enter the following:
$SOURCE_ROOT/include
 

Note: You will get a linker error when you try to import your library in your project, to get rid of it follow the below steps
(1) return to the build settings for the TestProject target
(2). Select the Build Phases tab
(3), and expand the Link Binary With Libraries section
(4). Finally, click the + button in that section
(5).click on the Add Other… button and locate the libTestLibrary.a library file in the lib subdirectory inside the project’s root folder
6. The final step is to add the -ObjC linker flag. Click on the Build Settings tab, and locate the Other linker Flags settings.



  • Now your library is SUCCESSFULLY linked with your project.
 
Happy Coding ...!!!!!

No comments:

Post a Comment