iOS新浪微博、騰訊微博分享功能實(shí)例
一個(gè)是新浪微博,騰訊微博的分享按鈕,一個(gè)是他們的綁定情況(其實(shí)就是是否授權(quán))。點(diǎn)擊微博分享中新浪或騰訊按鈕,就進(jìn)行相應(yīng)的授權(quán)(若沒授權(quán)),顯示微博內(nèi)容,而后發(fā)布微博。設(shè)置界面中的綁定,就是相關(guān)的應(yīng)用授權(quán)。 呵呵,其實(shí)也蠻簡單滴。
首先分別從新浪微博開放平臺(http://open.weibo.com/)、騰訊微博開放平臺(http://dev.t.qq.com/)中注冊應(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)到我們的程序來。他們通過一個(gè)bundle id 和這里設(shè)置的URL Schemes鍵值就會相應(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; //對騰訊微博的處理。做一個(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)請求--》新浪微博客戶端和網(wǎng)頁版通用的命令
/*
新浪微博客戶端,就是sso,通過新浪微博客戶端授權(quán)和發(fā)布微博內(nèi)容
網(wǎng)頁版,就是組合命令直接發(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.dhdzp.com/article/98044.htm
騰訊:
https://github.com/heloyue/TCWeiboSDK-LightVersion
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS 開發(fā)之網(wǎng)絡(luò)圖片輪播圖的實(shí)現(xiàn)
這篇文章主要介紹了IOS 開發(fā)之網(wǎng)絡(luò)圖片輪播圖的實(shí)現(xiàn)的相關(guān)資料,希望通過此文大家能夠掌握輪播圖的實(shí)現(xiàn),需要的朋友可以參考下2017-09-09
詳解iOS應(yīng)用的設(shè)計(jì)模式開發(fā)中Mediator中介者模式的使用
這篇文章主要介紹了iOS應(yīng)用的設(shè)計(jì)模式開發(fā)中Mediator中介者模式的使用,示例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-03-03
iOS實(shí)現(xiàn)UITableView左滑刪除復(fù)制即用功能
這篇文章主要介紹了iOS實(shí)現(xiàn)UITableView左滑刪除復(fù)制即用功能,在項(xiàng)目開發(fā)中經(jīng)常會用到這樣的需求,下面小編把實(shí)現(xiàn)代碼分享給大家,需要的朋友可以參考下2017-09-09
解析iOS開發(fā)中的FirstResponder第一響應(yīng)對象
這篇文章主要介紹了解析iOS開發(fā)中的FirstResponder第一響應(yīng)對象,包括View的FirstResponder的釋放問題,需要的朋友可以參考下2015-10-10
iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用
這篇文章主要介紹了iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用的方法,實(shí)例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-03-03
快速解決低版本Xcode不支持高版本iOS真機(jī)調(diào)試的問題方法
這篇文章主要介紹了快速解決低版本Xcode不支持高版本iOS真機(jī)調(diào)試的問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
IOS 陀螺儀開發(fā)(CoreMotion框架)實(shí)例詳解
這篇文章主要介紹了IOS 陀螺儀開發(fā)實(shí)例詳解的相關(guān)資料,介紹了螺旋儀參數(shù)意義及CoreMotion框架,需要的朋友可以參考下2016-10-10

