Clarifai Apple SDK
你好!欢迎来到 Clarifai SDK 针对苹果平台的公开仓库。我们目前支持 iOS,但我们也正在计划扩展。
Clarifai 的愿景是回答每个问题。这个 SDK 可以帮助您将强大的 AI 带到移动应用中。查看我们的开发者网站 https://developer.clarifai.com 或通过邮箱 [nuclearsci@gmail.com] 联系我们以获取更多信息。
入门指南
Git LFS
在继续安装之前,请确保您已经在其系统中安装了 Git-LFS。该框架中包含的二进制文件由 GitHub 使用 git-lfs
管理。
如果您尚未安装,您可以在以下地址找到详细信息: https://git-lfs.github.com。如果您未安装 GIT-LFS,SDK 将无法工作。
获取开发者账号
在以下网址注册免费的开发者账号: https://developer.clarifai.com/signup/
Clarifai-Apple-SDK 可以通过 CocoaPods 获取,或者可以手动安装。根据您的喜好,请按照以下说明操作。
CocoaPods
使用 CocoaPods 集成 SDK,请在您的 Podfile 中指定它
target '<Your Target>' do
platform :ios, '9.0'
use_frameworks!
pod 'Clarifai-Apple-SDK', '~> 3.0.0'
end
安装方式
# Use this option the first time you want to install the SDK in your project, but also every time you edit your Podfile to add, update or remove a pod.
pod install
or
# Use this option to find an updated version of the SDK. It will update the pod to the latest version possible, as long as it complies with restrictions in your Podfile
pod update
如果在您的项目中可用的更新没有反映出来,您可以尝试
pod install --repo-update
Clarifai SDK 至少支持 iOS 9.0 版本。
手动安装
使用手动安装方式保持与 SDK 最新版本的同步,最佳做法是克隆我们的 GitHub 仓库。
git clone https://github.com/Clarifai/clarifai-apple-sdk.git
-
在您项目的根目录下,$(PROJECT_DIR) [与 .xcodeproj 文件相同的目录],创建一个名为 Clarifai 的目录,并将 Clarifai-Apple-SDK.framework 移动到该目录
cd <project root directory> mkdir Clarifai cp -r <directory the SDK was cloned to>/Clarifai_Apple_SDK.framework <project root directory>/Clarifai
-
同时,将
setup_framework.sh
脚本复制到同一个 Clarifai 目录。cp -r <directory the SDK was cloned to>/scripts/setup_framework.sh <project root directory>/Clarifai
-
将 Clarifai-Apple-SDK.framework 添加到项目的 Embedded Binaries 中。
- 从 Xcode 项目/工作区转到项目配置,General 选项卡,在 Embedded Binaries 部分的 + 按钮下点击。导航到克隆仓库的目录,并选择 Clarifai-Apple-SDK.framework
-
在 Linked Frameworks and Libraries 中包含以下必需的依赖项
Accelerate.framework
CoreGraphics.framework
Foundation.framework
libc++
libsqlite3
libz
UIKit.framework
-
创建一个新的 Run Script Build Phase (Xcode > Editor > Add Build Phase > Add Run Script Build Phase)。在执行行中输入
"$PROJECT_DIR/Clarifai/setup_framework.sh"
-
确保新的 Run Script 的位置在 Compile Sources 之后和 Link Binary With Libraries 之前。如果需要,可以使用拖放功能将其移动到正确的位置。
您应该能够构建您的项目,并在项目中开始使用 SDK。
启动 SDK
Clarifai SDK 通过调用 startWithApiKey
方法进行初始化。我们建议在您的应用程序完成启动后启动 SDK,但这不是必需的。并且无需担心应用程序启动的占用问题。我们通过后台线程来承担工作,应该几乎没有影响。
-
Swift
import Clarifai_Apple_SDK func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { Clarifai.sharedInstance().start(apiKey:"<Your API Key>") return true }
-
Objective-C
@import Clarifai_Apple_SDK; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[Clarifai sharedInstance] startWithApiKey:@"<Your API Key>"]; return YES; }
一般模型可用性通知
Clarifai 的 一般模型 可根据需要在 SDK 中成为可用。首次使用 SDK并输入有效的 API 密钥后,该模型将被使用并缓存以便将来使用。
您可以通过注册以下通知来接收 SDK 可用性的进度通知
Swift | Objective-C | 描述 |
---|---|---|
NSNotification.Name.CAIWillFetchModel |
CAIWillFetchModelNotification |
在 SDK 开始提取模型之前广播 |
NSNotification.Name.CAIDidFetchModel |
CAIDidFetchModelNotification |
在提取了模型后立即广播 |
NSNotification.Name.CAIModelDidBecomeAvailable |
CAIModelDidBecomeAvailableNotification |
当模型可用时广播 |
前两个通知通常只会发生一次,当第一次使用 SDK 时。模型一旦可用,将保持本地缓存。另一方面,最后一个通知每次启动 SDK 时都会广播。
上述每个通知都包含一个有效载荷(userInfo),其中包含模型的 id
。通过使用 CAIModelUniqueIdentifierKey
键检索它。
-
Swift
func handleModelDidBecomeAvailable(notification: NSNotification) { if let userInfo = notification.userInfo { let modelId = userInfo[CAIModelDidBecomeAvailable] as? String } }
-
Objective-C
- (void)handleModelDidBecomeAvailable:(NSNotification *)notification { NSDictionary *userInfo = [notification UserInfo]; NSString *modelId = userInfo[CAIModelUniqueIdentifierKey]; }
学习和更多操作
请访问我们的 文档站点了解如何将人工智能带入您的应用程序。
支持
有问题?请联系我们,发送电子邮件至 [email protected]。
许可协议
Clarifai-Apple-SDK 以商业许可协议提供。有关更多信息,请参阅LICENSE文件。