相關資源地址:
本項目demo地址 : https://github.com/zhonggaorong/weiboSDKDemo
最新SDK下載: 最新微博SDK
官網注冊地址:點擊打開鏈接
最新版本的微博登錄實現步驟實現:
1. 使用此SDK需滿足以下條件:
- 在新浪微博開放平台注冊並創建應用
- 已定義本應用的授權回調頁
- 已選擇應用為iOS平台,並正確填寫Bundle id和apple id (要與Xcode 工程中的bundle相對應)
注: 關於授權回調頁對移動客戶端應用來說對用戶是不可見的,所以定義為何種形式都將不影響,但是沒有定義將無法使用SDK認證登錄。建議使用默認回調頁 https://api.weibo.com/oauth2/default.html
2. 最新SDK目錄結構預覽:

3. 把SDK集成到工程。
1. 通過pod 集成。
- pod "WeiboSDK"
2. 手動導入sdk
1. 把上面預覽圖上面的文件,全部拖入工程中。

2. Other Linker Flags 增加 -all_load. (一定要把官方說的 -Objc 換成 -all_load) 好大一個坑。

3. 添加依賴庫。
在 Target->Buid Phases->Link BinaryWith Libraries 下增加以下庫: QuartzCore.frameworkImageIO.framework SystemConfiguration.framework Security.framework CoreTelephony.frameworkCoreText.framework UIKit.framework Foundation.framework CoreGraphics.framework libz.dyliblibsqlite3.dylib

4. 編譯工程。應該編譯成功。
5. 針對iOS9的相關問題。
1. 你需要用到微博的相關功能,如登陸,分享等。並且需要實現跳轉到微博的功能,在iOS9系統中就需要在你的app的plist中添加下列鍵值對。否則在canOpenURL函數執行時,就會返回NO。了解詳情請至
https://developer.apple.com/videos/wwdc/2015/?id=703
- <key>LSApplicationQueriesSchemes</key>
- <array>
- <string>sinaweibohd</string>
- <string>sinaweibo</string>
- <string>weibosdk</string>
- <string>weibosdk2.5</string>
- </array>

2. 增加url Types。

注意: bundle ideatefier 一定要與 微博開發者后台要一致,不然會出現調試不成功,有以下錯誤出現。
- Flag Status: -R ------- networkStatusForFlags
- 2016-06-21 15:18:31.556 weiboSDKDemo[906:299845] -canOpenURL: failed for URL: "weibosdk://" - error: "(null)"
- 2016-06-21 15:18:31.720 weiboSDKDemo[906:299845] Reachability Flag Status: -R ------- networkStatusForFlags
- 2016-06-21 15:18:33.350 weiboSDKDemo[906:299845] -[AppDelegate application:openURL:sourceApplication:annotation:]: unrecognized selector sent to instance 0x12d615090
- 2016-06-21 15:18:33.351 weiboSDKDemo[906:299845] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate application:openURL:sourceApplication:annotation:]: unrecognized selector sent to instance 0x12d615090'
- *** First throw call stack:
- (0x1823fee38 0x181a63f80 0x182405ccc 0x182402aa4 0x182300d1c 0x100116a30 0x10021da7c 0x10021da3c 0x1002234e4 0x1823b4dd8 0x1823b2c40 0x1822dcd10 0x183bc4088 0x1875b1f70 0x1000d4f54 0x181e7a8b8)
- libc++abi.dyli
4. 正式進行相關編程了。
appDelegate.m中
1. 向微博進行注冊微博, 通過申請的appkey。
2. 重寫系統方法
- application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{}
具體代碼如下:
- //
- // AppDelegate.h
- // weiboSDKDemo
- //
- // Created by 張國榮 on 16/6/21.
- // Copyright © 2016年 BateOrganization. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import "WeiboSDK.h"
- @protocol WeiBoDelegate <NSObject>
- //登錄的代理
- -(void)weiboLoginByResponse:(WBBaseResponse *)response;
- //分享的大力
- -(void)weiboShareSuccessCode:(NSInteger)shareResultCode;
- @end
- @interface AppDelegate : UIResponder <UIApplicationDelegate>
- @property (strong, nonatomic) UIWindow *window;
- @property (weak , nonatomic) id<WeiBoDelegate> weiboDelegate;
- @end
- //
- // AppDelegate.m
- // weiboSDKDemo
- //
- // Created by 張國榮 on 16/6/21.
- // Copyright © 2016年 BateOrganization. All rights reserved.
- //
- #import "AppDelegate.h"
- //申請下來的appkey
- #define APP_KEY @"app key"
- @interface AppDelegate ()<WeiboSDKDelegate>
- @end
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
- [WeiboSDK registerApp:APP_KEY];
- return YES;
- }
- // 9.0 后才生效
- -(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
- return [WeiboSDK handleOpenURL:url delegate:self];
- }
- #pragma mark 9.0之前
- - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
- return [WeiboSDK handleOpenURL:url delegate:self];
- }
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation{
- return [WeiboSDK handleOpenURL:url delegate:self];
- }
- /**
- 收到一個來自微博客戶端程序的請求
- 收到微博的請求后,第三方應用應該按照請求類型進行處理,處理完后必須通過 [WeiboSDK sendResponse:] 將結果回傳給微博
- @param request 具體的請求對象
- */
- - (void)didReceiveWeiboRequest:(WBBaseRequest *)request{ //向微博發送請求
- NSLog(@" %@",request.class);
- }
- /**
- 微博分享 與 微博登錄,成功與否都會走這個方法。 用戶根據自己的業務進行處理。
- 收到一個來自微博客戶端程序的響應
- 收到微博的響應后,第三方應用可以通過響應類型、響應的數據和 WBBaseResponse.userInfo 中的數據完成自己的功能
- @param response 具體的響應對象
- */
- - (void)didReceiveWeiboResponse:(WBBaseResponse *)response{
- if ([response isKindOfClass:WBAuthorizeResponse.class]) //用戶登錄的回調
- {
- if ([_weiboDelegate respondsToSelector:@selector(weiboLoginByResponse:)]) {
- [_weiboDelegate weiboLoginByResponse:response];
- }
- }
- }
- @end
調用登錄的viewController.m 下面是具體的代碼:
- //
- // ViewController.m
- // weiboSDKDemo
- //
- // Created by 張國榮 on 16/6/21.
- // Copyright © 2016年 BateOrganization. All rights reserved.
- //
- #import "ViewController.h"
- #import "WeiboSDK.h"
- #import "AppDelegate.h"
- #define APP_REDIRECT_URL @"回調地址"
- @interface ViewController ()<WeiBoDelegate>
- {
- AppDelegate *delgate;
- }
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- }
- - (IBAction)weiboLoginAction:(id)sender {
- delgate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- delgate.weiboDelegate = self;
- WBAuthorizeRequest *request = [WBAuthorizeRequest request];
- //回調地址與 新浪微博開放平台中 我的應用 --- 應用信息 -----高級應用 -----授權設置 ---應用回調中的url保持一致就好了
- request.redirectURI = APP_REDIRECT_URL;
- //SCOPE 授權說明參考 http://open.weibo.com/wiki/
- request.scope = @"all";
- request.userInfo = nil;
- [WeiboSDK sendRequest:request];
- }
- -(void)weiboLoginByResponse:(WBBaseResponse *)response{
- NSDictionary *dic = (NSDictionary *) response.requestUserInfo;
- NSLog(@"userinfo %@",dic);
- }
- - (IBAction)weiboShareAction:(id)sender {
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end