Swift CoronaCards func receiveEvent event not work

HI 

lua Corona Can not trigger event to Swift

Xcode 8.2.1 

Swift Code

import UIKit

 

class ViewController: UIViewController , CoronaViewDelegate{

    

    override func viewDidLoad() {

        super.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!”]

        view.sendEvent(event)

        

    }

    

    override func didReceiveMemoryWarning() {

        super.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

end

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

    end

    return true

 

end

 

 

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

[lua]Runtime:addEventListener( “nativeMessage”, handleNativeMessage )[/lua]

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 {
  1. You have to assign view delegate, like this:

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

  2. 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!

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

[lua]Runtime:addEventListener( “nativeMessage”, handleNativeMessage )[/lua]

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 {
  1. You have to assign view delegate, like this:

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

  2. 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!