How to develop Android apps in C or C++ embed the Native Development Kit. In this tutorial, I’ll introduce how to use C or C++ together with Objective-C in iOS. Unlike Android which wants a special API (the NDK) to support native development, iOS helps it by default.
What Is Objective-C++?
Objective-C++ may sound like a new programming language, but it is same as previous. It’s a combined with two languages, Objective-C and C++. Apple provides Objective-C++ as a comfortable mechanism for mixing Objective-C code with C++ code.
Objective-C is close to C but with object-oriented features achieve as a thin layer on top of C. It’s a
tough superset of C which makes any C code a valid Objective-C program.
Even though Swift is now the suggested language for developing iOS apps, there are still good reasons to use past languages like C, C++ and Objective-C. Regardless the quick rise of Swift, Objective-C is still the principal language on iOS because of the sheer number of extant apps and libraries already created with it.
One reason to use Objective-C is to port an existing C/C++ program written for another platform to iOS. Developing cross-platform apps using C or C++ is attainble with some careful planning. Despite Swift being open source, it’s not yet fully supported other platforms.
Create the Project
Open Xcode and choose Create a new Xcode project.
In the template selection screen, choose Single View Application from the iOS Application tab and clickNext.
In the project options screen, name the product HelloCpp. Enter your organization and organization identifier in reverse domain name style.
Because it’s not really a language, there’s no option to create an Objective-C++ project. The only available source is either Objective-C or Swift. For this project, choose Objective-C. Keep all other ooption as it is and click Next and choose a folder to save the project.
It is the Time to add some C++ code. If this is your first time with C++, check out this tutorial on the language. Look at the Project Navigator pane on the left. Most of the files end with either an .h or .m. Those that end with .h are header files while those with .m are Objective-C source files.
Create a C++ class that will be called from an Objective-C file.
Name the file Greeting, keep the Also create a header file box checked and click Next. Save the file inside the HelloCpp folder.
The project’s structure should now look like the following. Feel free to drag files around to modify the arrangement in the Project Navigator.
Open Greeting.hpp and add the following code between the
include <stdio.h> and
#endif /* Greeting_hpp */ lines:
Define these methods in Greeting.cpp by adding the following code after the
This is simple code that creates a class named
Greeting with a single method named
greet() that returns a string value.
Using Objective-C with C++
Now that you’ve added the simple C++ Greeting class, try calling this from Objective-C. OpenViewController.m and import the Greeting.hpp header file:Variable Delaration
If “Show live issues” is selected then it display error message to overcome this issue rename the file Viewcontroller.mm while edit the above code.
To Add an Button in this app to make it more interactive. follow the below procedure.
Now give the name to that Created Button.
Open ViewController.h and add the following
IBAction method between
Define this method in ViewController.mm with the following code inserted after the
- (void)didReceiveMemoryWarning function and before
Build and run the app. Notice that the button appears off-center on the simulator. Fix this by selecting Auto Layout constraints using the Control + drag technique. Connect the button to its packet and enable both vertical and horizontal centering. Run the app again to notice the improvements.