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
- Acaccountstore *account = [[Acaccountstore alloc] init]; ①
- Acaccounttype *accounttype = [Account accounttypewithaccounttypeidentifier:
- Acaccounttypeidentifiersinaweibo]; Ii
- [Account Requestaccesstoaccountswithtype:accounttype Options:nil
- completion:^ (BOOL Granted, Nserror *error) ③
- {
- if (granted = = YES) ④
- {
- Nsarray *arrayofaccounts = [Account
- Accountswithaccounttype:accounttype]; ⑤
- if ([arrayofaccounts count] > 0) ⑥
- {
- < certification through >
- }
- };
- }];
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
- RequestForServiceType:requestMethod:URL:parameters: The following is the code that creates the Slrequest object:
- Slrequest *request = [Slrequest Requestforservicetype:slservicetypesinaweibo
- Requestmethod:slrequestmethodget
- Url:requesturl
- Parameters: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 invoking the Performrequestwithhandler: Method of the Slrequest, which has the following code:
- [Request performrequestwithhandler:^ (NSData *responsedata,
- Nshttpurlresponse *urlresponse, Nserror *error) {
- < processing request Results >
- }];
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
- ID jsonobj = [nsjsonserialization jsonobjectwithdata:responsedata
- 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