导入 SDK
远程方式
开始安装
pod init
我们的 SDK 可使用 CocoaPods 导入。
| SDK | 当前版本 |
|---|---|
| ZaigIosOCR | pod 'ZaigIosOCR', '~> 6.0.2' |
15.5
目前,我们的 iOS OCR SDK 不幸不支持在搭载 arm64 架构芯片(M1/M2/M3/M4)的 MacBook 上运行的模拟器中编译,除非使用 Rosetta,它能将 x86_64 架构转换为 arm64。
要开始安装,请在您的项目根目录运行旁边的命令。
在 podfile 中添加 source
source 'https://github.com/ZaigCoding/iOS.git'
下一步是在 podfile 文件中添加 QI Tech 的 source。
在 podfile 中添加 pod
pod 'ZaigIosOCR', '~> <version>'
最后,只需按照旁边的格式添加 pod 名称。
架构变更(v6.0.0+)从 6.0.0 版本开始,SDK 改为仅以静态方式分发。在您的 Podfile 中,必须使用配置 :linkage => :static。
podfile 示例(6.0.0 或更高版本)
source 'https://github.com/ZaigCoding/iOS.git'
source 'https://cdn.cocoapods.org/'
target 'ExampleApp' do
use_frameworks! :linkage => :static
pod 'ZaigIosOCR', '~> 6.0.0'
end
post_install do |installer|
installer.pods_project.targets.each do |target|
if ['DatadogCore', 'DatadogInternal', 'DatadogCrashReporting', 'DatadogLogs'].include?(target.name)
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.0'
config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
end
end
end
end
podfile 示例(早期版本)
source 'https://github.com/ZaigCoding/iOS.git'
source 'https://cdn.cocoapods.org/'
target 'ExampleApp' do
use_frameworks!
pod 'ZaigIosOCR', '~> 4.0.0'
end
post_install do |installer|
installer.pods_project.targets.each do |target|
if ['DatadogCore', 'DatadogInternal', 'DatadogCrashReporting', 'DatadogLogs'].include?(target.name)
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.0'
config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
end
end
end
end
在 iOS 中集成依赖项时,可能需要对某些库使用静态链接,对其他库使用动态链接。此配置对于确保兼容性、避免构建错误和优化项目性能至关重要。
混合依赖链接(如有必要)
混合链接的需求源于某些库有特定要求:一些库需要静态链接以避免内部冲突和符 号重复,而另一些依赖项可能需要动态链接,因为它们是为模块化和项目间共享而设计的。
静态链接与动态链接的区别
- 静态(static_framework):库代码直接嵌入到最终二进制文件中,减少运行时加载时间,并消除执行期间的外部依赖。
- 动态(dynamic_framework):库在运行时作为单独文件加载。这减少了最终二进制文件的大小,并便于独立更新/修改。
在 Podfile 中配置混合链接
...
use_frameworks! :linkage => :dynamic # 将默认链接模式配置为动态
...
static_frameworks = ['framework_1', 'framework_2', ...] # 包含所有需要静态链接的依赖项
pre_install do |installer|
installer.pod_targets.each do |pod|
if static_frameworks.include?(pod.name)
def pod.static_framework?;
true
end
def pod.build_type;
Pod::BuildType.static_framework
end
end
end
end
安装依赖项
pod install
最后,执行 pod install 命令下载并安装依赖项。
必要权限
为使 SDK 能够访问设备资源以采集照片,需要向用户请求权限。
在 info.plist 文件中,添加以下权限:
| 权限 | 原因 |
|---|---|
| Privacy - Camera Usage Description | 访问摄像头以采集文档照片。 |
启动 SDK
import ZaigIosOcr
class ViewController: UIViewController, ZaigIosOcrControllerDelegate {
var zaigOcrConfiguration : ZaigIosOcrConfiguration?
override func viewDidLoad() {
super.viewDidLoad()
self.setupOcr()
}
func setupOcr() -> Void
{
// The environment can be 'Sandbox' ou 'Production'
let environment = ZaigIosOcrEnvironment.Sandbox
// MobileToken is the key sent to you by QI Tech. Each environment requires a different MobileToken.
let mobileToken = "YOUR_MOBILE_TOKEN_SENT_BY_QITECH"
// The documentFlow can be 'CnhFull' , 'CnhFrontAndBack' ou 'RgFrontAndBack'
let documentFlow = ZaigIosOcrDocumentFlow.CnhFrontAndBack
self.ocrConfig = ZaigIosOcrConfiguration(environment: environment,
mobileToken: mobileToken,
sessionId: "UNIQUE_SESSION_ID",
documentFlow: documentFlow,
backgroundColor: "#000000",
fontColor: "#FFFFFF",
fontFamily: .open_sans,
showIntroductionScreens: true,
logLevel: .debug
)
}
// Event where you intend to call QI Tech OCR View Controller - on this example, when the user press 'next' button
@IBAction func pressNext(_ sender: Any) {
let zaigOcrController = ZaigIosOcrController(ocrConfiguration: self.ocrConfig)
zaigOcrViewController.delegate = self
let zaigOcrViewController = zaigOcrController.getViewController()
present(zaigOcrViewController, animated: true, completion: nil)
}
// Do something if QI Tech OCR's SDK succesfully collected document picture
func zaigIosOcrController(_ ocrViewController: ZaigIosOcrController, didFinishWithResults results: ZaigIosOcrControllerResponse) {
}
// Do something if QI Tech OCR's SDK found any error when collecting document picture
func zaigIosOcrController(_ ocrViewController: ZaigIosOcrController, didFailWithError error: ZaigIosOcrControllerError) {
}
// Do something if the user canceled the picture collection on any steps
func zaigIosOcrControllerDidCancel(_ ocrViewController: ZaigIosOcrController) {
}
}
要将 SDK 嵌入您的应用,需要通过 ZaigIosOcrConfiguration 类配置自定义采集应用,然后将自定义配置作为参数实例化 ViewController ZaigIosOcrController。
要启动文档分析流程,只需调用 present 函数来调用 QI Tech 的 ViewController 进行图片采集。
重要的是实现 Delegate,负责在成功、错误或用户在验证的任何步骤中断流程时接收返回值。
旁边提供了完整的实现示例。
在您的应用中启用 Portrait 和 Landscape Right 方向支持,以确保 SDK 正常运行。
Mobile Token
我们使用 Mobile Token 允许您的应用对我们的 API 进行身份验证访问。它可能已通过电子邮件发送给您。如果您尚未收到 Token,请发送电子邮件至 suporte.caas@qitech.com.br。
我们的 API 期望在所有来自 SDK 的服务器请求中接收 Mobile Token,因此必须通过前面提到的方法将其作为配置参数必选包含。
您必须将 "YOUR_MOBILE_TOKEN_SENT_BY_QITECH" 替换为从支持团队收到的 Mobile Token。