Call Qt built shared library from iOS native app
-
wrote on 10 Aug 2016, 16:31 last edited by Valp 8 Oct 2016, 16:33
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.
-
wrote on 10 Aug 2016, 21:38 last edited by shav 8 Oct 2016, 21:39
Hi,
For using some class from your library (*.a file) in iOS native application you need do steps bellow:
-
Add you library and public header file to iOS native application.
-
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++).
-
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!
-
-
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
-
Hi,
For using some class from your library (*.a file) in iOS native application you need do steps bellow:
-
Add you library and public header file to iOS native application.
-
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++).
-
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!
-
-
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
-
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.
-
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.
-
wrote on 7 Sept 2016, 17:49 last edited by
@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. }
-
@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. }
wrote on 8 Sept 2016, 14:16 last edited by@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.