Check out iOS and Android quick start guides as well
In this tutorial I am going to show you how to write a native plugin for macOS. This sample plugin will be getting battery precent
You will need:
-
a machine that runs macOS
-
xcode
-
corona enterprise installed and ready
-
corona simulator
First go to your corona enterprise folder, open ProjectTemplates, then copy and paste the “App” folder on to desktop (or somewhere you want to save this project)
Next lets open the “App” folder you just made a copy of.
Open the “mac” folder
open “Plugin.xcodeproj” in xcode
lets remove references to everything in the shared folder in Xcode
Let use the PluginLibrary.h and PluginLibrary.m from the iOS folder or just download these files( which are from the iOS plugin folder).
https://www.dropbox.com/sh/wuuas0zkhyniz2j/AAA9qDl3U_pd9ihhGCIR7zBFa?dl=0
Then drag PluginLibrary.h and PluginLibrary.m to the “Shared” folder
Next lets do some editing.
open PluginLibrary.h change
CORONA\_EXPORT int luaopen\_plugin\_library( lua\_State \*L );
to
CORONA\_EXPORT int luaopen\_plugin\_batteryPercent( lua\_State \*L );
now let open PluginLibrary.mm
lets change our imports from this
#import "PluginLibrary.h" #include "CoronaRuntime.h" #import \<UIKit/UIKit.h\>
to
#import "PluginLibrary.h" #import "CoronaRuntime.h" #include "CoronaAssert.h" #include "CoronaEvent.h" #include "CoronaLua.h" #include "CoronaLibrary.h" #include "CoronaRuntime.h" #include \<CoreFoundation/CoreFoundation.h\> #include \<IOKit/ps/IOPowerSources.h\> #include \<IOKit/ps/IOPSKeys.h\>
Lets next look at the PluginLibrary class and change our methods from this
protected: static int Finalizer( lua\_State \*L ); public: static Self \*ToLibrary( lua\_State \*L ); public: static int init( lua\_State \*L ); static int show( lua\_State \*L );
to just include one method called getBattery
protected: static int Finalizer( lua\_State \*L ); public: static Self \*ToLibrary( lua\_State \*L ); public: static int getBattery( lua\_State \*L );
now let go down to link up our project with lua, let call ours batteryPercent
change this
const char PluginLibrary::kName[] = "plugin.library"; // This corresponds to the event name, e.g. [Lua] event.name const char PluginLibrary::kEvent[] = "pluginlibraryevent";
to this
const char PluginLibrary::kName[] = "plugin.batteryPercent"; // This corresponds to the event name, e.g. [Lua] event.name const char PluginLibrary::kEvent[] = "batteryPercent";
Let navigate to PluginLibrary::Open
lets get rid of show and init
const luaL\_Reg kVTable[] = { { "getBattery", getBattery }, { NULL, NULL } };
next lets delete the show method
int PluginLibrary::show( lua\_State \*L ) { NSString \*message = @"Error: Could not display UIReferenceLibraryViewController. This feature requires iOS 5 or later."; if ( [UIReferenceLibraryViewController class] ) { id\<CoronaRuntime\> runtime = (id\<CoronaRuntime\>)CoronaLuaGetContext( L ); const char kDefaultWord[] = "corona"; const char \*word = lua\_tostring( L, 1 ); if ( ! word ) { word = kDefaultWord; } UIReferenceLibraryViewController \*controller = [[[UIReferenceLibraryViewController alloc] initWithTerm:[NSString stringWithUTF8String:word]] autorelease]; // Present the controller modally. [runtime.appViewController presentViewController:controller animated:YES completion:nil]; message = @"Success. Displaying UIReferenceLibraryViewController for 'corona'."; } Self \*library = ToLibrary( L ); // Create event and add message to it CoronaLuaNewEvent( L, kEvent ); lua\_pushstring( L, [message UTF8String] ); lua\_setfield( L, -2, "message" ); // Dispatch event to library's listener CoronaLuaDispatchEvent( L, library-\>GetListener(), 0 ); return 0; }
with our getBattery method
int PluginLibrary::getBattery( lua\_State \*L ) { CFTypeRef blob = IOPSCopyPowerSourcesInfo(); CFArrayRef sources = IOPSCopyPowerSourcesList(blob); CFDictionaryRef pSource = NULL; const void \*psValue; int numOfSources = CFArrayGetCount(sources); if (numOfSources == 0) return 1; for (int i = 0 ; i \< numOfSources ; i++) { pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i)); if (!pSource) return 2; psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey)); int curCapacity = 0; int maxCapacity = 0; int percent; psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey)); CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity); psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey)); CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity); percent = (int)((double)curCapacity/(double)maxCapacity \* 100); lua\_pushnumber(L, percent); return 1; } lua\_pushnil(L); return 1; }
lastly at the bottom lets change the name of our plugin from library to batteryPrecent
change this
CORONA\_EXPORT int luaopen\_plugin\_library( lua\_State \*L ) { return PluginLibrary::Open( L ); }
to this
CORONA\_EXPORT int luaopen\_plugin\_batteryPercent( lua\_State \*L ) { return Corona::PluginLibrary::Open( L ); }
next let hit the play button in the top
You “may” get this
“error: mkdir: /Library/Application Support/Corona/Simulator/Plugins: Permission denied”
this is ok
let copy the plugin to simulator
to do that left click the plugin_library.dylib in the Products folder
let copy and paste it on the desktop real quick
and rename it from plugin_library.dylib to plugin_batteryPercent.dylib
next lets go to the finder and click go
Paste in
/Users/"insert username here"/Library/Application Support/Corona/Simulator/Plugins
copy (will need a the copy on the desktop later) the plugin we just made (and hopeful on your desktop) into this folder.
let open up corona simulator
and create an new project call it what ever you want (i called mine batteryStatus)
open up the main.lua with a text editor and paste this in
local batteryPercent = require("plugin.batteryPercent") local myText = display.newText("Precent:"..batteryPercent.getBattery(), display.contentCenterX, display.contentCenterY, native.systemFont )
Your screen should look like this
https://www.dropbox.com/s/gizk2w2mh9l21cc/Screen%20Shot%202016-10-02%20at%2011.46.34%20PM.png?dl=0
now let build for macOS (corona still calls it OS X)
https://www.dropbox.com/s/jkiteifiune6g6y/Screen%20Shot%202016-10-02%20at%2011.49.36%20PM.png?dl=0
when your done let open up the app that was just built
open “Contents” and create a new folder called “Plugins”
open the “Plugins” folder you made
and copy the plugin_batteryPercent.dylib here
open the app and ta da
https://www.dropbox.com/s/a5oeelseobk1e1n/Screen%20Shot%202016-10-02%20at%2011.58.09%20PM.png?dl=0
Check out the code in the link below and get coding
https://www.dropbox.com/sh/1od23in41nat3an/AADflmHSYdM6LARqNjjGmDOMa?dl=0
P.S sorry for any bad grammar, it is late