CIOAPIClient 1.0

CIOAPIClient 1.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2015年10月

Alex Pretzlav 维护。



通过电子邮件构建出色的产品!我们省去了同步电子邮件数据与您的应用中的麻烦,这样您可以专注于使您的产品大放异彩。

CIOAPIClient 是一个易于使用的 iOS 和 OS X 库,用于与 Context.IO 2.0 API 通信。它基于 NSURLSession 构建,并提供便于异步基于块的交互方法。

入门

  • Context.IO 上注册开发者账户
  • 提交请求 3 位 OAuth 访问令牌。此库仅支持 3 位令牌,以确保您的应用最终用户只能访问他们自己的账户
  • 下载 CIOAPIClient 并查看包含的 iOS 示例应用。它也可以作为 CocoaPod 使用,以便更轻松地将其添加到项目中
  • 查看完整的 Context.IO API 文档 以更好地熟悉此 API

构建示例应用程序

在克隆 git 仓库后,请确保安装示例应用使用的 Cocoapods

  • cd <repository path>/Example
  • pod install
  • 打开 "Context.IO iOS Example App.xcworkspace"

要运行示例应用程序,您需要在 CIOAppDelegate.m 中插入您的 Context.IO 消费者密钥和密钥

在沙盒中探索 API

CIOPlayground 目录中有一个预先配置的 Xcode 沙盒(目前针对 Xcode 6.4 + Swift 1.2)。带有库依赖的沙盒在 Xcode 6.4 中有一些小问题,请按照以下步骤使其工作

  • cd CIOPlayground
  • pod install
  • 打开 CIOPlayground.xcworkspace
  • 在 Xcode 方案选择下拉菜单中选择 CIOAPIClient 方案(它应该有一个动态框架黄色工具箱图标)
  • 构建方案(⌘B)
  • 在项目浏览器左侧侧边栏中选择项目中的 CIOPlayground.playground
  • 将您的消费者密钥和消费者密钥添加到行
let s: CIOAPISession = CIOAPISession(consumerKey: "", consumerSecret: "")
  • 在此点,沙盒将执行并在屏幕左下角显示身份验证 WebView
  • 使用 WebView 中的 Context.IO 身份验证流程授权电子邮件账户
    • 身份验证后首次执行代码时可能会失败。编辑沙盒再次尝试。
  • 将您希望尝试执行的任何代码添加到沙盒中 authenticator.withAuthentication() { session in 块中

示例用法

使用 CIOAPISession 来构建和执行对 Context.IO API 的签名 NSURLRequests

开始一个 API 会话

使用您的 API 密钥、消费者密钥和消费者密钥初始化 CIOAPISession

CIOAPISession *session = [[CIOAPISession alloc] initWithConsumerKey:@"your-consumer-key"
                                                     consumerSecret:@"your-consumer-secret"];

身份验证

CIOAPISession 使用 Connect Tokens 来授权个别用户的电子邮件账户。请参阅示例应用程序以了解身份验证过程。您可以在自己的项目中重用或子类化 CIOAuthViewController,该视图控制器负责身份验证的细节,并且对于大多数目的应该直接可用。

检索消息

[[session getMessagesWithParams:nil]
 executeWithSuccess:^(NSArray *responseArray) {
     self.messagesArray = responseArray;
 } failure:^(NSError *error) {
     NSLog(@"error getting messages: %@", error);
 }];

将消息添加到现有的文件夹/标签

[[session updateFoldersForMessageWithID:message[@"message_id"]
                                 params:@{@"add": @"Test Label"}]
 executeWithSuccess:^(NSDictionary *response) {
     NSLog(@"Response: %@", response);
 } failure:^(NSError *error) {
     NSLog(@"error moving message: %@", error);
 }];

列出账户的文件夹/标签

// 0 is an alias for the first source of an account
[[session getFoldersForSourceWithLabel:@"0" params:nil]
 executeWithSuccess:^void(NSArray *folders) {
     NSLog(@"Folders: %@", folders);
 } failure:^void(NSError *error) {
     NSLog(@"Error getting folders: %@", error);
 }];

下载消息附件

NSDictionary *file = [message[@"files"] firstObject];
CIODownloadRequest *downloadRequest = [session downloadContentsOfFileWithID:file[@"file_id"]];
// Save file with attachment's filename in NSDocumentDirectory
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
                                                              inDomains:NSUserDomainMask] lastObject];
NSURL *fileURL = [documentsURL URLByAppendingPathComponent:file[@"file_name"]];
[session downloadRequest:downloadRequest
               toFileURL:fileURL
                 success:^{
                     NSLog(@"File downloaded: %@", [fileURL path]);
                 }
                 failure:^(NSError *error) {
                     NSLog(@"Download error: %@", error);
                 }
                progress:^(int64_t bytesRead, int64_t totalBytesRead, int64_t totalBytesExpected){
                    NSLog(@"Download progress: %0.2f%%",
                          ((double)totalBytesExpected / (double)totalBytesRead) * 100);
                }];

要求

CIOAPIClient 需要iOS 7.0或更高版本或Mac OS 10.9或更高版本。

致谢

感谢 Kevin Lord 编写了此库的原始版本,Sam Soffessskeychain 以及 TweetDeck 的 TDOAuth(用于 CIOAPIClient 中的 OAuth 签名生成)做出贡献。

许可协议

CIOAPIClient 在 MIT 许可协议下发布。有关详细信息,请参阅 LICENSE 文件。