iOS新浪微博、騰訊微博分享功能實(shí)例
一個(gè)是新浪微博,騰訊微博的分享按鈕,一個(gè)是他們的綁定情況(其實(shí)就是是否授權(quán))。點(diǎn)擊微博分享中新浪或騰訊按鈕,就進(jìn)行相應(yīng)的授權(quán)(若沒(méi)授權(quán)),顯示微博內(nèi)容,而后發(fā)布微博。設(shè)置界面中的綁定,就是相關(guān)的應(yīng)用授權(quán)。 呵呵,其實(shí)也蠻簡(jiǎn)單滴。
首先分別從新浪微博開放平臺(tái)(http://open.weibo.com/)、騰訊微博開放平臺(tái)(http://dev.t.qq.com/)中注冊(cè)應(yīng)用,獲取到Appkey,AppSecret和AppURL(其中
AppURL是要自己填寫的)。
然后分別下載相關(guān)的SDK.
http://wiki.open.t.qq.com/index.PHP/SDK%E4%B8%8B%E8%BD%BD#iOS_SDK
http://open.weibo.com/wiki/SDK
呵呵,上面這些都是些預(yù)備工作。下面正式開發(fā)。
建立一個(gè)工程,取名sinaqqbo,加入相關(guān)sdk文件。 總共5個(gè)文件:sina:libWeiboSDK.a,WeiboSDK.bundle 和WeiboSDK.h qq:libTCWeiboSDK.a WeiboApi.h
然后設(shè)置Info.plist文件的URL types鍵值,這個(gè)的作用是新浪客戶端或騰訊客戶端能回調(diào)到我們的程序來(lái)。他們通過(guò)一個(gè)bundle id 和這里設(shè)置的URL Schemes鍵值就會(huì)相應(yīng)我們app的AppDelete中的 (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation委托 函數(shù)。
URL Schemes 的鍵值為 wb + AppKey。 騰訊和新浪兩個(gè)就要建立兩個(gè)數(shù)值。
以上就是工程上設(shè)置。
下面具體代碼
// AppDelegate.h #import "WeiboSDK.h" #import "WeiboApi.h" @interface AppDelegate : UIResponder <UIApplicationDelegate,WeiboSDKDelegate,WBHttpRequestDelegate> { BOOL bSinaWB; NSString *sinaAccessToken; } @property (strong,nonatomic) WeiboApi *qqwbAppDelete; //對(duì)騰訊微博的處理。做一個(gè)全局的變量 ////////////////////////////////////////// // AppDelegate.m #define sinaAppKey @"yyyyyyyy" #define sinaAppSecret @"yyyyyyyyyyyyyyyyyyyyyyyyyy" #define sinaAppURL @"https://api.weibo.com/oauth2/default.html" #define qqAppKey @"xxxxxx" #define qqAppSecret @"xxxxxxxxxxxxxxxxxxxxxxxxx" #define qqAppURL @"https://api.weibo.com/oauth2/default.html" - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [WeiboSDK enableDebugMode:YES]; [WeiboSDK registerApp:sinaAppKey]; } //新浪,騰訊客戶端調(diào)用接口 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation { if (bSinaWB) { return [WeiboSDKhandleOpenURL:url delegate:self]; } else { return [_qqwbAppDeletehandleOpenURL:url]; } } - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { if (bSinaWB) { return [WeiboSDKhandleOpenURL:url delegate:self]; } else { return [_qqwbAppDeletehandleOpenURL:url] ; } } 以下處理sina的相關(guān) - (void)didReceiveWeiboRequest:(WBBaseRequest *)request { } - (void)didReceiveWeiboResponse:(WBBaseResponse *)response { if ([responseisKindOfClass:WBSendMessageToWeiboResponse.class]) { switch (response.statusCode) { caseWeiboSDKResponseStatusCodeSuccess: {//成功 } break; } } elseif ([response isKindOfClass:WBAuthorizeResponse.class]) { if (0 == response.statusCode) { //授權(quán)成功 } else { //授權(quán)失敗 } } } - (void)recvNotificationData:(NSNotification *)notification { NSString *sName = notification.name; if ([sName isEqualToString:@"Auth"]) { //sina 的授權(quán)請(qǐng)求--》新浪微博客戶端和網(wǎng)頁(yè)版通用的命令 /* 新浪微博客戶端,就是sso,通過(guò)新浪微博客戶端授權(quán)和發(fā)布微博內(nèi)容 網(wǎng)頁(yè)版,就是組合命令直接發(fā)送到新浪微博服務(wù)端。 */ WBAuthorizeRequest *request = [WBAuthorizeRequestrequest]; request.redirectURI = sinaAppURL; request.scope = @"all"; [WeiboSDK sendRequest:request]; } else if ([sNameisEqualToString:@"Data"]) { if ([WeiboSDKisWeiboAppInstalled]) { //安裝了新浪微博客戶端 WBMessageObject *message = [WBMessageObjectmessage]; message.text = @"文本"; WBImageObject *image = [WBImageObjectobject]; image.imageData = @"圖片";//jpeg格式的圖片,為NSData形式 message.imageObject = image; WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequestrequestWithMessage:message]; [WeiboSDK sendRequest:request]; } else { BOOL bText = YES; if (bText) { NSMutableDictionary* parameters = [NSMutableDictionarydictionary]; [parameters setObject:@"文本"forKey:@"status"]; [WBHttpRequestrequestWithAccessToken:sinaAccessTokenurl:[NSStringstringWithFormat:@"%@",@"https://api.weibo.com/2/statuses/update.json"]httpMethod:@"POST"params:parametersdelegate:selfwithTag:@"1"]; } else { NSMutableDictionary* parameters = [NSMutableDictionarydictionary]; [parameters setObject:@"文本"forKey:@"status"]; [parameters setObject:@"圖片"forKey:@"pic"]; //jpeg的 NSData 格式, [WBHttpRequestrequestWithAccessToken:sinaAccessTokenurl:[NSStringstringWithFormat:@"%@",@"https://api.weibo.com/2/statuses/upload.json"]httpMethod:@"POST"params:parametersdelegate:selfwithTag:[NSStringstringWithFormat:@"1"]]; } } } else if ([sNameisEqualToString:@"LogOut"]) { [WeiboSDK logOutWithToken:sinaAccessTokendelegate:selfwithTag:@"1"]; } } #pragma Mark WBHttpRequestDelegate - (void)request:(WBHttpRequest *)request didFailWithError:(NSError *)error { //發(fā)布失敗 } - (void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result { //發(fā)布成功 }
騰訊的實(shí)現(xiàn)方式和新浪的不一樣。下面是騰訊的調(diào)用方式
// MyViewController.h #import "WeiboApi.h" @interface MyViewController :UIViewController<WeiboAuthDelegate,WeiboRequestDelegate> { } @property(nonatomic,retain)WeiboApi *qqwb; @end // MyViewController.m #import "AppDelegate.h" - (void)viewDidLoad { [superviewDidLoad]; AppDelegate *delegate = (AppDelegate*)[UIApplicationsharedApplication].delegate; if (nil != delegate.qqwbAppDelete) { self.qqwb = delegate.qqwbAppDelete; } else { self.qqwb = [[WeiboApialloc]initWithAppKey:qqAppKeyandSecret:qqAppSecretandRedirectUri:qqAppURL]; delegate.qqwbAppDelete = _qqwb; } } - (void)buttonAction:(id)sender { UIButton *button = (UIButton *)sender; if (0 == button.tag) { //新浪 [selfsendAuthWithType:0]; } else { //騰訊 [selfsendAuthWithType:1]; } } - (void)sendAuthWithType:(NSInteger)aIndex { if (0 == aIndex) { if ([self isAuthValid]) { //顯示內(nèi)容界面 } else { [[NSNotificationCenterdefaultCenter] postNotificationName:@"Auth"object:nil]; } } else { if ([self isAuthValid]) { //顯示內(nèi)容界面 } else { [_qqwb loginWithDelegate:selfandRootController:self]; } } } - (void)sendDataWithPic:(NSString *)sText ImageData:(NSData *)aImageData { NSMutableDictionary* parameters = [NSMutableDictionarydictionary]; [parameters setObject:@"xml"forKey:@"format"]; [parameters setObject:sText forKey:@"content"]; [parameters setObject:aImageData forKey:@"pic"]; [_qqwb requestWithParams:parameters apiName:[NSStringstringWithFormat:@"%@",@"t/add_pic"] httpMethod:@"POST"delegate:self]; } - (void)sendText:(NSString *)sText { NSMutableDictionary* parameters = [NSMutableDictionarydictionary]; [parameters setObject:@"xml"forKey:@"format"]; [parameters setObject:sText forKey:@"content"]; [_qqwb requestWithParams:parameters apiName:[NSStringstringWithFormat:@"%@",@"t/add"] httpMethod:@"POST"delegate:self]; } #pragma Mark WeiboAuthDelegate - (void)DidAuthFinished:(WeiboApi *)wbapi { //授權(quán)成功,后顯示內(nèi)容界面 } #pragma Mark WeiboRequestDelegate - (void)didReceiveRawData:(NSData *)data reqNo:(int)reqno { //發(fā)布成功 } - (void)didFailWithError:(NSError *)error reqNo:(int)reqno { //發(fā)布失敗 } //以下是處理sina的授權(quán)驗(yàn)證函數(shù),qq的未寫。 - (void)removeAuthData { self.sinaid = nil; self.sinatoken =nil; self.sinadate =nil; } - (BOOL)isLoggedIn { returnsinaid && sinatoken && sinadate; } - (BOOL)isAuthorizeExpired { NSDate *now = [NSDatedate]; /*if (0 == bSina) {*/ return ([nowcompare:sinadate] ==NSOrderedDescending); //} /*else { AppDelegate *delegate = (AppDelegate*)[UIApplication sharedApplication].delegate; return [delegate.qqwbAppDelete isAuthorizeExpired]; }*/ } - (BOOL)isAuthValid { return ([selfisLoggedIn] && ![selfisAuthorizeExpired]); }
以下是網(wǎng)上資源
新浪:
http://www.dbjr.com.cn/article/98044.htm
騰訊:
https://github.com/heloyue/TCWeiboSDK-LightVersion
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS應(yīng)用開發(fā)中使用Auto Layout來(lái)適配不同屏幕尺寸
這篇文章主要介紹了iOS應(yīng)用開發(fā)中使用Auto Layout來(lái)適配不同屏幕尺寸的方法,根據(jù)Xcode IDE下的實(shí)際調(diào)試步驟講解其用法,需要的朋友可以參考下2016-03-03iOS實(shí)現(xiàn)視頻下載并自動(dòng)保存到相冊(cè)功能
這篇文章主要為大家詳細(xì)介紹了ios 視頻下載功能實(shí)現(xiàn),并自動(dòng)保存到相冊(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02iOS實(shí)現(xiàn)對(duì)不同分辨率設(shè)備的字號(hào)大小適配方法
下面小編就為大家分享一篇iOS實(shí)現(xiàn)對(duì)不同分辨率設(shè)備的字號(hào)大小適配方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01iOS如何利用一句話完成轉(zhuǎn)場(chǎng)動(dòng)畫
這篇文章主要給大家介紹了關(guān)于iOS如何利用一句話完成轉(zhuǎn)場(chǎng)動(dòng)畫的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08