-
Notifications
You must be signed in to change notification settings - Fork 15
IOS Setup
This page describes how to add Cobalt to an existing iOS Project with XCode.
If you want to create a new project, you should better use the CLI, as described here, it is the easiest way for newcomers.
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-Framework repository, drag-and-drop the Cobalt.framework & Cobalt.bundle files into your project navigator.
Note: do not forget to tick Copy items if needed & Create groups options like below.
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 into the project navigator.
Then, select the project file in the navigator and in the central pane, choose the application target, click on the Build Phases tab, expand the Target dependencies section and click the + button.
Here, select the Framework target of the Cobalt project and click the Add button.
Then expand one after the other the Cobalt.xcodeproj file and the Products folder in the navigator.
Expand the Link Binary with Libraries section and drag-and-drop the libCobalt.a from the navigator into the list.
Then expand the Copy Bunble Resources section and drag-and-drop the Cobalt.bundle from the navigator into the list.
Finally, in order to enable Cobalt to send Application Lifecycle events to the Web, you need to complete the following methods with these instructions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[NSNotificationCenter defaultCenter] postNotificationName:kOnAppStarted object:nil];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
[[NSNotificationCenter defaultCenter] postNotificationName:kOnAppBackgroundNotification object:nil];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
[[NSNotificationCenter defaultCenter] postNotificationName:kOnAppForegroundNotification object:nil];
}
Finally, in order to enable Cobalt to send Application Lifecycle events to the Web, you need to complete the following methods with these instructions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[NSNotificationCenter defaultCenter] postNotificationName:kOnAppStarted object:nil];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
[[NSNotificationCenter defaultCenter] postNotificationName:kOnAppBackgroundNotification object:nil];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
[[NSNotificationCenter defaultCenter] postNotificationName:kOnAppForegroundNotification object:nil];
}
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.
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
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 ;)
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
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.
Add the following line in your project prefix file in order to activate Cobalt logs
#define DEBUG_COBALT 1
Cobalt is an Open-Source Hybrid Mobile Framework. Read more about it on the home page or in the documentation
- Introduction to Cobalt navigation
- The cobalt.json file
- native navigation bars
- Handling the Android back button
- Introduction to Cobalt messages
- Publish/Subscribe on the Web side
- Publish/Subscribe on Android
- Publish/Subscribe on iOS
- Web Lifecycle messages
- Pull-To-Refresh and Infinite Scroll
- Custom alerts and Toasts
- LocalStorage
- OpenExternalUrl
- PlatformInfos
- Ajax
- Removing the top bar
- Adding Cobalt to an existing project
- Customizing your hybrid views
- Handle multiple Cobalt webviews on the same screen (TODO)