Skip to content
Guillaume Gendre edited this page Aug 7, 2015 · 55 revisions

Adding Cobalt to an iOS Project

This page describes how to add Cobalt to an existing iOS Project with XCode.

If you want to create a new project, you'd better use the CLI, as described here, it's a way more easy technique for newcomers.

Adding Cobalt

Once you created your project with Xcode (if you are not familiar with it, follow the steps in the Create a New Project section at Apple documentation), and cloned the Cobalt-iOS repository, drag-and-drop the Cobalt.xcodeproj file into your project workspace.

Note: if you have not already saved your project in a workspace, you will be prompted to do it.

Then, in the General tab in your app target in your project file, click on the add button of the Embedded Binaries section as below and select the Cobalt.framework.

Image

Linking to a www folder

This folder contains the web part of the application : web pages, styles, javascript libraries.

Let's start by creating a www folder in your app directory and add it to the project by drag & drop.

Image

Notice that we have ticked folder reference to keep files organisation within the bundle file. Do not tick "copy items[...]" option if you want the content to be automatically updated. The created group will appear in blue in Xcode Project Navigator

Initialise the web side

Once the www folder is linked to the projet, it is time to make it yours and initialise cobalt on the web side.

Jump to the Web Setup page to do this. Don't forget to come back ;)

Load your first html page

Create a Cobalt Controller

In your controller header file, import "CobaltViewController.h" and make the ViewController inherit of it and implement CobaltDelegate protocol.

#import "CobaltViewController.h"

@interface ViewController : CobaltViewController<CobaltDelegate>

@end

Then you need to implement methods required by the CobaltDelegate protocol in the ViewController:

#import "ViewController.h"

@implementation ViewController

- (BOOL)onUnhandledMessage:(NSDictionary *)message
{
    return NO;
}

- (BOOL)onUnhandledEvent:(NSString *)event withData:(NSDictionary *)data andCallback:(NSString *)callback
{
    return NO;
}

- (BOOL)onUnhandledCallback:(NSString *)callback withData:(NSDictionary *)data
{
    return NO;
}

@end

Load the page you want in the didFinishLaunchingWithOptions method

Finally, you have to declare your web folder location and load your ViewController from the AppDelegate:

#import "Cobalt.h"
#import "CobaltViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [Cobalt setResourcePath:myResourcePath];    // default: [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"/www/"]
    UIViewController * viewController = [CobaltViewController cobaltViewControllerForController:@"default" andPage:@"index.html"];
    UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    self.window.rootViewController = navigationController;
    
    [self.window makeKeyAndVisible];
    return YES;
}

By default index.html should be loaded if you don't specify any page.

Enable debug

Add the following line in your project prefix file in order to activate Cobalt logs

#define DEBUG_COBALT    1
Clone this wiki locally