How to use Share API in macOS and Qt Quick
Hi everyone!
In this article I’ve tried to explain how to use Mac OS X Share API in your Qt Quick app. Share API was implemented in Mac OS 10.10 (Yosemite).
As you know all frameworks in OS X which you could use in your application created with Objective-C/C++. That’s why you can’t use framework in your C++ classes. You need to rename your .cpp file to .mm and then you can call Objective-C code. But sometimes you need to call methods with arguments with Objective-C. As you know in .h file you can’t call Objective-C code. You must create Objective-C class which will call all needed methods to work with target framework.
First you need to create class with all methods in Objective-C/C++ to use Share API. To do this you need follow a few steps:
- Create a new class and rename .cpp file to .mm.
- Add .h file to .pro of your project in section “OBJECTIVE_HEADERS” and .mm to “OBJECTIVE_SOURCES”.
- Change .h file content to this code:
- Change .mm file content to this code:
p. More information you can find in official Apple documentation [developer.apple.com]. And you need to see information about class NSSharingServicePickerDelegate [developer.apple.com].
Second class which you need to create is a class to call from C++. This class will call Objective-C/C++. To do this you need follow a few steps:
- Create a new C++ class.
- Rename .cpp file to .mm and added it to “OBJECTIVE_SOURCES”.
- Change .h file content to code:
- Change .mm file content to code:
- Register new type to use in QML.
How to use in QML.
Import the new type to your QML file where you need to use share logic and add this code:
Summary
This Share item can share only text and link. If you need to share Image you can implement special logic to convert QImage to NSImage. And added a new parameter to method: shareCurrentContent().