Call Qt built shared library from iOS native app



  • I was wondering if it is possible to call a Qt cross platform API (.a file built by Qt shared library) from a native iOS app?

    I see it is easy to call this from a GUI app built with Qt but not sure how to call my .a file built using Qt from native (Objective C) app.

    Thanks.



  • Hi,

    For using some class from your library (*.a file) in iOS native application you need do steps bellow:

    1. Add you library and public header file to iOS native application.
    2. If you use C++ classes in *.a file you must rename all files where you planning to use library from *.m (Objective-C) to *.mm (Objective-C++).
    3. Use it as a extend classes/methods inside iOS application for example:
    //AppDelagete.mm
    #include "MyLib.h"
    //....
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        MyLib* libClass = new MyLib();
        libClass.doSomething();
    
    	return YES;
    }
    

    Hope this helps!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You can mix C++ and Objective C without any problem, it's called Objective-C++.

    You need to create a .mm file where you'll mix your code and link your application to your library.

    Note that I'm used to do it the other way around, create a C++ class to interface some Objective-C API.

    Hope it helps



  • @shav thanks for the reply! Will give it a go!



  • @SGaist Thanks for the reply. Certainly helped a lot......


  • Lifetime Qt Champion

    Just one small thing about @shav's code: libClass is leaked.

    Depending on what you need your library for, you can also have your classes as members of your Objective-C classes.



  • @SGaist @shav thanks. I have a Qt shared library that uses QWebsocket and I want to call it from a native iOS (non Qt) app. Is this something that is possible?



  • @Valp Yes, you can do it. In general you must something like this:

    //MyClass.h
    //includes all you need
    class MyClass : QObject
    {
        Q_OBJECT
        private:
            //private attributes of your class. Here you must have QWebSocketServer or QWebSocketClient attribute for work.
        public:
            void sendMessage(NSString* str);
    };
    

    Implementation:

    //MyClass.mm
    void MyClass::sendMessage(NSString* str)  {
        //Here you can use Qt WebSocket API for send message to web socket server/client.
        //for convert NSString to QString use QString::fromNSString(str);
    }
    

    I don't tested it. but in concept this must works. In you iOS app you can use it like:

    //Your iOS app SomeController.mm
    #include "MyClass.h"   //include header with you class from shared library
    //.....
    - (void)viewDidLoad {
        [super viewDidLoad];
        MyClass* obj = new MyClass();
        NSString* msg = @"Some message";
        obj.sendMessage(msg);
        //Don't forget to remove object after you finished work with it.
    }


  • @shav thanks, yes that works! Although am quite concerned about the size of the resulting app. Will see.

    The shared lib gets called and socket instance is created but the QWebsocket slots like onConnected, onTextReceived etc don't get called!

    If you know a good sample or any more info on how to do this, that would be great.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.