IOS Micro-Credit third-party login instance _ios

Source: Internet
Author: User
Tags openid

This example for you to share the iOS micro-letter Third-party login for your reference, the specific contents are as follows

First, preparatory work
1, to micro-letter open platform to register as a developer, access to AppID
2, Import Wechatconnection.framework
3. Configure URL schemes input AppID for example WX29CE0F21EA982CB8

Second, configure APPDELEGATE.M

1, registered micro-letter

Micro-letter Landing 
[Wxapi registerapp:weixin_appid withdescription:@ "Weixin"]; 

2. Set function

Set Proxy to login view
-(BOOL) Application: (UIApplication *) application 
   Handleopenurl: (nsurl *) URL 
{ 
  return [Wxapi Handleopenurl:url Delegate:[loginviewcontroller Sharelogin]]; 
} 
-(BOOL) Application: (UIApplication *) application 
      OpenURL: (nsurl *) URL 
 sourceapplication: (NSString *) Sourceapplication 
     annotation: (id) annotation 
{return 
  [Wxapi handleopenurl:url delegate:[ Loginviewcontroller Sharelogin]]; 
} 

Third, landing page code

1, micro-letter login licensing more complex than QQ, Sina more than a few steps, simply said that is the need for three steps, the first step, get code, this is used to obtain token, the second step is to take the code to obtain token, the third step, Obtain information about the user based on the token and OpenID obtained in the second step

2,

First step: Get code

-(void) Weixinlogin 
{ 
  sendauthreq* req =[[sendauthreq alloc] init]; 
  Req.scope = @ "Snsapi_userinfo,snsapi_base"; 
  Req.state = @ "0744"; 
  [Wxapi sendreq:req]; 
} 
 
-(void) Onreq: (Basereq *) req 
{ 
  NSLog (@ "hehe"); 
  [Self msghint:@ login failed]; 
} 
 
-(void) Onresp: (BASERESP *) resp 
{ 
  sendauthresp* sender = (sendauthresp*) resp; 
  nsstring* code = Sender.code; 
  NSLog (@ "Cheer code =%@", code); 
   
  Mbprogresshud * HUD = [Mbprogresshud showHUDAddedTo:self.view animated:yes]; 
  Hud.labeltext = @ "Collect user information ..."; 
  [Self getaccess_tokenwithcode:code]; 
} 

Step two to get token

-(void) Getaccess_tokenwithcode: (nsstring*) Mycode {//https://api.weixin.qq.com/sns/oauth2/access_token?appid= Appid&secret=secret&code=code&grant_type=authorization_code nsstring *url =[NSString StringWithFormat: @ "Https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type= 
   
  Authorization_code ", Kwxapp_id,kwxapp_secret,mycode]; Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{nsurl *zoneUrl = [NSURL URLWithStrin 
    G:url]; 
    NSString *zonestr = [NSString stringwithcontentsofurl:zoneurl encoding:nsutf8stringencoding Error:nil]; 
    NSData *data = [Zonestr datausingencoding:nsutf8stringencoding]; Dispatch_async (Dispatch_get_main_queue (), ^{if (data) {nsdictionary *dic = [Nsjsonserialization jsonobje 
        Ctwithdata:data Options:nsjsonreadingmutablecontainers Error:nil]; 
        nsstring* token = [dic objectforkey:@ "Access_token"]; nsstring* OpenID = [dic objectforkey:@ "OpenID"]; 
        [Self Getuserinfowithtoken:token Openid:openid]; 
        NSLog (@ "token =%@", token); 
         
         
      NSLog (@ "OpenID =%@", OpenID); 
  } 
    }); 
}); 
 }

Step three: Get user information

-(void) Getuserinfowithtoken: (nsstring*) MyToken openId: (nsstring*) Myopenid {//https://api.weixin.qq.com/sns/ Userinfo?access_token=access_token&openid=openid nsstring *url =[nsstring stringwithformat:@ "Https://api.weixi 
  n.qq.com/sns/userinfo?access_token=%@&openid=%@ ", Mytoken,myopenid]; 
  NSLog (@ "Infourl =%@", url); Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{nsurl *zoneUrl = [NSURL URLWithStrin 
    G:url]; 
    NSString *zonestr = [NSString stringwithcontentsofurl:zoneurl encoding:nsutf8stringencoding Error:nil]; 
    NSData *data = [Zonestr datausingencoding:nsutf8stringencoding]; Dispatch_async (Dispatch_get_main_queue (), ^{if (data) {nsdictionary *dic = [Nsjsonserialization jsonobje 
        Ctwithdata:data Options:nsjsonreadingmutablecontainers Error:nil]; 
        nsstring* nickname = [dic objectforkey:@ "nickname"]; 
         
        nsstring* Wxheadimgurl = [dic objectforkey:@ "Headimgurl"]; NSLog (@"nickname =%@", nickname); 
         
        NSLog (@ "headimg =%@", wxheadimgurl); 
        Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults]; 
        [Userdefaults Setobject:on forkey:logstate]; 
        [Userdefaults Setobject:thirdfoudationlogin Forkey:logtype]; 
        [Userdefaults Setobject:nickname Forkey:loginname]; 
        [Userdefaults Setobject:wxheadimgurl Forkey:userheaderpath]; 
         
        [Userdefaults Synchronize]; 
        [Mbprogresshud HideAllHUDsForView:self.view Animated:yes]; 
        [Self msghint:@ "micro-letter landing Success"]; 
      [Self popview]; 
     
  } 
    }); 
}); 
 }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.