The last time Hackerfall tried to access this page, it returned a not found error. A cached version of the page is below, or clickhereto continue anyway

Create and Import an Universal Library in a IBM Worklight Project – Die for Coding

Create Static Library

Open Xcode and select File \ New \ Project.

When the dialog show appers select iOS\Framework & Library\Cocoa Touch Static Library:

Insert the fields:

  1. Product Name : PocLibreria
  2. Organization name : MyOrgName
  3. Company Identifier : GT

Click Next. Now create a new NSObject in our project:

Select Cocoa Touch and Objective-C class, appoint the class PocLibreria and dont touch the field class (NSObject):

Open PocLibreria.h and write

#import <Foundation/Foundation.h>

@interface PocLibreria : NSObject

// return a dictionary : @{@"newstring": newstring, @"isUpper" : @"YES" / @"NO"}
-(NSDictionary *) myFunction:(NSString*) str1 andUpperCase:(BOOL)upper;

@end

Now open PocLibreria.m and write our method to return upper case string:

#import "PocLibreria.h"

@implementation PocLibreria

/*! Return an uppercase string or not
 \param str1 my string
 \param upper if = YES return uppercase string otherwise no
 \returns the result is a dictionary with two keys: 
  1) newstring: the new string  
  2) isUpper upper case value
 */
-(NSDictionary *) myFunction:(NSString*) str1 andUpperCase:(BOOL)upper{
 
    if(upper == TRUE)
        return @{@"newstring" : str1.uppercaseString, @"isUpper": @"YES"};
    else
        return @{@"newstring" : str1, @"isUpper": @"NO"};
}

@end

Now, we need to build two libraries (one for architecture), we need to compile twice, one for simulator and the other one for iOS Device

Simulator iOS Device

We have created a static library!

Open your desktop and create a new folder: Lib.

We have to take the two newly created library, open Xcode and in Product:

This will open the Finder and we will be in Debug-iphoneos Open parent folder as well as being in front of two folders:

  1. Debug-iphoneos
  2. Debug-iphonesimulator

Open Debug-iphoneos and copy libPocLibreria.a paste in a Desktop folder Lib and rename the file in libPocLibreria_iosdevice.a. Repat the same operations by copying libPocLibreria.a in Debug-iphonesimulator and rename it in libPocLibreria_simulator.a

Convert in Universal Library

open the terminal and run the command:

$ cd ~/Desktop/Lib
$ lipo -create "libPocLibreria_simulator.a" "libPocLibreria_iosdevice.a" -output "libPocBiblioteca.a"

Finally we have created our Universal Library.

Worklight

  1. Create new Worklight Hybrid Application PocLibreriaTest
  2. Create new Worklight Environment for iOS

Open file index.html in myApplication/common and append this code under the <body>:

<input id = "string" value = "" type="text"/><br />
<button onclick="useLibrary();">Use the library</button>

Create a new javascript file in common\js and rename in library.js , import this file in index.html:

<script src="js/library.js"></script>

Open library.js and write the new method useLibrary();:

function useLibrary(){
	
	// Cordava plugin
	var str = document.getElementById("string").value;
	cordova.exec(
		function(result){
			alert("Il risultato e' : " + result);
		},
		function(err){
			console.log("Err + " + err);
		}, 
		"BRTestLibreria", 
		"useLibrary", 
		[str]
	);
}

Build All Environment and open PocLibreriaTest in Xcode (click on iOS environment and select Run on Xcode).

Create a new group MieiFramework and drag the Universal Library libPocBiblioteca.a into the group MieiFramework, drag the header file PocLibreria.h into the folder Classes:

When we drag the files in our project you have to add them to the target, lets make sure that there is importing the targets Add to:

If you look closely you will see two files BRTestLibreria.h and BRTestLibreria.m, as well creates the file we created in the static library Libreria.h and Libreria.m

Open BRTestLibreria.h and change the type of class NSObject in CDVPlugin as shown in the following code:

#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>
#import <UIKit/UIKit.h>

@interface BRTestLibreria : CDVPlugin

-(void)useLibrary:(CDVInvokedUrlCommand*) command;

@end

Open BRTestLibreria.m and write:

#import "BRTestLibreria.h"
#import "PocLibreria.h"

@implementation BRTestLibreria

-(void)useLibrary:(CDVInvokedUrlCommand*) command;{
    
    NSString *result = [[[PocLibreria alloc] myFunction:[command.arguments objectAtIndex:0] andUpperCase:YES] objectForKey:@"newstring"];

    CDVPluginResult *plugInResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:result];
    [self.commandDelegate sendPluginResult:plugInResult callbackId:command.callbackId];
    
}

@end

We can run our project and we should get something similar to:

Sometimes, it depends on god belief, may not work the first time. Xcode might return the error that it can not find the folder WorklightSDK.

To solve this problem we go on and we go into Xcode project properties by clicking on PocLibreriaPocLibreriaEsternaIphone . At the top we have:

Well we go on Build Settings and in the small search bar try: search framework and click on the one element found:

Lets add another element to this list: $ (SRCROOT) / MieiFramework

Lets try again to launch the app and hope!

If still not working, doing the usual tests in style bluild / clean / open / close / Im going home / back / eat / and reboot, then delete the Environment created by Eclipse, saving files created by Xcode first (then BRTestLibreria. *), and recreate the Environment including:

  1. libBiblioteca.a
  2. Libreria.h
  3. BRTestLibreria.*
  4. good luck

2014 Gianluca Tursi

iosjavascriptcodecocoalibraryibm worklight Create and Import an Universal Library in a IBM Worklight Project was published on March 22, 2014 (revised: 08/20/2013) Gianluca Tursi

Continue reading on gianlucatursi.github.io