IOS Sina Weibo, Tencent Weibo share function Example _ios

Source: Internet
Author: User
Tags auth file url

One is Sina Weibo, Tencent Weibo's share button, one is their binding (in fact, is the authorization). Click on the microblogging share Sina or Tencent button, the corresponding authorization (if not authorized), display micro-blog content, and then publish Weibo. Setting the bindings in the interface is the relevant application authorization. Oh, actually quite simple drop.

First of all, from the Sina Weibo open platform (http://open.weibo.com/), Tencent Weibo open platform (http://dev.t.qq.com/) registered applications, access to Appkey,appsecret and AppUrl (which

AppUrl is to be filled out by himself).


Then download the relevant SDK separately.

Http://wiki.open.t.qq.com/index.PHP/SDK%E4%B8%8B%E8%BD%BD#iOS_SDK


Http://open.weibo.com/wiki/SDK


Oh, these are some preparatory work. Formally developed below.

Create a project, name Sinaqqbo, and add the relevant SDK files. Total 5 files: Sina:libweibosdk.a,weibosdk.bundle and WeiboSDK.h qq:libtcweibosdk.a WeiboApi.h


Then set the Info.plist file URL types key value, which is the role of Sina client or Tencent client can callback to our program. They schemes the key value via a bundle ID and the URL set here will correspond to our app's AppDelete (BOOL) Application: (UIApplication *) application OpenURL: ( Nsurl *) URL sourceapplication: (NSString *) sourceapplication annotation: (id) annotation delegate function.


The key value for the URL schemes is WB + Appkey. Tencent and Sina Two will establish two values.
The above is the project setting.
The following specific code

AppDelegate.h #import "WeiboSDK.h" #import "WeiboApi.h" @interface appdelegate:uiresponder <uiapplicationdelegat
 e,weibosdkdelegate,wbhttprequestdelegate> {BOOL bsinawb;
NSString *sinaaccesstoken; } @property (strong,nonatomic) Weiboapi *qqwbappdelete; The processing of Tencent Weibo. Make a global variable////////////////////////////////////////////APPDELEGATE.M #define SINAAPPKEY @ "yyyyyyyy" #define Sina Appsecret @ "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]; //Sina, Tencent client Invoke interface-(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 [Weibosdkhand
 Leopenurl:url Delegate:self];
 else {return [_qqwbappdeletehandleopenurl:url]; The following deals with Sina's related-(void) Didreceiveweiborequest: (Wbbaserequest *) Request {}-(void) Didreceiveweiboresponse: ( Wbbaseresponse *) Response {if ([ResponseisKindOfClass:WBSendMessageToWeiboResponse.class]) {switch (response.status
  Code) {caseweibosdkresponsestatuscodesuccess: {//success} break; } elseif ([response IsKindOfClass:WBAuthorizeResponse.class]) {if (0 = response.statuscode) {//Grant succeeded} Els 
 e {//Authorization failed}}-(void) Recvnotificationdata: (nsnotification *) Notification {nsstring *sname = notification.name; if ([sname isequaltostring:@ "Auth"]) {//sina Authorization request--"Sina Weibo client and Web version General Command * *
   Sina Weibo client, is SSO, through Sina Weibo client licensing and publishing microblogging content page version, is the combination of command sent directly to the Sina microblogging service side.
  * * Wbauthorizerequest *request = [Wbauthorizerequestrequest];
  Request.redirecturi = Sinaappurl;
  Request.scope = @ "All";
 [Weibosdk Sendrequest:request]; else if ([snameisequaltostring:@ "Data"]) {if ([weibosdkisweiboappinstalled]) {//Installed Sina Weibo client Wbmessageobject
   *message = [Wbmessageobjectmessage];
   Message.Text = @ "text";
   Wbimageobject *image = [Wbimageobjectobject];
   Image.imagedata = @ "picture";//jpeg format picture, for nsdata form message.imageobject = image;
   Wbsendmessagetoweiborequest *request = [Wbsendmessagetoweiborequestrequestwithmessage:message];
  [Weibosdk Sendrequest:request];
   else {BOOL btext = YES;
    if (btext) {nsmutabledictionary* parameters = [Nsmutabledictionarydictionary];
    [Parameters setobject:@ "text" 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:@ "text" forkey:@ "status"]; [Parameters setobject:@ "picture" forkey:@ "pic"]; JPEG nsdata format, [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 {//Publish

 Failed}-(void) Request: (Wbhttprequest *) Request Didfinishloadingwithresult: (NSString *) result {//Publish success}

Tencent's implementation of the way and Sina is not the same. The following is the way Tencent calls

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) {//Sina [selfsendauthwithtype:0];
 else {//Tencent [Selfsendauthwithtype:1];
   }-(void) Sendauthwithtype: (Nsinteger) Aindex {if (0 = = Aindex) {if ([self isauthvalid]) {//Show content interface} else {
  [[Nsnotificationcenterdefaultcenter] postnotificationname:@ "Auth" object:nil]; } else {if ([self IsAUthvalid]) {//Show content interface} else {[_QQWB loginWithDelegate:selfandRootController:self]; }}-(void) Senddatawithpic: (NSString *) stext imagedata: (NSData *) aimagedata {nsmutabledictionary* parameters = [NSM
 Utabledictionarydictionary];
 [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" Del
Egate: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" Delegat
E:self]; #pragma mark Weiboauthdelegate-(void) didauthfinished: (WEIBOAPI *) Wbapi {//authorized successfully, after display content interface} #pragma mark Weiborequest Delegate- (void) Didreceiverawdata: (NSData *) data reqno: (int) reqno {//Publish succeeded}-(void) Didfailwitherror: (Nserror *) error reqno: (int
reqno {///Post failed}//Below is the authorization validation function for processing Sina, which is not written by 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]);

 }

The following is the online resources
Sina:
Http://www.jb51.net/article/98044.htm

Tencent:
Https://github.com/heloyue/TCWeiboSDK-LightVersion

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.