Open Platform: Sina Weibo for iOS

Source: Internet
Author: User

Introduction:

Sina Weibo has opened up almost all APIs for data on the platform. Therefore, many excellent third-party Weibo clients provide comprehensive functions.

The SNS sharing and promotion methods have been widely used in the app world, and even can be seen everywhere. This article mainly introduces how the app is associated with Sina Weibo.

References:

1. Development Platform homepage:

Http://open.weibo.com /? Bottomnav = 1 & WVR = 5

2. API document homepage:

Http://open.weibo.com/wiki/api文 _v2

3. API error code description address:

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

4. iOS SDK address:

Https://github.com/mobileresearch/weibo_ios_sdk_sso-oauth

5. Authorization Mechanism:

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

6. developer Management Center

Http://open.weibo.com/apps

Usage:

After you create your own application in the management center, you will getAppkeyAndAPP secret

These two values are the two parameters required to initialize the Sina Weibo SDK.

When executedLoginThe following error may occur during function execution:

1: Access Error prompt

This parameter is set during Weibo SDK initialization.AppredirecturiAnd Weibo Open Platform-developer Management Center-application information-advanced information-oauth2.0 authorization settings-Authorization callback page

If the value is different, the above error occurs.

2: The app is not normally returned after the Sina Weibo client is called for authorization.

1: CheckURL type"URL Scheme"Whether the name is set:Sinaweibosso.Add appid
:

2:AppdelegateWhether to implement the delegate function in:

-(Bool) Application :( uiapplication *) Application Openurl :( nsurl *) URL sourceapplication :( nsstring *) sourceapplication annotation :( ID) annotation {nslog (@ "% @", URL. scheme); // if other applications are involved in interaction, make the following judgment. For example, if ([url. scheme isequaltostring: key_weixinappid]) {return [wxapi handleopenurl: URL delegate: Self];} else if ([url. scheme isequaltostring: [@ "sinaweibosso" stringbyappendingpathextension: key_sinaweiboappid]) {return [[sinaweibomanage defaultinstance]. sinaweibo o handleopenurl: url] ;}else {return YES ;}}

After the preceding settings are complete, the Authorization result is returned.

Next, we will call APIs to perform Weibo data interaction.

For example, how to obtain the personal information of authorized users:

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];[params setObject:userId forKey:@"uid"];[sinaWeibo requestWithURL:@"users/show.json"                   params:params               httpMethod:@"GET"                 delegate:self];

For more information about the parameters, see the official API documentation.

After the result is obtained, the successful or failed delegate will be returned:

In this case, the Link name can be used to identify the request type:

-(Void) Request :( sinaweibo orequest *) Request didfailwitherror :( nserror *) error {// retrieve the following list if ([request. URL hassuffix: @ "friendships/friends. JSON "]) {If ([Delegate respondstoselector: @ selector (usage: Using: withrequestdatatype: issuccess :)]) {[Delegate sinaweibomanage: Self usage: Nil withrequestdatatype: Self. requestdatatype issuccess: No] ;}}

About the built-in Weibo function in iOS 6:

In iOS 6, Apple integrates the social environment of Sina Weibo. Therefore, if you authorize Sina Weibo accounts on the settings page, you can directly use the Sina Weibo account in third-party applications.

First, introduce two newFramework

They are:

Accounts. Framework: Used to obtain account information in system settings

Social. Framework: Used for data interaction with third-party open platforms.

The process is divided into two steps:

First, you must know whether the user has authorized the corresponding account in the system settings,

If you get the corresponding account information, you can start data interaction with the third-party open platform. The Code is as follows:

// Create an account store object. create an account set acaccountstore * accountstore = [[acaccountstore alloc] init]; // create an account type that ensures Twitter accounts are retrieved. determine whether the account type is Sina Weibo or Facebook or twitteracaccounttype * accounttype = [accountstore accounttypewithaccounttypeidentifier: acaccounttypeidentifiersinaweibo]; // request access from the user to use their Twitter accounts. // obtain the account information of the corresponding type through an asynchronous request [accountstore requestaccesstoaccountswithtype: accounttype withcompletionhandler: ^ (bool granted, nserror * error) {If (granted) {// if granted returns no, it indicates that no account has been set. // get the list of Twitter accounts. nsarray * accountsarray = [accountstore accountswithaccounttype, we'll assume there is only one Twitter account present. // You wocould ideally ask the user which account they want to tweet from, if there is more than one Twitter account present. if ([accountsarray count]> 0) {// grab the initial Twitter account to tweet from. acaccount * sinaweiboaccount = [accountsarray objectatindex: 0]; nsmutabledictionary * Params = [[nsmutabledictionary alloc] init]; [Params setobject: @ "A New microblog" forkey: @ "status"]; slrequest * slrequest = [slrequest requestforservicetype: slservicetypesinaweibo o requestmethod: slrequestmethodpost URL: [nsurl urlwithstring: @ "https://open.weibo.cn/2/statuses/update.json"] parameters: Params]; slrequest. account = sinaweiboaccount; // This line of code must be assigned a value, and data interaction must fail. [slrequest implements mrequestwithhandler: ^ (nsdata * responsedata, nshttpurlresponse * urlresponse, nserror * error) {nslog (@ "% @", urlresponse. URL, error); nsdictionary * DIC = [nsjsonserialization jsonobjectwithdata: responsedata options: nsjsonreadingallowfragments error: Nil]; nslog (@ "% @", DIC) ;}] ;}}];

Each acaccount hasIdentifierWhen the user confirms which account to use, it is best to save it. Next time, the user can directly obtain the corresponding account through the following code.

[accountStore accountWithIdentifier:sinaWeiboAccount.identifier];

Summary:

As one of the main ways of application promotion, SNS must be well studied. How to hook up and arouse the user's desire to share, so that more people can know your application, that's not far from success.

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.