Share Extensions
Receiving Files from Other Apps
You can receive files from other apps via the iOS and Android Share Sheet, and process them using the Lens SDK.
- iOS
- Android
1. (Prerequisite) Create iOS Share Extension
To create a Share Extension in your iOS app:
- Add Share Extension Target: In Xcode, go to File → New → Target → iOS → Share Extension
- Add App Groups Capability: Enable App Groups capability in both your main app and Share Extension to share data between them
- Configure Share Extension: Replace the default ShareViewController with your custom implementation
- Configure App IDs and Provisioning Profiles: Make sure you create App ID for the Share Extension as well as matching provisioning profiles
2. Configure Share Extension Settings
Enable share extension mode in your VeryfiLensSettings:
let settings = VeryfiLensReceiptsSettings()
settings.shareExtensionModeIsOn = true
Note: shareExtensionModeIsOn
is a mandatory setting in iOS to handle RAM usage restrictions for Share Extensions
3. Configure VeryfiLens in Share Extension
Configure VeryfiLens with credentials and settings in your ShareViewController:
let credentials = VeryfiLensCredentials(
clientId: "your_client_id",
username: "your_username",
apiKey: "your_api_key",
url: "your_api_url"
)
let settings = VeryfiLensReceiptsSettings()
settings.shareExtensionModeIsOn = true
VeryfiLens.shared().delegate = self
VeryfiLens.shared().configure(with: credentials, settings: settings) { success in
if success {
// Handle attachment after successful configuration
self.handleAttachment()
}
}
4. Handle Shared Files
Process the shared files using the uploadImages
method:
func handleAttachment() {
guard let extensionItem = extensionContext?.inputItems.first as? NSExtensionItem,
let attachment = extensionItem.attachments?.first else { return }
if attachment.hasItemConformingToTypeIdentifier(UTType.image.identifier) {
attachment.loadItem(forTypeIdentifier: UTType.image.identifier, options: [:]) { [weak self] (data, error) in
if let url = data as? URL {
// uploadImages supports image paths
VeryfiLens.shared().uploadImages(paths: [url.path])
}
}
} else if attachment.hasItemConformingToTypeIdentifier(UTType.pdf.identifier) {
attachment.loadItem(forTypeIdentifier: UTType.pdf.identifier, options: [:]) { [weak self] (data, error) in
if let url = data as? URL {
// uploadImages supports PDF paths too
VeryfiLens.shared().uploadImages(paths: [url.path])
}
}
}
}
5. Implement VeryfiLensDelegate
Handle the upload results through the delegate methods:
extension ShareViewController: VeryfiLensDelegate {
func veryfiLensSuccess(_ json: [String: Any]) {
// Handle successful upload
}
func veryfiLensError(_ json: [String: Any]) {
// Handle upload error
}
func veryfiLensUpdate(_ json: [String: Any]) {
// Handle progress updates
}
}
Important: The uploadImages
method must be called after VeryfiLens.shared().configure
has completed successfully.
1. Enable File Sharing in Android
To allow your Android app to receive shared files, declare an intent-filter
inside the <activity>
tag of your AndroidManifest.xml
for the activity that will handle incoming content. Lens supports both image and PDF files:
<activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="application/pdf" />
</intent-filter>
</activity>
2. Handle the Incoming Intent
In your designated activity, handle the intent in the onCreate()
or onNewIntent()
method. Use ACTION_SEND
for a single file or ACTION_SEND_MULTIPLE
for multiple files:
class MyReceiveActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my_receive)
val credentials = VeryfiLensCredentials() // Configure your credentials
val settings = VeryfiLensSettings() // Configure your settings
VeryfiLens.setDelegate(...) // Implement your delegate
VeryfiLens.configure(application, credentials, settings) {}
when (intent?.action) {
Intent.ACTION_SEND -> {
if (intent?.type?.startsWith("image/") == true || intent?.type?.startsWith("application/pdf") == true) {
IntentCompat.getParcelableExtra(intent, Intent.EXTRA_STREAM, Uri::class.java)?.let {uri ->
VeryfiLens.uploadImages(arrayListOf(uri), true)
}
}
}
Intent.ACTION_SEND_MULTIPLE -> {
if (intent?.type?.startsWith("image/") == true || intent?.type?.startsWith("application/pdf") == true) {
(intent.parcelableArrayList<Uri>(Intent.EXTRA_STREAM))?.let { uris ->
VeryfiLens.uploadImages(uris, true)
}
}
}
}
}
}
To configure credentials, settings, and the delegate, refer to the Initialization and Communication sections of the documentation.