iOS Quickstart Tutorial

Hi Corona Team,

just tried the iOS quickstart tutorial and I got a problem:

As soon as I tap the “Flash Light on” text I got the following error:

ERROR: Runtime error

/Volumes/G-Drive/Projekte_Corona_Enterprise/Flashlight/ios/…/Corona/main.lua:27: attempt to call field ‘on’ (a nil value)

Can you explain what went wrong?

Best

René

Can you zip up your project and share a link to it?

Rob

Sure, here is a dropbox link:
https://www.dropbox.com/s/42wyd8o68r3nkta/Flashlight.zip?dl=0

You missed a step on the plugin code:

You have this:

PluginLibrary::Open( lua\_State \*L ) { // Register \_\_gc callback const char kMetatableName[] = \_\_FILE\_\_; // Globally unique string to prevent collision CoronaLuaInitializeGCMetatable( L, kMetatableName, Finalizer ); // Functions in library const luaL\_Reg kVTable[] = { { "init", init }, { "show", off }, // \<----- needs to be "off" not "show" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ "show", on }, // \<----- needs to be "on" not "show" { NULL, NULL } }; // Set library as upvalue for each library function Self \*library = new Self; CoronaLuaPushUserdata( L, library, kMetatableName ); luaL\_openlib( L, kName, kVTable, 1 ); // leave "library" on top of stack return 1; }

See if that takes care of it.

Rob

You are completely right. Sorry to bother you with such an oversight.
 

One last question regarding this tutorial.

There is this part of code in main.lua:

-- This event is dispatched to the global Runtime object by 'didLoadMain:' in 'MyCoronaDelegate.mm' local function delegateListener( event ) native.showAlert( "Event dispatched from 'didLoadMain:'", "of type: " .. tostring( event.name ), { "OK" } ) end Runtime:addEventListener( "delegate", delegateListener )

Shouldn’t this fire an alert popup at app start? I seems the “delegate” event does not get fired.

Ok I found out from this doc
https://docs.coronalabs.com/native/ios/index.html

that this piece of code is needed in the didLoadMain() method in the AppCoronaDelegate.mm:

- (void)didLoadMain:(id\<CoronaRuntime\>)runtime { &nbsp; &nbsp; lua\_State \*L = runtime.L; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // DISPATCH CUSTOM EVENT &nbsp; &nbsp; // Create 'delegate' event &nbsp; &nbsp; const char kNameKey[] = "name"; &nbsp; &nbsp; const char kValueKey[] = "delegate"; &nbsp; &nbsp; lua\_newtable( L ); &nbsp; &nbsp; lua\_pushstring( L, kValueKey ); &nbsp; &nbsp; // All events are Lua tables &nbsp; &nbsp; lua\_setfield( L, -2, kNameKey );&nbsp; &nbsp; // that have a 'name' property &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Corona::Lua::RuntimeDispatchEvent( L, -1 ); }

Can you zip up your project and share a link to it?

Rob

Sure, here is a dropbox link:
https://www.dropbox.com/s/42wyd8o68r3nkta/Flashlight.zip?dl=0

You missed a step on the plugin code:

You have this:

PluginLibrary::Open( lua\_State \*L ) { // Register \_\_gc callback const char kMetatableName[] = \_\_FILE\_\_; // Globally unique string to prevent collision CoronaLuaInitializeGCMetatable( L, kMetatableName, Finalizer ); // Functions in library const luaL\_Reg kVTable[] = { { "init", init }, { "show", off }, // \<----- needs to be "off" not "show" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ "show", on }, // \<----- needs to be "on" not "show" { NULL, NULL } }; // Set library as upvalue for each library function Self \*library = new Self; CoronaLuaPushUserdata( L, library, kMetatableName ); luaL\_openlib( L, kName, kVTable, 1 ); // leave "library" on top of stack return 1; }

See if that takes care of it.

Rob

You are completely right. Sorry to bother you with such an oversight.
 

One last question regarding this tutorial.

There is this part of code in main.lua:

-- This event is dispatched to the global Runtime object by 'didLoadMain:' in 'MyCoronaDelegate.mm' local function delegateListener( event ) native.showAlert( "Event dispatched from 'didLoadMain:'", "of type: " .. tostring( event.name ), { "OK" } ) end Runtime:addEventListener( "delegate", delegateListener )

Shouldn’t this fire an alert popup at app start? I seems the “delegate” event does not get fired.

Ok I found out from this doc
https://docs.coronalabs.com/native/ios/index.html

that this piece of code is needed in the didLoadMain() method in the AppCoronaDelegate.mm:

- (void)didLoadMain:(id\<CoronaRuntime\>)runtime { &nbsp; &nbsp; lua\_State \*L = runtime.L; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // DISPATCH CUSTOM EVENT &nbsp; &nbsp; // Create 'delegate' event &nbsp; &nbsp; const char kNameKey[] = "name"; &nbsp; &nbsp; const char kValueKey[] = "delegate"; &nbsp; &nbsp; lua\_newtable( L ); &nbsp; &nbsp; lua\_pushstring( L, kValueKey ); &nbsp; &nbsp; // All events are Lua tables &nbsp; &nbsp; lua\_setfield( L, -2, kNameKey );&nbsp; &nbsp; // that have a 'name' property &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Corona::Lua::RuntimeDispatchEvent( L, -1 ); }