ImgurSession 是一个 Objective-C 网络库,易于在 iOS 和 OS X 应用程序中发送 Imgur API 请求。它基于 AFNetworking 的 AFHTTPSessionManager 基础类。ImgurSession 提供了 API V3 的访问权限。它处理用户认证会话的 OAuth2 认证,也支持匿名会话的基本认证。它涵盖了 Imgur 文档中所有的端点。这正在我的免费应用程序 Imgurian 的生产中使用。
该项目最初由 Johann Pardanaud 从 ImgurKit 分叉而来,并进行了重构。
完整的 Imgur API V3
OAuth2 管理。
用户账户
只需导入 ImgurSession.h 并使用您的凭据设置会话,然后进行任何请求。对于用户授权,您必须根据令牌类型向 OAuth 2 授权注册,带有或没有回调 URL(在 Imgur 上阅读更多)。对于授权登录,您必须实现 imgurSessionNeedsExternalWebview: 代理方法,以便打开一个外部的 imgur.com 页面进行 OAuth 授权。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[IMGSession authenticatedSessionWithClientID:@"clientID" secret:@"secret" authType:IMGCodeAuth withDelegate:self];
return YES;
}
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
/*
configure you app URL schema to handle the callback url then call completion with retrieved code from URL
*/
}
#pragma mark - IMGSessionDelegate
-(void)imgurSessionNeedsExternalWebview:(NSURL *)url completion:(void (^)())completion{
//open imgur website to authenticate with callback url in safari
[[UIApplication sharedApplication] openURL:url];
//save the completion block for later use when imgur responds with url callback
}
或者匿名会话(如注册的 Imgur 应用程序所配置的)。
[IMGSession anonymousSessionWithClientID:@"anonToken" withDelegate:self];
在其他任何应用程序位置,都会使用先前创建的会话单例进行请求以处理认证和错误处理。要检索病毒画廊:
[IMGGalleryRequest hotGalleryPage:0 success:^(NSArray *objects) {
//use gallery objects in a table for example
self.tableRows = objects;
[self.tableView reloadData];
} failure:^(NSError *error) {
//handle error
}];