测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新发布 | 2016年10月 |
SPM支持 Swift Package Manager (SPM) | ✗ |
由 Yansong Li 维护。
这是一个 Swift Github API Wrapper,如果您想要使用 GitHub 的奇妙数据来制作应用,它可以让您的生活变得轻松一些。
前往您的 GitHub 主页,点击您的头像 -> 设置,在左侧选择 应用 -> 开发者应用,然后您应该在右上角点击 注册一个新的 OAuth 应用。
请记住您应该使用自定义的 授权回调 URL,稍后将使用它,例如 FunnyGithubTest://random。注册后,您可以获得 客户端 ID 和 客户端密钥。
要允许用户在 OAuth 舞蹈后被重定向回您的应用,您需要将与您的应用关联一个自定义 URL 方案。
打开您的 Xcode,然后打开您的项目的 Info.plist。将以下代码复制并粘贴到 Info.plist 源代码中。
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>your.custom.scheme(eg. FunnyGithubTest)</string>
</array>
<dict>
<array>
首先,在您的 AppDelegate 中顶部添加 import GithubPilot
。然后,您可以添加 application(_: didFinishLaunchingWithOptions:)
与以下代码进行身份验证客户端。同时,请确保处理好您的客户端将使用的 scope
参数,请参见 GitHub Scope
Github.setupClientID("YourClientID", clientSecret: "YourClientSecret", scope: ["user", "repo"], redirectURI: "YourCustomCallBackURL")
Github.authenticate()
其次,将以下代码添加到您的 AppDelegate 中以获取 GitHub 访问令牌
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
{
Github.requestAccessToken(url)
return true
}
if let client = Github.authorizedClient {
client.users.getAuthenticatedUser().response({ user, requestError in
if let me = user {
print(me.description)
} else {
print(requestError?.description)
}
})
}
if let client = Github.authorizedClient {
client.users.getUser(username: "onevcat").response({ (githubUser, error) -> Void in
if let user = githubUser {
print(user.description)
} else {
print(error?.description)
}
})
}
if let client = Github.authorizedClient {
client.users.getAllUsers("1209").response({ (httpResponse, users, requestError) -> Void in
if let response = httpResponse {
// next `since` id
print("Since :\(response)")
}
if let result = users {
for user in result {
print(user.description)
}
} else {
print(requestError?.description)
}
})
}
if let client = Github.authorizedClient {
client.repos.getAuthenticatedUserRepos().response({ (result, error) -> Void in
if let repos = result {
print(repos.count)
for i in repos {
print(i.name)
print(i.stargazersCount)
}
}
if let requestError = error {
print(requestError.description)
}
})
}
if let client = Github.authorizedClient {
client.repos.getRepo("Yep", owner: "CatchChat").response({ (result, error) -> Void in
if let repo = result {
print(repo.name)
}
if let requestError = error {
print(requestError.description)
}
})
}
if let client = Github.authorizedClient {
client.repos.getRepoFrom(owner: "onevcat").response({ (nextPage, result, error) -> Void in
if let page = nextPage {
print("Next Page is \(page)")
}
if let repos = result {
print(repos.count)
for r in repos {
print(r.name)
print(r.stargazersCount)
}
}
if let requestError = error {
print(requestError.description)
}
})
}
if let client = Github.authorizedClient {
client.events.getReceivedEventsForUser("someUser", page: "1").response({ (nextpage, results, error) -> Void in
if let events = results {
// New events
}
})
}
您可以参考我的项目中的一个GitPocket作为例子。
还有许多其他API尚未实现,例如 搜索。我将不断改进这个仓库。欢迎提交pull请求和打开问题。