iOS 图文教程完成三方登陆

fdpg 8年前
   <p>前言: 首先你要明白, 这种集成三方的东西, 都非常简单,可能会有坑, 填上就好, 这篇文章以微博为例, 其实都是一样的. 步骤或繁或简单, 习惯就好.</p>    <p>1. 首先在微博开放平台注册成为开发者<a href="/misc/goto?guid=4959613981276441739">http://open.weibo.com</a></p>    <p>2. 创建应用</p>    <p><img alt="iOS 图文教程完成三方登陆" src="https://simg.open-open.com/show/b40daa064ecf4648e6ed17276451bc46.png"></p>    <p>3. 完善应用信息, 这里需要注意的是Bundle ID需要与应用一致, 同时记录appKey 项目中会用到</p>    <p>4. 在应用信息-高级信息中</p>    <p><img alt="iOS 图文教程完成三方登陆" src="https://simg.open-open.com/show/7a5c4abd163493160d4f0838262f38c8.png"></p>    <p><img alt="iOS 图文教程完成三方登陆" src="https://simg.open-open.com/show/f381c2c1cea07f34996c120fb84001ac.png"></p>    <p>与上方的保持一致---这段代码后面后使用到</p>    <p>5. 填写URL Types</p>    <p><img alt="iOS 图文教程完成三方登陆" src="https://simg.open-open.com/show/a99af863cd6f6980c42aacbe9b166ef0.png"></p>    <p>6. 配置info.plist同时加入白名单</p>    <p><img alt="iOS 图文教程完成三方登陆" src="https://simg.open-open.com/show/4419eb58397c5c91adcdcd65200bfa6d.png"></p>    <pre>  <code class="language-objectivec"> <array>    <string>sinaweibohd</string>    <string>sinaweibo</string>    <string>weibosdk</string>    <string>weibosdk2.5</string>   </array></code></pre>    <p>7. 导入SDK.</p>    <p>7.1 使用<code>cocoapods</code>你可以这样</p>    <pre>  <code class="language-objectivec">pod "WeiboSDK", :git => "https://github.com/sinaweibosdk/weibo_ios_sdk.git"</code></pre>    <p>7.2 或者直接下载拖入工程</p>    <p>8. 代码</p>    <pre>  <code class="language-objectivec">// AppDelegate中  #import <WeiboSDK.h>  #define WB_iOS_Key @""  @interface AppDelegate ()<WeiboSDKDelegate></code></pre>    <pre>  <code class="language-objectivec">   /**       *  1. 设置WeiboSDK的调试模式       *  2. 注册       */      [WeiboSDK enableDebugMode:YES];      [WeiboSDK registerApp:WB_iOS_Key];</code></pre>    <pre>  <code class="language-objectivec">#pragma mark - WeiboSDKDelegate  - (void)didReceiveWeiboRequest:(WBBaseRequest *)request  {    }  // 根据自己的需求写回调  // 这是微博Demo提供的代码  - (void)didReceiveWeiboResponse:(WBBaseResponse *)response  {  if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])      {          NSString *title = NSLocalizedString(@"发送结果", nil);          NSString *message = [NSString stringWithFormat:@"%@: %d\\n%@: %@\\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode, NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil),response.requestUserInfo];            WBSendMessageToWeiboResponse* sendMessageToWeiboResponse = (WBSendMessageToWeiboResponse*)response;          NSString* accessToken = [sendMessageToWeiboResponse.authResponse accessToken];          if (accessToken)          {              self.wbtoken = accessToken;          }          NSString* userID = [sendMessageToWeiboResponse.authResponse userID];          if (userID) {              self.wbCurrentUserID = userID;          }      }      else if ([response isKindOfClass:WBAuthorizeResponse.class])      {          NSString *title = NSLocalizedString(@"认证结果", nil);          NSString *message = [NSString stringWithFormat:@"%@: %d\\nresponse.userId: %@\\nresponse.accessToken: %@\\n%@: %@\\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode,[(WBAuthorizeResponse *)response userID], [(WBAuthorizeResponse *)response accessToken],  NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil), response.requestUserInfo];          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title                                                          message:message                                                         delegate:nil                                                cancelButtonTitle:NSLocalizedString(@"确定", nil)                                                otherButtonTitles:nil];            self.wbtoken = [(WBAuthorizeResponse *)response accessToken];          self.wbCurrentUserID = [(WBAuthorizeResponse *)response userID];          self.wbRefreshToken = [(WBAuthorizeResponse *)response refreshToken];          [alert show];      }  }  - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation  {      return [WeiboSDK handleOpenURL:url delegate:self];  }    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url  {      return [WeiboSDK handleOpenURL:url delegate:self ];  }</code></pre>    <p>9. 写个登陆按钮登陆</p>    <pre>  <code class="language-objectivec">- (void)loginClick  {      [[[[UIApplication sharedApplication] delegate] window] makeKeyWindow];        WBAuthorizeRequest *request = [WBAuthorizeRequest request];      // 前面提到的      request.redirectURI = kRedirectURI;      request.scope = @"all";      request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",                           @"Other_Info_1": [NSNumber numberWithInt:123],                           @"Other_Info_2": @[@"obj1", @"obj2"],                           @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};      [WeiboSDK sendRequest:request];  }</code></pre>    <p>10. 完成图</p>    <p><img alt="iOS 图文教程完成三方登陆" src="https://simg.open-open.com/show/0ce19855677be2924f78a79d047dccca.png"></p>    <p><img alt="iOS 图文教程完成三方登陆" src="https://simg.open-open.com/show/fb6ebdc5f2f1852656f80b100b99d59a.png"></p>    <p><img alt="iOS 图文教程完成三方登陆" src="https://simg.open-open.com/show/032f0108c6fe72e3cf1c2850eaf50a38.png"></p>    <p>状态0 代表成功. 如果回忆没有错基本就成功了. O.-</p>    <p>来自:http://www.jianshu.com/p/b8b0e085b074<br>  </p>