Incorporate scanned documents and pictures taken with a user's iPhone, iPad, or iPod touch into your Mac app using Continuity Camera.
With Continuity Camera in macOS 10.14 and later, and iOS 12 and later, you can use your iPhone, iPad, or iPod touch to scan documents or take a picture of something nearby and then access those documents or pictures instantly from your app.
If your app works with images, this feature can be a convenient way to get images into the app. For example, a text-editing app could use this feature to easily incorporate images into a document. It could also be a handy way to bring images into a social media app.
NSText get Continuity Camera support automatically. When the user control-clicks in an app’s text view, a Continuity Camera menu item is automatically displayed. The user can then capture a photo or scan a document on their iPhone or iPad. Once a photo or scan is taken, it automatically appears in the text view. The image is then accessible as an attachment in the
NSText text storage object.
If you’re not using
NSText, you need to add support to your macOS interface to enable Continuity Camera and to merge photos taken from the user’s iOS device.
Enable Support in Your Responder Objects
You must tell AppKit that your app can take advantage of any image data originating from Continuity Camera. You do this in responder objects; for example, in a view controller.
When the Continuity Camera menu item is displayed, AppKit calls the
valid method of your responder objects in the current responder chain to find an object that can handle image data generated by Continuity Camera. Override this method to let AppKit know that your responder object supports image data produced by Continuity Camera. When the user actually captures a photo or scans a document using Continuity Camera, AppKit places the image data on the pasteboard and calls the designated responder object to handle the data.
valid implementation must verify that it can receive pasteboard image data of the specified type, then return the object to receive the image data when it's placed on the pasteboard by AppKit. Your
valid method can designate the same receiver object to handle the image data.
Here’s an example implementation:
Note that your
valid method can return a different object to receive the image data. For example, implement the
valid method in your view controller and perform the checks, but return a view object to actually incorporate the data. You could also return a parent or managing object instead. To illustrate, this code implements
valid in a window controller but returns the active view controller to target the pasted image:
After you implement
valid and specify an object to receive the image data, AppKit enables the Continuity Camera menu item for the designated menus in your app, including contextual menus associated with your view.
Add a Continuity Camera Menu Item
The user initiates Continuity Camera by using a menu item in one of your app’s menu-bar menus or contextual menus. You can add a Continuity Camera menu item to any of your app’s menu-bar menus, or have AppKit automatically add a Continuity Camera menu item to one of your app's contextual menus. A good place to include a Continuity Camera menu item is in menus that contain options for performing editing-related activities (for example, the File and Insert menus).
To add a Continuity Camera menu item to one of your app’s menu-bar menus, locate the storyboard file where your menu bar is defined, and follow these steps in Interface Builder:
Add an item to your app’s menu.
Set the name of the item; for example, “Take Picture.” AppKit provides the actual name later.
In the Identity inspector, set the Identifier property of the menu item to
Item Import From Device Identifier
Here’s how it looks:
You don’t add a Continuity Camera menu item directly to your app’s contextual menus. Instead, you enable the appropriate support in your app’s responder objects as described in the previous section, and AppKit adds the menu item for you.
For example, the following code demonstrates how to display a contextual menu in response to a mouse-down event, and have AppKit insert the menu item. This code overrides the
mouse method and creates a menu. It then invokes the
NSMenu class method
pop, passing the event object related to the mouse-down event and the view owning the contextual menu. AppKit automatically inserts the Continuity Camera menu item in the contextual menu for you.
When the user selects the Continuity Camera menu item, the system automatically launches the Continuity Camera interface on the user's device. After the user captures an image, AppKit places that image on the app's pasteboard.
Incorporate the Image Data from the Pasteboard
You need to incorporate images captured from Continuity Camera into your app. Images captured from the user’s iOS device using Continuity Camera are placed on the pasteboard. AppKit then calls the active responder object’s
read method to read the image data. The
read method supports Continuity Camera image data and other types of data in your app. Use that method to determine if the image is in a format your app supports, and incorporate that image data into your app.
Here's an example implementation of the