Example code for implementing the WeChat authorized logon function in IOS

Source: Internet
Author: User
It is a platform that is often used in development, such as logon, authorization, payment, and sharing. Today, let's take a look at how to integrate authorization into your own applications. For more information, see a platform that is frequently used in development, such as login, authorization, payment, and sharing. Let's take a look at how to integrate authorization into your own applications. For more information, see.

1. authorization definition

OAuth2.0 authorized logon allows users to log on to a third-party application or website using their identities. After authorized users log on to a third-party application already connected to OAuth2.0, the third party can obtain the user's interface call credential (access_token ), access_token can be used to call the open platform authorization link interface, so as to obtain basic user open information and help users implement basic open functions.

2. authorization steps

A third party initiates an authorized login request. after you authorize a third-party application, the application is pulled or redirected to a third-party website, and the authorization temporary ticket code parameter is included;

Add AppID and AppSecret to the code parameter, and exchange access_token through the API;

Use access_token to call the interface to obtain basic user data resources or help users perform basic operations.

3. preparations

We need to prepare for authorized logon.

You can directly view the Developer Documentation. The address is as follows: iOS access guide.

In addition to the points mentioned in the developer documentation, there are several points to note:

3.1 modify the plist file as follows:

 
  NSAppTransportSecurity
 
  
  
   NSAllowsArbitraryLoads
   
  
 

This allows HTTP transmission.

4. authorization Step 1: Get the code

Assume that there is a button on the interface, and the user clicks this button to initiate the authorization operation.

The code is as follows:


-(IBAction) sendAuthRequest {// Construct the SendAuthReq struct SendAuthReq * req = [[[SendAuthReq alloc] init] autorelease]; req. scope = @ "snsapi_userinfo"; req. state = WXPacket_State; // used in OnResp to determine which application initiated the authorization, the message entered here will be returned in OnResp // A SendAuthReq message structure will be sent to the terminal in the third direction [WXApi sendReq: req];}

4.1 parameter description

// Proxy method-(void) onResp :( BaseResp *) resp {SendAuthResp * aresp = (SendAuthResp *) resp; if (aresp. errCode = 0 & [aresp. state isEqualToString: WXPacket_State]) {NSString * code = aresp. code; [self getWeiXinOpenId: code] ;}// obtain access_token, openid, unionid-(void) getWeiXinOpenId :( NSString *) code {NSString * url = [NSString stringWithFormat: @" https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code ", AppId, AppSerect, code]; dispatch_async (random (Random, 0), ^ {NSURL * zoneUrl = [NSURL URLWithString: url]; NSString * zoneStr = [NSString stringWithContentsOfURL: zoneUrl encoding: NSUTF8StringEncoding error: nil]; NSData * data = [zoneStr dataUsingEncoding: NSUTF8StringEncoding]; dispatch_async (Callback (), ^ {if (data) {NSDictionary * dic = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: nil]; NSString * openID = dic [@ "openid"]; NSString * unionid = dic [@ "unionid"] ;}}) ;});}

5.1 Return instructions:

The above is the details of the instance code for implementing the authorized login function in IOS. For more information, see other articles on php Chinese network!

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.