Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

Swift CoronaCards func receiveEvent event not work
Started by arthur15 Jun 27 2017 11:17 PM

1 reply to this topic
This topic has been archived. This means that you cannot reply to this topic.
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 1 posts
  • Corona SDK





lua Corona Can not trigger event to Swift

Xcode 8.2.1 


Swift Code


import UIKit


class ViewController: UIViewController , CoronaViewDelegate{


    override func viewDidLoad() {


        // Do any additional setup after loading the view, typically from a nib.


        let coronaController = CoronaViewController()  // Step 1

        self.addChildViewController(coronaController)  // Step 2


        let view:CoronaView = coronaController.view as! CoronaView

        view.frame = self.view.frame   // Step 3

        self.view.addSubview(view)     // Step 4

        view.run();                    // Step 5


        let event = ["name":"nativeMessage", "phase":"loaded", "message":"Hello from Swift!"]





    override func didReceiveMemoryWarning() {


        // Dispose of any resources that can be recreated.



    private func coronaView(view: CoronaView, receiveEvent event: NSDictionary) -> AnyObject? {

        print("receiveEvent \(event)")

        return("Nice to meet you!" as AnyObject?)






lua Code:


local function handleTap( event )
     print( 'hello swift' )
    local data = { name="coronaView", message={} }
    Runtime:dispatchEvent( data )
    return true
local startText = display.newText( "START", display.contentCenterX, 200, "HelveticaNeue", 24 )
startText:addEventListener( "tap", handleTap )
-- Set up a listener to handle return event from Swift
local message = display.newText( "", display.contentCenterX, 25, "HelveticaNeue", 16 )
local function handleNativeMessage( event )
    if ( event.phase == "loaded" ) then
        print( 'corona:' .. event.name )  -- "nativeMessage"
        print( 'corona:' .. event.message )  -- "Hello from Swift!"
        message.text = event.message
    return true





[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Contributor

  • 981 posts
  • Corona Staff

To make Lua code handle events from Swift, you have to register Event Listener in Lua code:

Runtime:addEventListener( "nativeMessage", handleNativeMessage )


To go from Lua to Swift, there are some parts missing in Swift code


1. ViewController should be implementing CoronaViewDelegate protocol. For this, change your class declaration to:

class ViewController: CoronaViewController, CoronaViewDelegate {

2. You have to assign view delegate, like this:

self.view.addSubview(view)     // Step 4
view.coronaViewDelegate = self;
view.run();                    // Step 5

3. You have function set to be private, which makes it invisible for Corona to call. Try to have definition corresponding to one in CoronaViewDelegate:

func coronaView(_ view: CoronaView, receiveEvent event: [AnyHashable : Any]!) -> Any? {
    print("receive message: \(event["message"]!)")
    return "Nice to meet you!"

This code sends messages back and forward without any troubles.

Have fun!