Using iOS API to write the whole process of a simple Weibo client, iosapi

Source: Internet
Author: User

Using iOS API to write the whole process of a simple Weibo client, iosapi

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:

Java code
  • ACAccountStore * account = [[ACAccountStore alloc] init]; ①
  • ACAccountType * accountType = [account accountTypeWithAccountTypeIdentifier:
  • ACAccountTypeIdentifierSinaWeibo]; ②
  • [Account requestAccessToAccountsWithType: accountType options: nil
  • Completion: ^ (BOOL granted, NSError * error) ③
  • {
  • If (granted = YES) ④
  • {
  • NSArray * arrayOfAccounts = [account
  • AccountsWithAccountType: accountType]; ⑤
  • If ([arrayOfAccounts count]> 0) ⑥
  • {
  • <Certified>
  • }
  • };
  • }];
  • 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. The class-level constructor can be used to create an SLRequest object.

    Java code
  • RequestForServiceType: requestMethod: URL: parameters:. The following Code creates an SLRequest object:
  • SLRequest * request = [SLRequest requestForServiceType: SLServiceTypeSinaWeibo
  • RequestMethod: SLRequestMethodGET
  • URL: requestURL
  • Parameters: parameters];
  • The above Code only creates an SLRequest object. We also need to set account information for the request object and 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 being submitted to the social network server for authentication.
  • The specific start request is implemented by calling the callback mrequestwithhandler of SLRequest. The Code is as follows:
  • [Request processing mrequestwithhandler: ^ (NSData * responseData,
  • NSHTTPURLResponse * urlResponse, NSError * error ){
  • <Request processing result>
  • }];
  • RequestForServiceType: requestMethod: URL: parameters: the following code creates an SLRequest object: SLRequest * request = [SLRequest requestForServiceType: parameters: SLRequestMethodGETURL: requestURLparameters]; the above Code only creates an SLRequest object. We also need to set account information for the request object and 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:

    Java code
  • Id jsonObj = [NSJSONSerialization JSONObjectWithData: responseData
  • Options: NSJSONReadingAllowFragments error: & err];
  • 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.

     

    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.