pod 'WeiboSDK')。Info.plist 中配置 LSApplicationQueriesSchemes (添加 sinaweibo, sinaweibohd 等) 及 URL Types (URL Scheme 設為 wb+你的AppKey)。#define kAppKey @"你的AppKey"
#define kAppSecret @"你的AppSecret"
#define kRedirectURI @"你的授權回調頁"
// AppDelegate.h
#import <UIKit/UIKit.h>
@class SinaWeibo; // 前向宣告
@interface AppDelegate : UIResponder <UIApplicationDelegate, SinaWeiboDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) SinaWeibo *sinaWeibo;
@end
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self _initSinaWeibo];
return YES;
}
- (void)_initSinaWeibo {
_sinaWeibo = [[SinaWeibo alloc] initWithAppKey:kAppKey appSecret:kAppSecret redirectURI:kRedirectURI delegate:self];
// 讀取本地儲存的 Token (實現自動登入)
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *authData = [defaults objectForKey:@"SinaWeiboAuthData"];
if (authData[@"accessToken"] && authData[@"expirationDate"] && authData[@"userId"]) {
_sinaWeibo.accessToken = authData[@"accessToken"];
_sinaWeibo.expirationDate = authData[@"expirationDate"];
_sinaWeibo.userId = authData[@"userId"];
NSLog(@"本地授權信息讀取成功,用戶已自動登入");
}
}
// 實現 Delegate: 登入成功回調
- (void)sinaweiboDidLogIn:(SinaWeibo *)sinaweibo {
NSDictionary *authData = @{
@"accessToken": sinaweibo.accessToken,
@"expirationDate": sinaweibo.expirationDate,
@"userId": sinaweibo.userId,
@"refreshToken": sinaweibo.refreshToken
};
[[NSUserDefaults standardUserDefaults] setObject:authData forKey:@"SinaWeiboAuthData"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"登入成功,Token 已持久化");
}
// 處理 URL Scheme 回調 (iOS 9+)
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
return [_sinaWeibo handleOpenURL:url delegate:_sinaWeibo];
}
// 兼容 iOS 8-
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [_sinaWeibo handleOpenURL:url delegate:_sinaWeibo];
}
// 獲取單例
- (SinaWeibo *)sinaWeibo {
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
return delegate.sinaWeibo;
}
// 觸發登入 (按鈕點擊事件)
- (IBAction)loginBtnClicked:(id)sender {
[self.sinaWeibo authorize:[WBAuthorizeRequest requestForAuthorize] withCompletionHandler:^(WBBaseResponse *response) {
if (response.statusCode == WBResponseStatusCodeSuccess) {
// 登入成功,UI 更新或跳轉
NSLog(@"授權成功");
} else {
NSLog(@"授權失敗: %@", response.errorMessage);
}
}];
}
// 登出功能
- (IBAction)logoutBtnClicked:(id)sender {
// 清除本地 Token
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"SinaWeiboAuthData"];
[[NSUserDefaults standardUserDefaults] synchronize];
// SDK 登出 (可選)
[self.sinaWeibo authorize:[WBAuthorizeRequest requestForAuthorize] withCompletionHandler:nil];
// 注意:SDK 可能無直接 logout 方法,通常僅清本地 Token 並重置 sinaWeibo 實例屬性即可視為登出
self.sinaWeibo.accessToken = nil;
self.sinaWeibo.userId = nil;
self.sinaWeibo.expirationDate = nil;
NSLog(@"已登出");
}
NSAppTransportSecurity 允許 HTTP。WBHttpRequest, WBAuthorizeRequest 等,請參考官方 Demo 調整 API 呼叫方式。不依賴第三方封裝庫,直接整合官方 SDK 是最穩健、可控性最高的方案。核心在於 AppDelegate 統一管理 SinaWeibo 單例生命週期與 Token 持久化,配合 URL Schemes 處理回調,即可實現完整的授權登入流程。