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. How can I share a URL with Qt on iOS?
Forum Updated to NodeBB v4.3 + New Features

How can I share a URL with Qt on iOS?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
8 Posts 3 Posters 919 Views 3 Watching
  • 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.
  • A Offline
    A Offline
    Andreas E.
    wrote on last edited by
    #1

    Hi,

    what is the correct way to share texts, images or URLs to social media apps with Qt on iOS? I have the following code:

    void SocialIos::shareLink(const QString &title, const QString &url)
    {
        NSURL *ns_url =[NSURL URLWithString:url.toNSString()];
        UIActivityViewController *controller = [[UIActivityViewController alloc]initWithActivityItems:@[title.toNSString(), ns_url] applicationActivities:nil];
        UIViewController *view_controller = [UIApplication sharedApplication].keyWindow.rootViewController;
        while (view_controller.presentedViewController) {
            view_controller = view_controller.presentedViewController;
        }
        [[view_controller popoverPresentationController] presentViewController:controller animated:YES completion:nil];
    }
    

    I don't really know what UIViewController to use or how to obtain it in Qt, I used this code that I found somewhere in an example. But if I run the code, nothing happens. There also isn't any output on the debug console.

    What is the correct/best way to do this in Qt?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What exactly do you mean by share ?

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Andreas E.
        wrote on last edited by
        #3

        I mean post a link to WhatsApp or similar apps, like the share buttons in web browsers do.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Check the applications you want to interact and see if they provide custom URLs to work with. Then you can use QDesktopService::openUrl with these custom URLs.

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

          1 Reply Last reply
          1
          • A Offline
            A Offline
            Andreas E.
            wrote on last edited by
            #5

            I don't want to predefine the apps the user can interact with. The OS is supposed to let the user choose from any installed app that can handle shared contents. UIActivityViewController seems to be the standard API to do this, but I can't get it to work within a Qt app.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Take a look at the QtMacExtras module to see how you can mix Objective-C and C++.

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

              1 Reply Last reply
              0
              • D Offline
                D Offline
                daljit97
                wrote on last edited by
                #7

                Have you tried looking here https://blog.qt.io/blog/2017/12/01/sharing-files-android-ios-qt-app/ ?

                1 Reply Last reply
                3
                • A Offline
                  A Offline
                  Andreas E.
                  wrote on last edited by
                  #8

                  Thanks a lot, that's exactly what I was looking for.

                  Correct code is:

                  void IosShareUtils::share(const QString &text, const QUrl &url) {
                      NSMutableArray *sharingItems = [NSMutableArray new];
                      if (!text.isEmpty()) {
                          [sharingItems addObject:text.toNSString()];
                      }
                      if (url.isValid()) {
                          [sharingItems addObject:url.toNSURL()];
                      }
                      // get the main window rootViewController
                      UIViewController *qtUIViewController = [[UIApplication sharedApplication].keyWindow rootViewController];
                      UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
                      if ( [activityController respondsToSelector:@selector(popoverPresentationController)] ) { // iOS8
                          activityController.popoverPresentationController.sourceView = qtUIViewController.view;
                      }
                      [qtUIViewController presentViewController:activityController animated:YES completion:nil];
                  }
                  
                  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