VIMNetworking 6.0.4

VIMNetworking 6.0.4

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最新发布2016年4月

Alfie HanssenRob HuebnerGavin King 维护。



  • 作者:
  • Alfie Hanssen, Rob Huebner, Gavin King, Kashif Muhammad, Andrew Whitcomb, Stephen Fredieu 和 Rahul Kumar

VIMNetworking 是一个 Objective-C 库,它允许与 Vimeo API 进行交互。它处理身份验证、请求提交和请求取消。高级功能包括缓存和强大的模型对象解析。

如果您想上传视频,请查看 VimeoUpload.

设置

Git 子模块

VIMNetworkingVIMObjectMapperAFNetworking(发布 2.5.4)添加为您的 git 仓库的子模块。

git submodule add [email protected]:vimeo/VIMNetworking.git
git submodule add [email protected]:vimeo/VIMObjectMapper.git
git submodule add [email protected]:AFNetworking/AFNetworking.git

将每个子模块的类添加到您的项目/目标中。

如果您还在项目/目标中包含 VIMUpload,请注意,VIMUploadVIMNetworking 都包含 Certificate/digicert-sha2.cer 文件(此文件用于证书固定)。您必须从目标中删除一个 digicert-sha2.cer 文件,以避免 "多个输出文件为..." 警告。

初始化

在应用启动时,使用您的客户端密钥、密钥和作用域字符串配置 VIMSession。一旦初始化完成,如果需要,进行身份验证。

#import "VIMNetworking.h"

. . .

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    VIMSessionConfiguration *config = [[VIMSessionConfiguration alloc] init];
    config.clientKey = @"your_client_key";
    config.clientSecret = @"your_client_secret";
    config.scope = @"your_scope"; // E.g. "private public upload etc"
    config.keychainService = @"your_service"; 
    config.keychainAccessGroup = @"your_access_group"; // Optional

    [VIMSession sharedSession setupWithConfiguration:config];    

    if ([[VIMSession sharedSession].account isAuthenticated] == NO)
    {
        NSLog(@"Authenticate...");
    }
    else
    {
        NSLog(@"Already authenticated!");
    }

    . . .
}

请注意,您必须为 keychainService 指定一个值,并且可以可选地为 keychainAccessGroup 提供一个值。后者值的角色在 此处 的密钥访问组部分进行了详细说明。

身份验证

所有对 Vimeo API 的调用都必须 进行身份验证。这意味着在向 API 发出请求之前,您必须进行身份验证并获取访问令牌。提供了两种身份验证方法

  1. 客户端凭证授权:此机制允许您的应用访问 Vimeo 上的公开内容。

  2. OAuth 授权代码授权:此机制允许 Vimeo 用户授予您的应用权限,使其代表用户访问其个人私密内容。

客户端凭证授权

[[VIMSession sharedSession] authenticateWithClientCredentialsGrant:^(NSError *error) {

        if (error == nil)
        {
            NSLog(@"Success!");
        }
        else
        {
            NSLog(@"Failure: %@", error);
        }

}];

OAuth 授权代码授权

  1. 设置重定向 URL 方案

导航到您的应用目标设置 > 信息 > URL 类型。添加一个新的 URL 类型,并在 URL 方案中输入 vimeo{CLIENT_KEY}(例如,如果您的 CLIENT_KEY 是 1234,则输入 vimeo1234)。这可以使 Vimeo 在授权后返回到您的应用中。

您还需要将此重定向 URL 添加到 Vimeo API 网站上的您的应用。在“应用回调 URL”下,添加 vimeo{CLIENT_KEY}://auth(对于上面的示例,则为 vimeo1234://auth)。

  1. 在 Mobile Safari 中打开授权 URL
NSURL *URL = [[VIMSession sharedSession].authenticator codeGrantAuthorizationURL];
[[UIApplication sharedApplication] openURL:URL];
  1. Mobile Safari 将打开,并将显示一个网页,要求用户根据您在上述 VIMSessionConfiguration 中指定的 scope 授予访问权限。

  2. 然后用户将被重定向回您的应用程序。在您的 AppDelegate 的 URL 处理方法中,将 URL 返回到 VIMAPIClient 以完成授权授予

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    [[VIMSession sharedSession] authenticateWithCodeGrantResponseURL:url completionBlock:^(NSError *error) {

        if (error == nil)
        {
            NSLog(@"Success!");
        }
        else
        {
            NSLog(@"Failure: %@", error);
        }

    }];

    return YES;
}

请求

配置和认证 VIMNetworking 后,您即可开始向 Vimeo API 发送请求。

JSON 请求

[[VIMSession sharedSession].client requestURI:@"/videos/77091919" completionBlock:^(VIMServerResponse *response, NSError *error) {

    id JSONObject = response.result;
    NSLog(@"JSONObject: %@", JSONObject);

}];

模型对象请求

VIMRequestDescriptor *descriptor = [[VIMRequestDescriptor alloc] init];
descriptor.urlPath = @"/videos/77091919";
descriptor.modelClass = [VIMVideo class];

[[VIMSession sharedSession].client requestDescriptor:descriptor completionBlock:^(VIMServerResponse *response, NSError *error) {

    VIMVideo *video = (VIMVideo *)response.result;
    NSLog(@"VIMVideo object: %@", video);

}];

集合请求

VIMRequestDescriptor *descriptor = [[VIMRequestDescriptor alloc] init];
descriptor.urlPath = @"/me/videos";
descriptor.modelClass = [VIMVideo class];
descriptor.modelKeyPath = @"data";

[[VIMSession sharedSession].client requestDescriptor:descriptor completionBlock:^(VIMServerResponse *response, NSError *error) {

    NSArray *videos = (NSArray *)response.result; 
    NSLog(@"Array of VIMVideo objects: %@", videos);

}];

缓存行为

VIMRequestDescriptor *descriptor = [[VIMRequestDescriptor alloc] init];
descriptor.urlPath = @"/videos/77091919";
descriptor.modelClass = [VIMVideo class];
descriptor.cachePolicy = VIMCachePolicy_NetworkOnly; // Or VIMCachePolicy_LocalOnly etc.
descriptor.shouldCacheResponse = NO; // Defaults to YES

...

// See VIMRequestDescriptor.h/m additional request configuration options

请求取消

id<VIMRequestToken> currentRequest = [[VIMSession sharedSession].client requestURI:@"/videos/77091919" completionBlock:^(VIMServerResponse *response, NSError *error) {

    id JSONObject = response.result;
    NSLog(@"JSONObject: %@", JSONObject);

}];

[[VIMSession sharedSession].client cancelRequest:currentRequest];

// or

[[VIMSession sharedSession].client cancelAllRequests];

轻量级使用

如果您想使用自己的 OAuth 令牌,可以绕过 VIMSession 及其认证机制,并按如下方式发送请求

VIMClient *client = [[VIMClient alloc] initWithDefaultBaseURL];
client.requestSerializer = ...

// Where client.requestSerializer is an AFJSONRequestSerializer subclass that sets the following information for each request:
// [serializer setValue:@"application/vnd.vimeo.*+json; version=3.2" forHTTPHeaderField:@"Accept"];
// [serializer setValue:@"Bearer your_oauth_token" forHTTPHeaderField:@"Authorization"];

[client requestURI:@"/videos/77091919" completionBlock:^(VIMServerResponse *response, NSError *error)
{

    id JSONObject = response.result;
    NSLog(@"JSONObject: %@", JSONObject);

}];

缓存

如果您想在更轻盈的使用场景中开启缓存

VIMClient *client = [[VIMClient alloc] initWithDefaultBaseURL];
client.cache = VIMCache *cache = [VIMCache sharedCache];
// Or client.cache = VIMCache *cache = [[VIMCache alloc] initWithName:@"your_cache_name"];

VIMObjectMapper

VIMObjectMapper 将 JSON 转换为模型对象。如果您想单独使用它(通过在项目中包含原始源,或通过包含 ObjectMapper subspec),请按以下步骤执行。

子类化 VIMModelObject

将您的自定义模型对象作为一个 VIMModelObject 的子类,并可选实现 VIMMappable 协议方法

#import "VIMModelObject.h"

@class VIMPictureCollection;

@interface VIMUser : VIMModelObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) VIMPictureCollection *pictureCollection;
@property (nonatomic, strong) NSDictionary *uploadQuota;
@property (nonatomic, strong) NSArray *websites;

@end
#import "VIMUser.h"
#import "VIMPictureCollection.h"
#import "VIMObjectMapper.h"

@implementation VIMUser

#pragma mark - VIMMappable // All methods are optional, implement to specify how the object should be "inflated"

- (NSDictionary *)getObjectMapping
{
return @{@"pictures": @"pictureCollection"};
}

- (Class)getClassForCollectionKey:(NSString *)key
{
if ([key isEqualToString:@"uploadQuota"])
{
return [NSDictionary class];
}

if ([key isEqualToString:@"websites"])
{
return [NSArray class];
}

return nil;
}

- (Class)getClassForObjectKey:(NSString *)key
{
if ([key isEqualToString:@"pictures"])
{
return [VIMPictureCollection class];
}

return nil;
}

- (void)didFinishMapping
{
// Do any post-parsing work you might want to do
}

获取一些 JSON

{
user = {
name = "Homer Simpson";
pictures = {
uri = "...";
sizes = (...);
};
"upload_quota" = { ... };
websites = ( ... );
};
}

让 VIMObjectMapper 开始工作

NSDictionary *JSON = ...;

VIMObjectMapper *mapper = [[VIMObjectMapper alloc] init];

[mapper addMappingClass:[VIMUser class] forKeypath:@"user"];

VIMUser *user = [mapper applyMappingToJSON:JSON];

发现问题了吗?

请在此处进行问题跟踪:[GitHub issue tracker](https://github.com/vimeo/VIMNetworking/issues)

想贡献吗?

如果您想进行贡献,请遵循我们提供的指南,见 [CONTRIBUTING.md](https://raw.github.com/vimeo/VIMNetworking/6.0.4/CONTRIBUTING.md)。

许可协议

VIMNetworking 在 MIT 许可协议下可用。有关更多信息,请参阅 [LICENSE](https://raw.github.com/vimeo/VIMNetworking/6.0.4/LICENSE.md) 文件。

还有问题吗?

在这里 Tweet 我们:[vimeoapi](https://twitter.com/vimeoapi)。

Stackoverflow 上发布关于 vimeo-ios 标签的帖子。

在此 联系我们。

对在Vimeo工作感兴趣吗?我们现在正 招聘