Swiftmessages: Make accessible from Objective-C code

Created on 1 Dec 2016  路  4Comments  路  Source: SwiftKickMobile/SwiftMessages

I haven't managed to get it used by ObjC code...

Most helpful comment

Hi, yes of course:

I made a class "Messenger" in Swift:

import Foundation
import UIKit
import SwiftMessages

let status2 = MessageView.viewFromNib(layout: .StatusLine)

open class Messenger : NSObject {
    open class func showStatusBarMessage(_ msg: String, color: UIColor, sender: UIViewController) {
        status2.backgroundView.backgroundColor = color
        status2.bodyLabel?.textColor = UIColor.white
        status2.configureContent(body: msg)
        var status2Config = SwiftMessages.defaultConfig
        status2Config.preferredStatusBarStyle = UIStatusBarStyle.lightContent
        status2Config.duration = .forever
        status2Config.presentationContext = .automatic

        SwiftMessages.show(config: status2Config, view: status2)
    }
}

and call it from my Objective-C class like (of course I have to #import my "Module"-Swift.h in it):

[Messenger showStatusBarMessage:@"Blabla" color:[UIColor colorWithRed:0.25 green: 0.43 blue: 0.10 alpha:1.0] sender:self];

Hope this helps...!

All 4 comments

SwiftMessages uses data structures that can't be seen by Objective-C. So it isn't possible. What you can do is write your own helper function in Swift that can be seen by Objective-C and call that:

swift func showErrorMessage(title: String, subtitle: String) { // show message here }

@mattem86 Can you explain how you made this work?

Hi, yes of course:

I made a class "Messenger" in Swift:

import Foundation
import UIKit
import SwiftMessages

let status2 = MessageView.viewFromNib(layout: .StatusLine)

open class Messenger : NSObject {
    open class func showStatusBarMessage(_ msg: String, color: UIColor, sender: UIViewController) {
        status2.backgroundView.backgroundColor = color
        status2.bodyLabel?.textColor = UIColor.white
        status2.configureContent(body: msg)
        var status2Config = SwiftMessages.defaultConfig
        status2Config.preferredStatusBarStyle = UIStatusBarStyle.lightContent
        status2Config.duration = .forever
        status2Config.presentationContext = .automatic

        SwiftMessages.show(config: status2Config, view: status2)
    }
}

and call it from my Objective-C class like (of course I have to #import my "Module"-Swift.h in it):

[Messenger showStatusBarMessage:@"Blabla" color:[UIColor colorWithRed:0.25 green: 0.43 blue: 0.10 alpha:1.0] sender:self];

Hope this helps...!

You have to add "@objc" start of method

@objc
open class func showStatusBarMessage

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrey-zelenin picture andrey-zelenin  路  4Comments

Khalphion picture Khalphion  路  4Comments

airspeed picture airspeed  路  7Comments

lixiang1994 picture lixiang1994  路  6Comments

mergesort picture mergesort  路  5Comments