iOS5.0 above using Sina Weibo open platform OAuth (and resolve login invalid issue)

Source: Internet
Author: User
Tags oauth what interface

Sina Weibo's open platform provides a simple mode of collaboration for third-party applications to meet the needs of mobile users and tablet users to share information anytime, anywhere. Many of the features on Weibo can be achieved by invoking the platform's API.

The main purpose of this article is to record the embedding and use of the Sina Weibo Mobile SDK iOS version under IOS5.

1, apply for a Sina Weibo mobile app.

Application address: Http://open.weibo.com/development, get app key and app Secret after application

2. Download IOS_SDK

: Http://open.weibo.com/wiki/SDK#iOS_SDK, download the first one is OK.

3. Create a new project Sina_weibo, select the single View app. and uses the ARC feature after 5.0. Import the extracted SDK

Import SDK

4. The Adaptation SDK runs in the ARC environment

Running the program at this time, you will find a lot of errors about arc, because the SDK is not using arc. At this time if you want the SDK files do not participate in the ARC mode of compilation, then you need to do the following settings, in build phases Add "-fno-objc-arc" Mark

Double-click the file you want to identify and enter-fno-objc-arc.

This way the Weibo SDK files will not be compiled in arc.

5, in their own projects to increase the security.framework. The SDK needs to use Security.framework to put the OAuth-certified tokens into keychain to increase the security of the entire project.

At this point, the program compiles and runs normally.

6, the other and the SDK in the same as the demo

Login Call

[weiboenginelogin];

Logoff Call

[weiboenginelogout];

Send Weibo:

You can call the SDK default interface to send:

Wbsendview *sendview = [[wbsendviewalloc] initwithappkey:appkeyappsecret: appsecrettext:@ "Test"image: [UIImageimagenamed: @ "Bg.png" ]];

[Sendview setdelegate:Self];

[Sendview show:YES];

The corresponding API for sending the Weibo is: statuses/upload send Weibo and upload images. If the map is displayed on Weibo, send latitude and longitude parameters, plus

Lat False Float Latitude, valid range: 90.0 to +90.0,+ for latitude, default to 0.0.
Long False Float Longitude, valid range: 180.0 to +180.0,+ represents longitude, default is 0.0.
7. Calling the Custom API

6 step is called in the SDK package is good, that microblogging so API and function, how to call it?

We're trying to get personal information.

[CPP]View Plaincopy
  1. Nsmutabledictionary *params = [Nsmutabledictionary dictionarywithcapacity:2];
  2. [Params setobject:[engine accesstoken]forkey:@"Access_token"];
  3. [Params setobject:[engine userid]forkey:@"UID"];
  4. NSLog (@"params:%@", params);
  5. [Engine loadrequestwithmethodname:@"Users/show.json"
  6. httpmethod:@"GET"
  7. Params:params
  8. Postdatatype:kwbrequestpostdatatypenone
  9. Httpheaderfields:nil];

The params parameter is required.

The returned data reference interface Http://open.weibo.com/wiki/2/users/show

This gives you access to information such as your Twitter nickname.

All API documentation for Weibo is http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2 on this page, using both methods and examples.

What needs to use what interface, the loadrequestwithmethodname to change into their own needs of the interface, the params parameter changed to the required parameters, it is possible.

Some interfaces do not require a params, such as

Statuses/friends_timeline.json get attention to people's Weibo, where the params can be nil.

PS: This record uses OAuth authentication, XAUTH certification needs to audit eligibility to use.

8, project Source: http://download.csdn.net/detail/totogo2010/4633077

Follow the iOS learning iOS5.0 above using the Sina Weibo open platform OAuth

After that, Sina Weibo authorized pop-up pages have been adjusted, the middle of the paralysis. This is true if you press the authorization page that you made in the previous article:

The first: The page is getting bigger,

Second: no Cancel button.

According to this situation, write adjustments are made in the Sina Weibo SDK.

Adjust: Add a Close button, pop-up window size.

In the wbauthorizewebview.m file method: bounceoutanimationstopped Add a button:

[CPP]View Plaincopy
  1. UIButton *closebutton = [UIButton buttonwithtype:uibuttontypecustom];
  2. [CloseButton Setframe:cgrectmake (280, 430, 60, 60)];
  3. [CloseButton setimageedgeinsets:uiedgeinsetsmake (3, 0, 0, 0)];
  4. [CloseButton setimage:[uiimage imagenamed:@"Close"] forstate:uicontrolstatenormal];
  5. [CloseButton addtarget:self Action: @selector (Hideandcleanup) forcontrolevents:uicontroleventtouchupinside];
  6. [Self Addsubview:closebutton];


Close.png Picture SDK comes with it. The Hideandcleanup method is to remove the window. The Hideandcleanup method was originally there. Operating effect:

Look at the lower right corner has a close button, why put in the lower right corner, because there is a registration button in the upper right corner, easy to be point to. You can see it by maximizing the page window.

Enlarge window

Method in wbauthorizewebview.m file-(void) Sizetofitorientation: (uiinterfaceorientation) orientation Modify the following:

The size of the above is horizontal screen, I modified the vertical screen when the size of the window.

[CPP]View Plaincopy
  1. -(void) sizetofitorientation: (uiinterfaceorientation) Orientation
  2. {
  3. [Self settransform:cgaffinetransformidentity];
  4. if (Uiinterfaceorientationislandscape (orientation))
  5. {
  6. [Self setframe:cgrectmake (0, 0, 480, 320)];
  7. [Panelview Setframe:cgrectmake (10, 30, 460, 280)];
  8. [Containerview Setframe:cgrectmake (10, 10, 440, 260)];
  9. [WebView setframe:cgrectmake (0, 0, 440, 260)];
  10. [Indicatorview Setcenter:cgpointmake (240, 160)];
  11. }
  12. Else   
  13. {
  14. [Self setframe:cgrectmake (0, 5, 320, 470)];
  15. [Panelview setframe:cgrectmake (0, 5, 320, 470)];
  16. [Containerview setframe:cgrectmake (0, 5, 320, 460)];
  17. [WebView setframe:cgrectmake (0, 0, 320, 460)];
  18. [Indicatorview Setcenter:cgpointmake (160, 240)];
  19. }
  20. [Self setcenter:cgpointmake (160, 240)];
  21. [Self settransform:[self transformfororientation:orientation]];
  22. Previousorientation = orientation;
  23. }

Operating effect:

This state is almost ready.

There is also a case where the logout of the call Weiboengine is invalid. Modify the following:

In the WBAUTHORIZE.M file, modify the Startauthorize function as follows:

[CPP]View Plaincopy
  1. nsdictionary *params = [Nsdictionary dictionarywithobjectsandkeys:appkey, @"client_id",
  2.                                                                          @< Span class= "string" > "code" ,  @ ,   
  3.                                                                           redirecturi, @ "Redirect_uri" ,    
  4.                                                                          @< Span class= "string", "mobile" ,  @ ,   
  5. @ "true", @"Forcelogin", Nil];


is to add @ "true" @ "Forcelogin" in the params.

The above is the use of Sina Weibo SDK development encountered problems and some of the methods to solve.

Modified Project code: http://download.csdn.net/detail/totogo2010/4928029

Source: http://blog.csdn.net/totogo2010/article/details/8435174

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.