Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Call Qt built shared library from iOS native app

Call Qt built shared library from iOS native app

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
9 Posts 3 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Valp
    wrote on 10 Aug 2016, 16:31 last edited by Valp 8 Oct 2016, 16:33
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      shav
      wrote on 10 Aug 2016, 21:38 last edited by shav 8 Oct 2016, 21:39
      #2

      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!

      Mac OS and iOS Developer

      V 1 Reply Last reply 11 Aug 2016, 17:14
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 10 Aug 2016, 22:10 last edited by
        #3

        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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        V 1 Reply Last reply 11 Aug 2016, 17:19
        1
        • S shav
          10 Aug 2016, 21:38

          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!

          V Offline
          V Offline
          Valp
          wrote on 11 Aug 2016, 17:14 last edited by
          #4

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

          1 Reply Last reply
          0
          • S SGaist
            10 Aug 2016, 22:10

            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

            V Offline
            V Offline
            Valp
            wrote on 11 Aug 2016, 17:19 last edited by
            #5

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 11 Aug 2016, 21:00 last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              V 1 Reply Last reply 7 Sept 2016, 15:40
              0
              • S SGaist
                11 Aug 2016, 21:00

                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.

                V Offline
                V Offline
                Valp
                wrote on 7 Sept 2016, 15:40 last edited by
                #7

                @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?

                S 1 Reply Last reply 7 Sept 2016, 17:49
                0
                • V Valp
                  7 Sept 2016, 15:40

                  @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?

                  S Offline
                  S Offline
                  shav
                  wrote on 7 Sept 2016, 17:49 last edited by
                  #8

                  @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.
                  }
                  

                  Mac OS and iOS Developer

                  V 1 Reply Last reply 8 Sept 2016, 14:16
                  1
                  • S shav
                    7 Sept 2016, 17:49

                    @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.
                    }
                    
                    V Offline
                    V Offline
                    Valp
                    wrote on 8 Sept 2016, 14:16 last edited by
                    #9

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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved