Open with (Share) Guide
Open images and PDF files from other apps
In order to enable your app to open any kind of files that are identified by the OS as images or PDF files, follow these steps:
1. Register image and PDF file types
Add the following to your Info.plist
:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>PDF</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Images</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.jpeg</string>
<string>public.png</string>
<string>public.tiff</string>
<string>com.compuserve.gif</string>
</array>
</dict>
</array>
You can also add these by going to your target’s Info tab and entering the values into the Document Types section.
In order to initiate Open with
from your app’s Documents folder, add UISupportsDocumentBrowser to your Info.plist
.
Documentation
Document types from Apple documentation.
2. Enable the “Open with (Share)” feature inside the Gini Capture SDK
Please note that the “Open with (Share)” feature is disabled by default. You have to enable this feature as explained below.
To enable Gini Capture SDK to handle files imported from other apps and to show the Open with tutorial in the Help menu, it is necessary to indicate it in the GiniConfiguration
.
let giniConfiguration = GiniConfiguration.shared
...
...
giniConfiguration.openWithEnabled = true
3. Handle incoming images and PDF files
When your app is requested to handle an image or a PDF file your AppDelegate
’s application(_:open:options:)
(Swift) method is called. You can then use the supplied URL to create a document as shown later.
In some cases, especially when the LSSupportsOpeningDocumentsInPlace
flag is enabled in your Info.plist
file, reading data directly from the URL might fail. For that reason, GiniCapture
uses the asynchronous UIDocument
API internally which handles any of the potential security requirements.
In order to determine that the file opened is valid (correct size, correct type, and number of pages below the threshold on PDF files), it is necessary to validate it before using it.
Gini Capture
func application(_ app: UIApplication,
open url: URL,
options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
// 1. Build the document
let documentBuilder = GiniCaptureDocumentBuilder(documentSource: .appName(name: sourceApplication))
documentBuilder.importMethod = .openWith
documentBuilder.build(with: url) { [weak self] (document) in
guard let self = self else { return }
// 2. Validate the document
if let document = document {
do {
try GiniCapture.validate(document,
withConfig: giniConfiguration.captureConfiguration())
// Load the GiniCapture with the validated document
} catch {
// Show an error pointing out that the document is invalid
}
}
}
return true
}
Documentation
AppDelegate resource handling from Apple Documentation
Supported file formats from the Gini API
Open images from Photos app
In order to enable your app to open images from Photos, implement a shared extension. Share extensions, in particular, let you share content with your application.
1. Add a share extension to your project
Go to File -> New -> Target
and select Share Extension
. Make sure you link it to the main app. When the system asks if you want to activate the Share scheme
, select Activate
.
2. Set the extension activation rule
Change the NSExtensionActivationRule
in the Info.plist
in your extension target. Check the example here.
3. Handle the URL
Find the example implementation here.
4. Pass the data from the extension to the main app
Here we connect the share extension directly to the main app using AppGroups
and UserDefaults
. Add AppGroups to the capabilities of both the extension and main app using the same app group. Check the example here.
5. Open the main app and retrieve the shared data
Register your app extension scheme in the URL types for the main app.
Open the main app from the extension.
Handle the incoming URL from the app extension in
AppDelegate
and retrieve data from the sharedUserDefaults
. The system delivers the URL to your app by calling your app delegate’sapplication(_:open:options:)
method. You can check our example implementation here.