橙OAuth 1.0.2

×

语言语言
Objective-CObjective C 许可证
MIT 发布上次发布
2015年6月Maintained by Ben Rosen.

安装指南



×

安装

安装橙OAuth最简单的方式是从Cocoapods安装。在你的Podfile中,添加一行:pod 'OrangeOAuth'。你也可以使用详细示例项目进行测试。

如果你愿意手动安装,只需将这些文件添加到项目中:CARedditOAuth.hCARedditOAuth.mCARedditOAuthDelegate.hCAWebViewController.hCAWebViewController.m以及这里找到的资源。

使用方法

橙OAuth的使用设计得尽可能简单,但仍然需要对Reddit和您的应用进行大量的准备工作。为了设置您的应用以进行OAuth,请遵循以下步骤,包括Reddit和您的iOS客户端。

Web

前往Reddit网站下的“应用”。然后创建一个应用。输入名称、勾选安装的应用,并将redirect_uri输入为您的bundle identifier,后面加上://response。对我来说,就是com.contextdev.orangeoauth://response

保存应用后,您应该看到以下这样的界面

保存应用后,你应该能看到像这样

在“安装的应用”下面是你的客户端标识符。在接下来的一步中,这将非常重要,所以请记住它。

iOS

首先,请在你应用的App Delegate中导入所需的OrangeOAuth文件。

#import <CARedditOAuth.h>

然后,在App Delegate中放入以下代码。当URL scheme被运行时,这会使我的代码为您完成剩余的所有工作,手动。

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [[CARedditOAuth sharedInstance] recievedURL:url];
}

完成这些后,打开您将要实际展示OAuth视图控制器的文件。在这个文件中,导入必要的文件。

#import <CARedditOAuth.h>

遵守CARedditOAuthDelegate协议。现在,当您要推送Reddit视图控制器时,请运行以下代码

// [CARedditOAuth sharedInstance] is the instance you should use.
CARedditOAuth *redditOAuth = [CARedditOAuth sharedInstance];

// Client identifier is from the Reddit OAuth API.
redditOAuth.clientIdentifier = @"RV9IIn94H5YmNQ";

// Scopes are the permissions you want on this users Reddit account.
redditOAuth.scopes = @[@"identity"];

// This is the URL scheme you created earlier.
redditOAuth.urlScheme = @"com.contextdev.orangeoauth";

// Set the delegate so that the library can work.
redditOAuth.delegate = self;

// This will start the OAuth process.
[redditOAuth startRedditOAuth];

客户端标识符应该是你在Reddit网站上设置应用程序时获得的标识符。作用域应该是请求的权限的字符串数组。您可以在Reddit的GitHub wiki上查看所有作用域在这里。URL方案应该是您的bundle标识符。委托应该设置为self

在您设置委托的类中,您必须实现以下方法

- (void)redditOAuth:(CARedditOAuth *)redditOAuth presentViewController:(UIViewController *)viewController {
    [self presentViewController:viewController animated:YES completion:nil];
}

- (void)redditOAuth:(CARedditOAuth *)redditOAuth didFinishWithAuthorizationCode:(NSString *)authorizationCode {
    NSLog(@"The authorization code is %@", authorizationCode);
}

在第一个方法中,您需要显示提供的视图控制器,无论是展示它、推送它,还是做其他事情。在第二个方法中,您会收到Reddit的授权代码,这意味着OAuth过程已完成。

现在,在项目中的info选项卡下添加一个URL类型。将标识符和URL方案设置为您的bundle标识符。这对您的OAuth过程至关重要。

注意: iOS 9中的URL方案

  • iOS 9中的URL方案已更改。苹果使它们更加安全。
  • 这篇文章这个WWDC会议中有所讨论。
  • 如果您在iOS 9中部署应用程序,在Info.plist中添加键LSApplicationQueriesSchemes,作为一个数组,将org-appextension-feature-password-management和您的bundle标识符放入数组中。

就是这样!

将调用委托方法,您将从Reddit收到OAuth代码。接下来的步骤由您决定!您可以在Reddit的OAuth wiki上查看您可以做什么在这里

作者

Context Apps,https://github.com/contextapps

Ben Rosen,[email protected]

许可

OrangeOAuth: Orangered's dead-simple drop-in Reddit OAuth library.
Copyright (C) 2015 Context Apps

The MIT License (MIT)     
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.