Use ios api to write the whole process of a simple Weibo Client

Source: Internet
Author: User

There are four main steps to compile a social network client program.


Next we will introduce the process as follows:

1. Introduce the accounts and social frameworks

The accounts and social frameworks need to be introduced in the process. The accounts framework has classes required for user account authentication, and the slrequest class in the social framework is what we need. To add a framework or library, select targets> Weibo> build phases> link binary with libraries in the project, and click "+" in the lower right corner.

Select social. Framework and add accounts. Framework respectively.


2. User Account Authentication

User account authentication uses the acaccount, acaccountstore, and acaccounttype classes. The acaccount class encapsulates user account information, which is stored in the account database. The acaccountstore class is used to manage account databases, the acaccounttype class describes the Account type.

The template code for the authentication process is as follows:

Acaccountstore * account = [[acaccountstore alloc] init]; ① acaccounttype * accounttype = [Account accounttypewithaccounttypeidentifier: role]; ② [account types: accounttype options: nilcompletion: ^ (bool granted, nserror * error) ③ {If (granted = Yes) ④ {nsarray * arrayofaccounts = [accountaccountswithaccounttype: accounttype]; ⑤ if ([arrayofaccounts count]> 0) ⑥ {<authentication passed >};}];



3. Send a request

After authentication, the user can send a request using the slrequest object. To create an slrequest object, use the class-level constructor requestforservicetype: requestmethod: URL: parameters:. The following Code creates the slrequest object:

Slrequest * request = [slrequest requestforservicetype: slservicetypesinaweibo orequestmethod: slrequestmethodgeturl: requesturlparameters: parameters]; The above Code only creates an slrequest object. We also need to set account information for the request object, use the following statement: request. account = weiboaccount; weiboaccount account information is obtained from the user account information database. It is set to the account attribute of the request object before it can be submitted to the social network server for authentication. The specific request is implemented by calling the receivmrequestwithhandler method of slrequest. The Code is as follows: [request processing mrequestwithhandler: ^ (nsdata * responsedata, nshttpurlresponse * urlresponse, nserror * error) {<request processing result >}];



4. Process request results

When the request ends, the code block is called. We process the request results in the code block. The basic task is to parse data and update the UI. The three social network services return JSON data. The responsedata parameter in the code block can use nsjsonserialization to parse the JSON object:

id jsonObj = [NSJSONSerialization JSONObjectWithData:responseDataoptions:NSJSONReadingAllowFragments error:&err];



The parsed jsonobj object structure varies with social network services. For detailed parameter information, see the developer websites of different services.

In the following example, we will introduce the use of slrequest. In the table view, we can pull down and refresh the view to obtain the latest social network service information. Click the action button in the navigation bar of the screen to display the mode view for writing information (shown in the right figure). After the writing is complete, click "save" to send information. You can click "cancel" to cancel sending.

From IOS network programming and cloud application best practices: Guan Dongsheng @ Tony _ Guan Dongsheng

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.