Bridging between Swift and Objective-C in the Same Project

In this tutorial you will call objective-C from swift using bridging header.

Swift and Objective-C in the Same Project
Swift’s compatibility with Objective-C lets you create a project that contains files written in either language. You can use this feature, called mix and match, to write apps that have a mixed-language codebase. Using mix and match, you can implement part of your app’s functionality using the latest Swift features and seamlessly incorporate it back into your existing Objective-C codebase.

Mix and Match Overview
Objective-C and Swift files can coexist in a single project, whether the project was originally an Objective-C or Swift project. You can simply add a file of the other language directly to an existing project. This natural workflow makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.

The process for working with mixed-language targets differs slightly depending on whether you’re writing an app or a framework. The general import model for working with both languages within the same target is depicted below and described in more detail in the following sections.

swift

swift

Importing Code from Within the Same App Target
If you’re writing a mixed-language app, you may need to access your Objective-C code from Swift and your Swift code from Objective-C. The process described in this section applies to non-framework targets.

Reference: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

Open any of your xcode projects (swift based) using xcode6. (Use this one, https://github.com/datomnurdin/SimpleiOSApp). Create new Objective-C Implementation (.m file) to your project. File > New > File.

xcode6 - new file

xcode6 – new file

Select iOS > Source > Objective-C File. Click Next.

xcode6 - Objective-C File

xcode6 – Objective-C File

Name it CustomObject. Click Next and Create.

xcode6 - new file

xcode6 – new file

When adding your .m file, you’ll likely be hit with a prompt that looks like this below and click Yes.

Objective-C bridging header

Objective-C bridging header

Your CustomObject.m and SimpleiOSApp-Bridging-Header.h have been created.

CustomObject.m and SimpleiOSApp-Bridging-Header.h

CustomObject.m and SimpleiOSApp-Bridging-Header.h

Add another .h file and name it CustomObject.hFile > New > File. Select iOS > Source > Header File. Click Next.

Header File

Header File

Name it CustomObject and click Create.

CustomObject.h

CustomObject.h

Your CustomObject.h just created.

CustomObject.h

CustomObject.h

Build your Objective-C Class

In CustomObject.h

#import <Foundation/Foundation.h>

@interface CustomObject : NSObject

@property (strong, nonatomic) id someProperty;

- (void) someMethod;

@end

In CustomObject.m

#import "CustomObject.h"

@implementation CustomObject 

- (void) someMethod {
    NSLog(@"I love you iena!!");
}

@end

Now add Class to Bridging-Header. In YourProject-Bridging-Header.h (For this tutorial is SimpleiOSApp-Bridging-Header.h):

#import "CustomObject.h"

Then use your Object. In ViewController.swift after viewDidLoad() function.

var instanceOfCustomObject: CustomObject = CustomObject()
instanceOfCustomObject.someProperty = "Hello Iena"
println(instanceOfCustomObject.someProperty)
instanceOfCustomObject.someMethod()

Then run your project. Check your console.

xcode6 console

xcode6 console

Output

Hello Iena
2014-12-30 15:10:16.760 SimpleiOSApp[2114:74245] I love you iena!!

How to adjust status bar properly for iOS (hybrid app)

Sometimes you will see your status bar not properly adjust in your iOS. Here got some tip and trick to fix your problem.

Put this code inside your MainViewController.m right after MainViewController @implementation and before @end.

- (void)viewDidLayoutSubviews{

    if ([self respondsToSelector:@selector(topLayoutGuide)]) // iOS 7 or above
    {
        CGFloat top = self.topLayoutGuide.length;
        if(self.webView.frame.origin.y == 0){
            // We only want to do this once, or if the view has somehow been "restored" by other code.
            self.webView.frame = CGRectMake(self.webView.frame.origin.x, self.webView.frame.origin.y + top, self.webView.frame.size.width, self.webView.frame.size.height - top);
        }
    }
}

Before

status bar (before)

status bar (before)

After

status bar (after)

status bar (after)

iOS – Adding native functionality to hybrid application with Apache Cordova plugin

Previous tutorial I already discuss about how to adding native functionality hybrid application with Apache Cordova Plugin for android. Today I would like to give you knowledge for iOS part.

phonegap (cordova) + ios

phonegap (cordova) + ios

Try implement this example into your project.

Make sure you already define your plugin in your config.xml

<feature name="CustomPlugin">
      <param name="ios-package" value="CustomPlugin" />
</feature>

Implementing the plug-in by using Objective-C code

CustomPlugin.h

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

@interface CustomPlugin : CDVPlugin

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

@end

CustomPlugin.m

#import "CustomPlugin.h"

    @implementation CustomPlugin

    - (void)sayHello:(CDVInvokedUrlCommand*)command{

        NSString *responseString =
            [NSString stringWithFormat:@"Hello World, %@", [command.arguments objectAtIndex:0]];

        CDVPluginResult *pluginResult =
            [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:responseString];

        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }

    @end

Calling a plug-in from JavaScript

function initial(){
    var name = $("#NameInput").val();
    cordova.exec(sayHelloSuccess, sayHelloFailure, "CustomPlugin", "sayHello", [name]);
}

function sayHelloSuccess(data){
    alert("OK: " + data);
}

function sayHelloFailure(data){
    alert("FAIL: " + data);
}

I hope this simple tutorial can help you to integrate native functionality with hybrid application. Stay tune for my next push notification tutorial for iOS and android.