Use the iOS API to write a simple Weibo client-wide process

Source: Internet
Author: User

To write a social networking client program, you can broadly divide it into 4 main steps

Below we follow this process, introduce:

1. Introduction of accounts and social framework

The accounts and social frameworks need to be introduced in the project, and the accounts framework has the required classes for user account authentication, and the Slrequest class in the social framework is what we need. To add a specific step is to select Targets→weibo→build phases→link Binary with Libraries in the project, select the "+" button in the lower right corner to open the Frame and Library selection dialog box.

Select Social.framework to add, and then select Accounts.framework Add.

2. User account Authentication

User account authentication using the Acaccount, Acaccountstore, and Acaccounttype classes, the Acaccount class is the encapsulation of user account information, which is stored in the account database, and the Acaccountstore class is used to manage the account database. The Acaccounttype class describes the account type.

The template code for the authentication process is as follows:

Java code
  1. Acaccountstore *account = [[Acaccountstore alloc] init]; ①
  2. Acaccounttype *accounttype = [Account accounttypewithaccounttypeidentifier:
  3. Acaccounttypeidentifiersinaweibo]; Ii
  4. [Account Requestaccesstoaccountswithtype:accounttype Options:nil
  5. completion:^ (BOOL Granted, Nserror *error) ③
  6. {
  7. if (granted = = YES) ④
  8. {
  9. Nsarray *arrayofaccounts = [Account
  10. Accountswithaccounttype:accounttype]; ⑤
  11. if ([arrayofaccounts count] > 0) ⑥
  12. {
  13. < certification through >
  14. }
  15. };
  16. }];
Acaccountstore *account = [[Acaccountstore alloc] init]; ①acaccounttype *accounttype = [Account Accounttypewithaccounttypeidentifier:acaccounttypeidentifiersinaweibo]; ②[account Requestaccesstoaccountswithtype:accounttype options:nilcompletion:^ (BOOL Granted, NSError *error) ③{if ( granted = = YES) ④{nsarray *arrayofaccounts = [Accountaccountswithaccounttype:accounttype];⑤if ([ArrayOfAccounts Count] > 0) ⑥{< certification through >}};}];

3. Send Request

User authentication can be sent by sending requests using Slrequest objects, creating Slrequest objects can use class-level construction methods

Java code
  1. RequestForServiceType:requestMethod:URL:parameters: The following is the code that creates the Slrequest object:
  2. Slrequest *request = [Slrequest Requestforservicetype:slservicetypesinaweibo
  3. Requestmethod:slrequestmethodget
  4. Url:requesturl
  5. Parameters:parameters];
  6. The above code just creates the Slrequest object, and we also need to set the account information for the request object, using the following statement:
  7. Request.account = Weiboaccount;
  8. Weiboaccount account information is what we obtain from the user account information database, set to the client property of the requested object, before it can be submitted to the social network server for authentication.
  9. The specific start request is implemented by invoking the Performrequestwithhandler: Method of the Slrequest, which has the following code:
  10. [Request performrequestwithhandler:^ (NSData *responsedata,
  11. Nshttpurlresponse *urlresponse, Nserror *error) {
  12. < processing request Results >
  13. }];
RequestForServiceType:requestMethod:URL:parameters:, here is the code to create the Slrequest object: slrequest *request = [Slrequest  RequestForServiceType:SLServiceTypeSinaWeiborequestMethod:SLRequestMethodGETURL:requestURLparameters: Parameters]; The above code just creates the Slrequest object, and we also need to set the account information for the request object, using the following statement: Request.account = Weiboaccount; Weiboaccount account information is what we obtain from the user account information database, set to the client property of the requested object, before it can be submitted to the social network server for authentication. The specific start request is implemented by calling Slrequest's Performrequestwithhandler: method, which reads the following code: [Request performrequestwithhandler:^ (NSData * Responsedata,nshttpurlresponse *urlresponse, Nserror *error) {< processing request Results >}];

4. Processing Request Results

The end of the request calls the code block, and we process the request result in the code block. The basic work is to parse the data, as well as updates to the UI. These 3 social network services return JSON-formatted data, where the ResponseData parameter in a code block resolves a JSON object using Nsjsonserialization:

Java code
    1. ID jsonobj = [nsjsonserialization jsonobjectwithdata:responsedata
    2. Options:nsjsonreadingallowfragments error:&err];
ID jsonobj = [nsjsonserialization JSONObjectWithData:responseDataoptions:NSJSONReadingAllowFragments error:&err ];

The Jsonobj object structure of the parsing is different depending on the social network Service, please refer to the developer website of different service for detailed parameters.

Here we introduce an example of the use of slrequest, in the Table view screen, you can pull down to refresh the view to get the latest social network service information. Click on the Action button on the screen navigation bar to pop up the modal view (shown on the right) to compose the message, then click "Save" button to send the message and click "Cancel" button to cancel the send.

Use the iOS API to write a simple Weibo client-wide process

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.