我们不再维护此存储库!请访问新的 GIPHY SDK 以获取将 GIPHY 添加到应用程序的最佳和最简单的方式。
Giphy Core SDK for Swift
Giphy Core SDK 是围绕 Giphy API 的包装。
Giphy 是在互联网上搜索、分享和发现 GIF 的最佳方式。与搜索引擎类似,我们的大部分内容来自对最佳和最受欢迎的 GIF 以及网络上的搜索词进行索引。我们整理了所有这些 GIF,以便您更容易找到优质内容并通过社交渠道进行分享。我们还特色展示了一些我们最喜欢的 GIF 艺术家,并与品牌合作创建和推广他们的原创 GIF 内容。
入门
支持的平台
- iOS
- macOS
- tvOS
- watchOS
支持的端点
高级用法
设置
CocoaPods 设置
将 GiphyCoreSDK条目添加到您的 Podfile
pod 'GiphyCoreSDK'
运行 pods 以获取 GiphyCoreSDK 框架
pod install
Carthage 设置
将 GiphyCoreSDK条目添加到您的 Cartfile
git "[email protected]:Giphy/giphy-ios-sdk-core.git" "master"
运行 carthage update 以获取 GiphyCoreSDK 框架
carthage update
Swift 包管理器设置
- 将 .package(url:“[https://github.com/Giphy/giphy-ios-sdk-core](https://github.com/Giphy/giphy-ios-sdk-core)”, from: “1.2.0”) 添加到您的包依赖项。
- 然后添加 GiphyCoreSDK 到您的目标依赖项。
初始化 Giphy SDK
// Configure your API Key
GiphyCore.configure(apiKey: "YOUR_API_KEY")
搜索接口
搜索包含指定词汇或短语的Giphy GIF。标点符号将被移除并忽略。
/// Gif Search
let op = GiphyCore.shared.search("cats") { (response, error) in
if let error = error as NSError? {
// Do what you want with the error
}
if let response = response, let data = response.data, let pagination = response.pagination {
print(response.meta)
print(pagination)
for result in data {
print(result)
}
} else {
print("No Results Found")
}
}
/// Sticker Search
let op = GiphyCore.shared.search("dogs", media: .sticker) { (response, error) in
//...
}
热门接口
获取当前网络上热门的GIF。由Giphy编辑团队精心挑选。返回的数据与Giphy主页上展示的热门GIF一致。
/// Trending GIFs
let op = GiphyCore.shared.trending() { (response, error) in
//...
}
/// Trending Stickers
let op = GiphyCore.shared.trending(.sticker) { (response, error) in
//...
}
随机接口
按标签限制返回随机GIF。不包含标签参数将返回Giphy目录中的随机GIF。
/// Random GIF
let op = GiphyCore.shared.random("cats") { (response, error) in
if let error = error as NSError? {
// Do what you want with the error
}
if let response = response, let data = response.data {
print(response.meta)
print(data)
} else {
print("No Result Found")
}
}
/// Random Sticker
let op = GiphyCore.shared.random("cats", media: .sticker) { (response, error) in
//...
}
通过ID获取GIF接口
通过GIF ID返回有关GIF的元数据。以下示例中的GIF ID是"feqkVgjJpYtjy"。
/// Gif by Id
let op = GiphyCore.shared.gifByID("feqkVgjJpYtjy") { (response, error) in
//...
}
通过ID获取多个GIF接口
get GIF by ID端口的multiget版本。 在这种情况下,ID为feqkVgjJpYtjy和7rzbxdu0ZEXLy。
/// GIFs by Ids
let ids = ["feqkVgjJpYtjy", "7rzbxdu0ZEXLy"]
let op = GiphyCore.shared.gifsByIDs(ids) { (response, error) in
if let error = error as NSError? {
// Do what you want with the error
}
if let response = response, let data = response.data, let pagination = response.pagination {
print(response.meta)
print(pagination)
for result in data {
print(result)
}
} else {
print("No Result Found")
}
}
高级用法
过滤模型
我们在请求过程中增加了对任何模型结果进行过滤的支持。以下是几个代码示例用例。
GiphyCore.setFilter(filter: { obj in
if let obj = obj as? GPHMedia {
// Check to see if this Media object has tags
// Say we only want GIFs/Stickers with tags, otherwise filter them out
return obj.tags == nil
} else if let obj = obj as? GPHChannel {
// We only want channels which have featured Gifs
return obj.featuredGif != false
}
// Otherwise this is a valid object, don't filter it out
return true
})
用户词典
我们认为你可能想给我们的模型添加额外数据,比如 GPHMedia
,所以现在所有的模型都有了 userDictionary
,你可以将任何对象附加到我们的模型中。
/// Gif Search
let op = GiphyCore.shared.search("cats") { (response, error) in
if let error = error as NSError? {
......
}
if let response = response, let data = response.data, let pagination = response.pagination {
for result in data {
result.userDictionary = ["Description" : "Results from Cats Search"]
}
} else {
print("No Results Found")
}
}