以下几个Objective-C类为TVDB XML API提供封装,并可用于iOS应用中。这些类是Objective-C ARC(自动引用计数),因此您需要在XCode中启用ARC以设置新项目。ARC在iOS 4.0及更高版本中受支持,但您需要使用iOS 5.x SDK。
iTVDb通过CocoaPods提供,因此您可以添加iTVDb作为pod或者老式方式添加。以下均展示如下:
*.h
和*.m
文件从拖拽到您的项目中。#import "iTVDb.h"
要开始使用iTVDb iOS封装类,您首先需要从TVDB网站获取API密钥。您可以在TVDB网站上注册获取API密钥。一旦您有了一个API密钥,您需要通过以下方式初始化TVDbClient:
[[TVDbClient sharedInstance] setApiKey: @"YOUR API KEY"];
现在您的API密钥已设置好,您就可以开始使用TVDbShow
和TVDbEpisode
类了。
如上所述,除了TVDbShow
类上的findByName:
方法外,要想在iOS应用中使用iTVDb iOS封装,需要API密钥。下面将展示如何获取所有与'权力的游戏'匹配的电视节目。
NSMutableArray *shows = [TVDbShow findByName:@"Game of Thrones"];
此操作将内部从TVDB API获取XML文件,将其转换为NSDictionary,并将其加载到TVDbShow对象中,然后将该对象添加到名为shows
的NSMutableArray中。从这次调用获取的数据是关于电视节目的基本信息。
如果您想获取更详细信息,则需要API密钥。由于详细的详细信息需要通过另一个API调用获取,NSMutableArray shows
可以包含多个TVDbShow实例,但在这个例子中是一个单独的对象。以下属性可以从TVDbShow实例中访问
show.showId // 121361
show.title // Game of Thrones
show.description // Based on the fantasy novel series "A Song of Ice and Fire," Game of Thrones explores the story of an epic battle among seven kingdoms and two ruling families in the only game that matters - the Game of Thrones. All seek control of the Iron Throne, the possession of which ensures survival through the 40-year winter to come.
show.imdbId // tt0944947
show.premiereDate // 2011-04-17
show.banner // http://www.thetvdb.com/api/banners/graphical/121361-g19.jpg
show.bannerThumbnail // http://www.thetvdb.com/api/banners/_cache/graphical/121361-g19.jpg
要获取'权力的游戏'的详细信息,则需要从之前的API调用中获取showId
。以下是如何获取此类信息的示例。
TVDbShow *show = [TVDbShow findById:[NSNumber numberWithInt:121361]];
show
属性包括所有以上信息和以下内容:
show.status // Continuing
show.genre // ["Action and Adventure", "Drama", "Fantasy"]
show.actors // ["Peter Dinklage", "Kit Harington", "Emilia Clarke", ...]
show.airDay // Sunday
show.airTime // 9:00 PM
show.runtime // 60
show.network // HBO
show.contentRating // TV-MA
show.rating // 9.4
show.poster // http://www.thetvdb.com/api/banners/posters/121361-13.jpg
show.posterThumbnail // http://www.thetvdb.com/api/banners/_cache/posters/121361-13.jpg
show.episodes // [<TVDbEpisode instance>, <TVDbEpisode instance>, ...]
当您有一个TVDbShow
类的实例时,您可以通过调用:show.episodes
来检索所属的剧集。但您也可以使用以下描述的findById:
和findByShowId:seasonNumber:episodeNumber:
类方法来检索一个剧集
TVDbEpisode *episode = [TVDbEpisode findById:[NSNumber numberWithInt:4245779]];
TVDbEpisode *episode = [TVDbEpisode findByShowId:[NSNumber numberWithInt:121361] seasonNumber:[NSNumber numberWithInt:2] episodeNumber:[NSNumber numberWithInt:9]];
上述类方法都返回一个TVDbEpisode
实例。可以从episode
(两种情况下)检索的属性如下
episode.episodeId // 4245779
episode.title // Valar Morghulis
episode.description // Tyrion awakens to a changed situation. King Joffrey doles out rewards to his subjects. As Theon stirs his men to action, Luwin offers some final advice. Brienne silences Jaime. Arya receives a gift from Jaqen. Dany goes to a strange place. Jon proves himself to Qhorin.
episode.seasonNumber // 2
episode.episodeNumber // 10
episode.banner // http://www.thetvdb.com/api/banners/episodes/121361/4245779.jpg
episode.bannerThumbnail // http://www.thetvdb.com/api/banners/_cache/episodes/121361/4245779.jpg
episode.writer // ["David Benioff", "D.B. Weiss"]
episode.director // ["Alan Taylor"]
episode.gueststars // in this case it's empty, but when filled it's an array like `episode.writer`
episode.imdbId // tt2112510
episode.premiereDate // 2012-06-03
episode.rating // 8.5
episode.showId // 121361
当您的应用中有一系列剧集时,您可能想要检索有新剧集的剧集(或者可能只是剧集)。TVDbUpdater
单例可以帮您做这个。第一次您有一系列剧集(或剧集)时,您应该在您的应用中保存该时刻的时间戳。嗯,就像以下这样简单
[[TVDbUpdater sharedInstance] updateLastUpdatedAtTimestamp];
完成此操作后,您就已经为下一次更新做好了准备。假设您想在第二天更新剧集列表(或剧集)。您可以使用以下方法只检索更新的剧集,剧集或两者(从那一刻起)
NSDictionary *showUpdates = [[TVDbUpdater sharedInstance] showUpdates];
NSDictionary *episodeUpdates = [[TVDbUpdater sharedInstance] episodeUpdates];
NSDictionary *updates = [[TVDbUpdater sharedInstance] updates];
为了澄清,所有这些方法都将从保存时间戳的updateLastUpdatedAtTimestamp
方法开始检索数据,直到您调用上述列出的任何一个更新方法的那一刻。
iTVDb使用bcaccinolo的XML-to-NSDictionary库将检索到的XML转换为NSDictionary。
非常欢迎您为此项目做出贡献。为此,请按照以下步骤操作
git checkout -b my-new-feature
创建您的功能分支git commit -am '添加新功能'
git push origin my-new-feature
版权所有2012 Kevin Tuhumury。遵照MIT许可证发布。