Skip to main content

How Cross Platform works in native world

Cross Platform:

Cross platform means here single application or codebase run on multiple mobile platform(iOS, android).

Today most of the organization focusing on mobile world. When an organisation take first step in the domain the very first question in there mind is.

Should i go with native development or cross platform?

There Decision based on the some factors

Native (Pros): 

1) Native Look and feel
2) High Performance
3) No restriction over device capability (Like camera, Bluetooth) etc
and Many more

Native (Cons):

1) More resources require to develop native application. i.e more money
2) Training require for each platform.

CrossPlatform (Pros):

1) Developer need to know only single technology or language (Javascript and Html). 
2) Less resources require for developing and maintaining application.
3) Distribution can be done like other native apps (App store, Play store) 
  
 CrossPlatform (Cons):

1) Application developed using HTML have look and feel like web pages.
2) Some Platform provides native look and feel like Titanium, Kony, But when we need to focus on performance like execution time, memory handling then again we need to look into native side.
3) There are some restriction on device capability.
4) Major drawback in case of cross platform frame work is we cannot to be up to date with the technology coming. 

Types of Cross platform mobile application:

HTML and CSS based Applications:

These are like a mobile website created with HTML and CSS. Phone-gap is most popular platform in this category. Such apps have poor performance because javascript component are two heavy and slow for mobile application. They provide some native capability like camera, open url etc. How they provide native access will be discuss in later part.

Hybrid cross platform Applications:

Mobile operating system provides support to javascript through javascript engine. These platform use this support to develop the framework for application. In case of iOS this engine is javascript core and android has V8

The advantage of these applications over HTML is it provide you native look and feel. All the component will be native. But the interaction between javascript and native is little bit slow. Also you do to have access all the api of native until it is exposed by the framework developer. Next section will explain how you can expose or develop this kind of framework for iOS.

How these application interact with native environment:

HTML and CSS based Applications:

A HTML based application will run in a web view. All your code render in the web view. You do not have direct access to device capability. Following is the architecture for a HTML based application:



So the question is how your application launch the camera application from the web view. If we only have a web view to render the code. In iOS here is the trick:

There is abstract class called NSURLProtocol which can capture all the request going from your application. So you can redefine all the network handling for your application using this class.

We will implement this class to open a camera from a web-view.
Create a project and add a web view to your application. Crate a class let be "CrossURLProtocol" and inherit it from NSURLProtocol.

Register the Protocol with the loading system:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [NSURLProtocol registerClass:[CrossURLProtocol class]];
    return YES;
}

CrossURLProtocol.m

Override the method :
These method will be called every time whenever a request is made from your application and you are returning No here. That means default loading system will handle this. If you want to handle it yourself then you must return Yes. 

+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
    return NO;
}

Let suppose you want to open camera from the application then you must write the code in following manner:

+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
    NSString* urlString = request.URL.absoluteString;
    if([urlString hasPrefix:@"Camera:"]){
        //Write a code to open camera
        return NO;
    }
    return NO;
}

Now whenever you open url in html with camera prefix:
"window.location.href = 'Camera://'"

This will open the camera in the application. So in this manner you can write your own protocol and provide a framework for the mobile web application.

Hybrid cross platform Applications:

An application written in javascript language which interact with native component at run time through javascript engine. You can access all the device capability just need to write code to handle that. Look and feel of application is like a native application.




So now question is how to develop framework which can support this type of application. Create a application in Xcode and add a "JavaScriptCore.framework" in the application.

Create a class JavascriptHandler indoor project.
Make sure your class adopt the protocol JSExport.

Import the header file in your class:
#import <JavaScriptCore/JavaScriptCore.h>

JavascriptHandler.h

@interface JavascriptHandler : NSObject<JSExport>

- (NSString*) testName;

@end

JavascriptHandler.m

@implementation JavascriptHandler

- (NSString*) testName{
    return @"java Script Core Test";
}

@end

Now in your main view controller add a webview and try to load any simple html page with single button.

ViewController.m

In web view Delegate method:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // get JSContext from UIWebView instance
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    // register JavascriptHandler class
    context[@"JavascriptHandler"] = [JavascriptHandler class];
  

}

Now try to call the method "JavascriptHandler.testName()" from your html page on button click. It will return the same value. i.e it . You learn first step to build a framework. In this manner you can provide all the API to javascript developer for native handling.

There are other Hybrid platform like Xamarin which use C#language to develop the application for cross platform. Approach is totally different in that case. It works on compile time.   
  

  

Comments

Popular posts from this blog

iOS8 UIWebView Remove or Modify Keyboard Toolbar

Remove Toolbar: One of my client requirements is to remove the default toolbar for the keyboard. I found numerous examples for iOS7 to remove toolbar from the keyboard window. But it did not work for iOS8. So i came to following solution after some Research on Google: Step1:   First we need to add observer for keyboard notification: [[ NSNotificationCenter defaultCenter ]  addObserver : self selector : @selector ( removeKeyboardTopBar :) name : UIKeyboardWillShowNotification object : nil ]; Step 2: We need to implement the method for observer: - ( void )removeKeyboardTopBar {     } Step 3: Find the keyboard window from all the application  windows:   - ( void )removeKeyboardTopBar {     UIWindow *keyboardWindow = nil ;     UIView * toolBarContainer = nil ;     NSArray * windows = [[ UIApplication sharedApplication ] windows ];     for ( U...

Implement orientation modes in iPhone Hybrid Applications

Let suppose you are working on a hybrid application which runs only in single (portrait) mode. One day a requirement come that PDF and Doc Viewer (HTML Page) should support both (landscape, portrait) mode. Your Application loads all the HTML content from the local html files and you need to implement the above functionality only for one HTML file. Let break the above task in the modules: Step 1: Application should detect when the PDF and Doc viewer is open in application. I setup location.href tag in html to " docvieweron:// " and " docvieweroff:// " when page is open and closed respectively. In this way I am getting a delegate callback in web view: WebViewDelegate: - ( BOOL ) webView:( UIWebView *)webView shouldStartLoadWithRequest:( NSURLRequest *)request   navigationType: ( UIWebViewNavigationType )navigationType {          NSString * urlString = [[request URL ] absoluteString ];...

Blocks are Objective-C Objects !!!

Blocks Blocks are executable code just like a function.  It can be written inside a function or we can store the reference in a variable and call it later.  But wait a minute these all functionality we can achieve by function pointer and functions.  Then what is so special about blocks? Let first go through how we can define blocks. int (^sum)( int , int ) = ^( int number1, int number2){ return number1+number2; }; The above block can be used to find the sum of two numbers.  If we recall the function pointer in c then it is very much similar to above: int sumFunction ( int number1, int number2){ return number1+number2; } int (*sum) ( int , int ) = sumFunction; //Function Pointer *sum Then Why we need Blocks if we already have Function pointer?  1) B locks can capture variables from the enclosing scope by simply referring to them within the block. How ? Let see the next Section 2)  Blocks...