Important: Please read the Qt Code of Conduct -

How can I share a URL with Qt on iOS?

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

  • Lifetime Qt Champion


    What exactly do you mean by share ?

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

  • Lifetime Qt Champion

    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.

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

  • Lifetime Qt Champion

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

  • 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];

Log in to reply