Third party QQ Login to obtain personal information

Source: Internet
Author: User
Tags openid

QQ Login iOS SDK encapsulates the login authorization of QQ login and most openapi, the application only need to modify the corresponding parameters, do not need to understand the authentication authorization process, can quickly realize the QQ login function.

iOS platform (ipad,iphone,ipod) app, please use the iOS SDK provided by QQ Internet after applying for Appid,appkey.

Application URL http://connect.qq.com

1. IOS SDK Download

Please go to the SDK download page to download the latest version of QQ login iOS SDK.

2. IOS SDK Directory structure

There are two files in the IOS SDK package:

1. tencentopenapi.framework packaged the header file definition and implementation for the iOS SDK.

2. Tencentopenapi_ios_bundle.bundle packaged the resource files required by the IOS SDK.

3. Add the iOS SDK file to your project

1. Copy the Tencentopenapi.framework and Tencentopenapi_ios_bundle.bundle files from the IOS SDK to the app development directory.

Then drag the tencentopenapi.framework from the Save directory of the SDK to the frameworks virtual directory in the Project Navigation view (Project Navigator).

2. In the popup dialog box, tick "Create groups for any added folders", remove "copy items into destination group ' s folder (if needed)" in Add to Targe In TS Select target to join the SDK and click Finish. Once completed, the iOS SDK framework files were added to the development project.

3. Add the System library files that the SDK relies on. respectively, "Security.framework", "Libiconv.dylib", "Systemconfiguration.framework", "Coregraphics.framework", " Libsqlite3.dylib "," Coretelephony.framework "," Libstdc++.dylib "," Libz.dylib ".

4. Open the project configuration file in Xcode and select the "Build Phases" column.

5. In "Build phases", select expand the "Copy Bundle Resources" column and click the "+" icon

6. Select "Add other ...", go to the directory where the IOS SDK files are located, select Tencentopenapi_ios_bundle.bundle, click Enter or click "Open".

7. Modify the necessary engineering configuration properties.

In the "Build Settings" column in the project configuration, locate the "linking" configuration area and add the property value "-fobjc-arc" to the "other Linker Flags" configuration item.

4. Modify the necessary Code 4.1 modify the project configuration file

In Xcode, select your project settings, select the "TARGETS" column, add a new "URL scheme" in the "Info" tab, "URL type", new scheme = Tencent + AppID. If you are using XCODE3 or a lower version, you will need to add it in the Plist file. The AppID we registered in the demo is 222222. Such as

In addition, the bundle Display Name property value may not be set individually by default when creating a project in Xcode 6.0. But because the SDK needs to use the value of the bundle display name, be sure to check to make sure that this property exists, if not added on.

4.2 Overriding the Handleopenurl and OpenURL methods of Appdelegate

OpenURL:

-(BOOL) Application: (UIApplication *) application OpenURL: (nsurl *) URL sourceapplication: (NSString *) sourceapplication annotation: (ID) annotation{

return [Tencentoauth Handleopenurl:url];

}

Handleopenurl:

-(BOOL) Application: (UIApplication *) application Handleopenurl: (Nsurl *) url{

return [Tencentoauth Handleopenurl:url];

}

4.3 Implementing the methods in the Tencentsessiondelegate protocol in code

The specific protocol can refer to the TencentOAuth.h file in Tencentopenapi.framework/headers


4.4 Initialize iOS SDK API data Object Tencentoauth.


(1) Create a tencentoauth and initialize its appid,demo to 222222. Delegate is the object that implements Tencentsessiondelegate:

_tencentoauth = [[Tencentoauth alloc]initwithappid:@ "222222", anddelegate:self];

Delegate can't be empty here.
(2) Initialize the Redirecturi (you need to fill in the domain name when registering the app. The default is no need to fill out. It is not recommended to fill in. The address registered in the demo is "Www.qq.com"):

_tencentoauth.redirecturi =@ "www.qq.com";


(3) Set the list of APIs that the app requires user authorization.

(It is recommended that if there is too much authorization, users may be reluctant to authorize.) It's a good idea to only authorize apps that require user-given authorization. ):

_permissions = [[nsarrayarraywithobjects:@ "Get_user_info", @ "Get_simple_userinfo", @ "add_t", Nil] retain];

5. Invoking the SDK Login

1. When logging in, call the authorize method of the Tencetnoauth object:

[_tencentoauthauthorize:_permissions Insafari:no];

2. When the login is complete, the protocol method for login in Tencentsessiondelegate is called.
Login success:

@protocol tencentsessiondelegate<nsobject>

-(void) Tencentdidlogin

{

_labeltitle.text = @ "Login complete";

if (_tencentoauth.accesstoken && 0!= [_tencentoauth.accesstoken length])

{

Record the OpenID, token, and expiration time of the logged-on user

_labelaccesstoken.text =_tencentoauth.accesstoken;

}

Else

{

_labelaccesstoken.text = @ "Login unsuccessful did not get Accesstoken";

}

}

A non-network error caused logon failure:

@protocol tencentsessiondelegate<nsobject>

-(void) Tencentdidnotlogin: (BOOL) cancelled

{

if (cancelled)

{

_labeltitle.text = @ "user Cancel login";

}

Else

{

_labeltitle.text= @ "Login failed";

}

}


A network error caused logon failure:

@protocol tencentsessiondelegate<nsobject>

-(void) tencentdidnotnetwork

{

[Email protected] "No network connection, please set up the network";

}

3. Once the login is successful, access token and OpenID can be obtained. Accesstoken and OpenID are saved in the Tencentoauth object. Can be obtained directly through the corresponding property method.

[_tencentoauth Accesstoken];

[_tencentoauth OpenId];

Special Don't Tips:

1. Because the login is an asynchronous process, there may be a process in which the user's behavior causes the entire login to fail to complete properly, that is, there is a possibility that no login callbacks will be invoked after the login is completed due to user behavior. Developers need to take this into account when developing with the SDK, preventing the application from being stuck due to a callback that is waiting to be logged in synchronously, and it is recommended that the implementation be made an asynchronous process when logged in.
2. Acquired access token has a 3-month validity period and prompts the user to re-login authorization after expiration.

3. Third-party websites can store access token information for subsequent calls to Openapi to access and modify user information. If you need to save the authorization information, you need to save the login completed after the return of Accesstoken,openid and expirationdate three data, the next time you log in to the three data is set to the Tencentoauth object.

Get:

[_tencentoauth Accesstoken];

[_tencentoauth OpenId];

[_tencentoauth ExpirationDate];

Set up:

[_tencentoauth Setaccesstoken:accesstoken];

[_tencentoauth Setopenid:openid];

[_tencentoauth Setexpirationdate:expirationdate];


4. It is recommended that after the user is logged in, the GetUserInfo interface is called to get the user's avatar, nickname and display on the interface, so that the user experience is unified.

Third party QQ Login to obtain personal information

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.