InstagramKit
Instagram API 的一个大型的 Objective C 封装,完全兼容 Swift。
以下是一个快速示例,用于检索 Instagram 上的热门媒体
InstagramEngine *engine = [InstagramEngine sharedEngine];
[engine getPopularMediaWithSuccess:^(NSArray *media, InstagramPaginationInfo *paginationInfo) {
// media is an array of InstagramMedia objects
...
} failure:^(NSError *error, NSInteger statusCode) {
...
}];
此框架建立在 AFNetworking 的基于块的架构之上,并此外异步解析 JSON 数据并创建模型对象,因此绝对不存在主线程上的解析。它很漂亮、很快,效果奇妙。
示例
要运行示例项目,请克隆存储库,然后首先从 Example 目录中运行 pod install
。
安装
InstagramKit 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile
pod 'InstagramKit'
如果您的 App 使用授权,并且希望由 UICKeyChainStore 自动处理在 Keychain 中的访问令牌的存储和检索,则替换以下行 -
pod 'InstagramKit'
pod 'InstagramKit/UICKeyChainStore'
InstagramKit 使用 UICKeyChainStore 作为可选的子依赖项进行 Keychain 访问。如果您选择使用可选的 pod,InstagramKit 无需任何额外代码即可在 App 启动之间继续您的认证会话。
Instagram 开发者注册
前往 http://instagram.com/developer/clients/manage/,将您的应用与 Instagram 注册,并在您的应用的 Info.plist 文件中设置正确的 InstagramAppClientId
和 InstagramAppRedirectURL
凭据。
InstagramAppClientId
是您的应用客户端 ID,而 InstagramAppRedirectURL
是在 Instagram 开发者仪表板上注册您的应用时获得的重定向 URI。重定向 URI 指定了 Instagram 在用户选择是否验证您的应用后应将用户重定向到的位置。
Instagram 平台更新
Instagram 经常更新其 API 并弃用正在使用的端点。如果您从服务器响应中看到 400 或其他奇怪的错误,请检查 Instagram 的 API 变更日志,并创建一个问题,报告您的发现。 https://www.instagram.com/developer/changelog/
使用
认证
对于每个 API 调用,您都需要访问令牌和特定的作用域权限。要获取访问令牌,用户需要通过认证您的应用来访问他们带有指定权限的 Instagram 账户。
为此,请将用户重定向到 https://instagram.com/oauth/authorize/?client_id=[Client ID]&redirect_uri=[Redirect URI]&response_type=token
,或者允许 InstagramEngine 的辅助方法为您完成 tough work -
NSURL *authURL = [[InstagramEngine sharedEngine] authorizationURL];
[self.webView loadRequest:[NSURLRequest requestWithURL:authURL]];
作用域
所有应用默认都拥有基本的读取访问权限,但如果您计划请求扩展权限,如点赞、评论或管理好友,则需要使用InstagramKitScope枚举在授权请求中指定这些作用域。
注意,为了使用这些扩展权限,您首先需要将您的应用提交给Instagram进行审核。
为了让您的应用能够POST或DELETE点赞、评论或关注,您必须在这里申请: https://#/help/instagram/contact/185819881608116#
// Set scope depending on permissions your App has been granted from Instagram
// InstagramKitLoginScopeBasic is included by default.
InstagramKitLoginScope scope = InstagramKitLoginScopeRelationships | InstagramKitLoginScopeComments | InstagramKitLoginScopeLikes;
NSURL *authURL = [[InstagramEngine sharedEngine] authorizationURLForScope:scope];
[self.webView loadRequest:[NSURLRequest requestWithURL:authURL]];
一旦用户授权您的应用,他们将被重定向到一个类似 https:///#access_token=[access_token]
的url,并且 [access_token]
将以点分隔,例如 [userID].[剩余的访问令牌]
。InstagramEngine 包含一个用来验证此令牌的帮助方法。
UIWebView
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSError *error;
if ([[InstagramEngine sharedEngine] receivedValidAccessTokenFromURL:request.URL error:&error]) {
// success!
...
}
return YES;
}
WKWebView
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler
{
NSError *error;
if ([[InstagramEngine sharedEngine] receivedValidAccessTokenFromURL:navigationAction.request.URL error:&error]) {
// success!
...
}
decisionHandler(WKNavigationActionPolicyAllow);
}
Authenticated Requests
一旦您进行认证并且InstagramKit提供了accessToken
,它会自动将其保存在调用-logout
在InstagramEngine之前。认证调用看起来没有区别。
InstagramEngine *engine = [InstagramEngine sharedEngine];
[engine getSelfRecentMediaWithSuccess:^(NSArray *media, InstagramPaginationInfo *paginationInfo) {
// media is an array of InstagramMedia objects
...
} failure:^(NSError *error, NSInteger statusCode) {
...
}];
分页
InstagramPaginationInfo
对象包含您需要做出下一个分页调用的一切。
如果您需要获取分页结果列表,请使用接受count
和maxId
作为参数的方法。例如,使用getMediaForUser:count:maxId:withSuccess:failure:
将获得的下一个maxId
传递给maxId
参数,每次从最新分页信息的paginationInfo.nextMaxId
得到。
[engine getMediaForUser:user.Id
count:15
maxId:self.currentPaginationInfo.nextMaxId
withSuccess:^(NSArray *media, InstagramPaginationInfo *paginationInfo)
{
if (paginationInfo) {
self.currentPaginationInfo = paginationInfo;
}
...
}
failure:^(NSError *error)
{
...
}];
第一次请求时,maxId
参数为nil。
Instagram API 中每个支持分页的端点通常都支持一个count参数。您可以使用此方法,并将count参数传递给每个分页请求。在没有分页需求但却需要在第一个请求中指定feed数量时,您也可以使用它。
在分页Wiki中详细了解如何轻松实现您请求的分页方式。
有贡献吗?
很高兴您这么问。查看公开问题并积极参与。
有问题吗?
如果出现问题,请参考Instagram API 文档。请在发布问题前确保已经阅读过文档。
作者
Shyam Bhat,[email protected] Twitter: @bhatthead
许可证
InstagramKit 在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。
==================
InstagramKit使用公开的Instagram API,并未与Instagram或Facebook有任何关联。