Foreword
This is a continuation of the translation of a series of articles about creating extensions for OpenFL from Laurent Bédubourg. In the
first part, we created a simple extension and compiled it for native platforms (Linux / Windows, Android, iOS). In this part we will add the ability to send tweets to our iOS application.
What we learn:
- how to structure the source code of our extension for various platforms
- how to link haxe code and functions from our extension
- how to link with iOS frameworks (with the Twitter framework in particular)
What to do?
Let's start by looking at how the
NME project is structured, for example.
At first glance,
Build.xml looks complicated and we will return to it later. Looking at the contents of the directory, we will see the following:
- include / contains header files to be implemented in common / and plaforms
- common / platform-independent c ++ code (more or less platform-independent, we can use #if defined (HX_xxxx) to achieve maximum platform-independence)
- common / ExternalIterface.cpp exports functions to the runtime (DEFINE_PRIM)
- platform in this directory contains implementations for specific platforms (iPhone, Mac, windows and others)
In my opinion, such a structure looks quite comfortable, let us and we will use it.
')
Let's start with our previous extension, we have to declare the Tweet () function in the existing include / Util.h file (by the mind, we have to use a separate header file, but today I am too lazy to do this).
namespace testextension { int SampleMethod(int inputValue); bool Tweet(const char* msg); }
Now we need to implement it for the iphone platform:
cd project mkdir iPhone
Create an iPhone / Tweet.mm file:
#import <Foundation/Foundation.h> #import <Twitter/Twitter.h> namespace testextension { bool Tweet(const char* message){ // Objective-C // // // :) NSString* str = [[NSString alloc] initWithUTF8String:message]; TWTweetComposeViewController* tweetView = [[TWTweetComposeViewController alloc] init]; [tweetView setInitialText:str]; TWTweetComposeViewControllerCompletionHandler completionHandler = ^(TWTweetComposeViewControllerResult result) { [[[[UIApplication sharedApplication] keyWindow] rootViewController] dismissModalViewControllerAnimated:YES]; }; [tweetView setCompletionHandler:completionHandler]; [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentModalViewController:tweetView animated:YES]; return true; } }
And register the Tweet function in haxe, for this we edit the file common / ExternalInterface.cpp:
static value testextension_tweet(value message){
DEFINE_PRIME, val_get_string, val_true and everything else are part of hxcpp and are defined in
hx / CFFI.h.Learning about
ExternalInterface.cpp from NME will help you understand how to register your functions.
The haxe part is in TextExtension.hx and looks like this:
class TestExtension {
How to compile
I had to make an effort to figure out how to compile and link the project, but as a result, everything turned out to be quite simple.
First you need to create a project / Build.xml file:
<files id="iphone"> <file name="iPhone/Tweet.mm"/> </files> <files id="mac"> <file name="Mac/Tweet.mm"/> </files>
In the section with id = NDLL, you need to add files that will be compiled:
<files id="mac" if="mac"/> <files id="iphone" if="ios"/>
Finally, we need to add the Twitter framework depending on the include.xml. Through this file, hxcpp will know what frameworks need to be added to our application.
<dependency name="Twitter.framework" if="ios"/>
I compiled the extension, just like I did in the
first part .
haxelib run hxcpp Build.xml -Dmac haxelib run hxcpp Build.xml -Diphoneos -DHXCPP_ARMV7 haxelib run hxcpp Build.xml -Diphonesim
How to start
To test the operation of the extension, I added the following call to my test application:
TestExtension.tweet("This is my tweet message");
And I launched it in the simulator and on my iOS device:
cd TestApp openfl test project.xml iphone -simulator openfl test project.xml iphone

We can tweet using the native API from our application! Cool, is not it? :)
I want to note one very important thing - the way I got the information - I found it in the source code of hxcpp and nme, on a githaba.
Trying to make it all work, I ran into several problems. If you are experimenting and something is not working out for you, do not forget to clear the ndl / and project / obj directories and rebuild the entire project. I continued to experiment until I found a suitable format for Build.xml and include.xml.
The only real difficulty is the lack of documentation on the Build.xml format in hxcpp. But, thank God, there are examples on which to learn.
The next thing I want to do is try to build an extension for android to make sure everything works on this platform. They say that not only developing an extension to java, but also describing this process will be useful :)
I updated the
github expansion repository, in the hope that my small contribution would be useful to other haxers! I hope this tutorial will work on your system ... although sometimes things don’t go as far as they think;)