preface

Recently, I encountered some setbacks when integrating QQ login into the company application, so I thought it was necessary to record it.

I. Integrate SDK

1. Integrate the official Framework

First download the official website SDK iOS_SDK download, so far the latest package is V3.1.0, download after decompression you will see

  • Tencentopenapi. framework packages the iOS SDK header definition and implementation
  • TencentOpenApi_iOS_Bundle. Bundle packages the resource files required by the iOS SDK and then drags these two files into the project.

2.Cocoapods integration mode

In addition to importing the official Framework, there is another way to integrate Pods :TencentOpenApiSDK which I currently use is version 2.9.5. The way I did it was the way Cocoapods integrated. How to choose please make a decision according to your own situation.

2. Configuration project

1. Add the system library file that the SDK depends on

In addition, some libraries need to be imported respectively: “SystemConfiguration.framework” “Security.framework” “CoreTelephony.framework” “CoreGraphics.Framework” “libiconv.tbd” “libsqlite3.tbd” “libstdc++.tbd” “libz.tbd”

Dylib changed to.tbd after XCode7, if you are under XCode7, it is.dylib, but the name of the library is the same

Open the project configuration file in Xcode, select the “General” column, go to the “Linked Framewords and Libraries” option at the bottom and click the Add button below to add the above library.

2. Modify required project configuration attributes.

Go to “Linking” under “Build Settings” in project configuration and add “-fobjc-Arc” to the “Other Linker Flags” configuration.

3. The Info. Plist Settings

Find “URL Types” in “Info” in project configuration and add a new “URL scheme “. Note:

Identifier: tencentopenapi
URL Schemes: tencent + appid
Copy the code

Where Identifier and URL Schemes are mandatory,Identifier is tencentopenapi,URL Schemes are Tencent plus the appid you apply for on the official website. Apply for APPID

Do you think this is the end of it? NO,NO,NO. In order to achieve the effect of clicking on QQ as a jump between applications instead of opening a login page, we need to add a link to the info.plist table. Find the project Info. Plist, then add “LSApplicationQueriesSchemes”

<key>LSApplicationQueriesSchemes</key> <array> <string>mqqapi</string> <string>mqq</string> <string>mqqOpensdkSSoLogin</string> <string>mqqconnect</string> <string>mqqopensdkdataline</string> <string>mqqopensdkgrouptribeshare</string> <string>mqqopensdkfriend</string> <string>mqqopensdkapi</string> <string>mqqopensdkapiV2</string> <string>mqqopensdkapiV3</string> <string>mqzoneopensdk</string> <string>mqqopensdkapiV3</string> <string>mqqopensdkapiV3</string> <string>mqzone</string> <string>mqzonev2</string> <string>mqzoneshare</string> <string>wtloginqzone</string> <string>mqzonewx</string> <string>mqzoneopensdkapiV2</string>  <string>mqzoneopensdkapi19</string> <string>mqzoneopensdkapi</string> <string>mqzoneopensdk</string> </array>Copy the code

Three. Business integration

1. AppDelegate

At this point, our engineering configuration is complete. The next in the AppDelegate # import < TencentOpenAPI/TencentOAuth. H > and rewrite the AppDelegate handleOpenURL and openURL method

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];
}
Copy the code

2. Initialize the iOS SDK API data object TencentOAuth

(1) Create TencentOAuth and initialize its APPID. Demo is 123456789. Delegate is the object that implements the TencentSessionDelegate:

_tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"123456789", andDelegate:self];
Copy the code

The delegate can’t be empty

(2) Set the API list that the application requires user authorization. (It is suggested that users may be reluctant to authorize too many users. It is best to grant only the permissions the application requires from the user. :

NSArray *permissions = [NSArray arrayWithObjects:kOPEN_PERMISSION_GET_INFO, kOPEN_PERMISSION_GET_USER_INFO, kOPEN_PERMISSION_GET_SIMPLE_USER_INFO, nil]; [_tencentOAuth authorize:permissions];Copy the code

Note: the official website of this place is written as follows

_permissions = [[NSArray arrayWithObjects:@"get_user_info"The @"get_simple_userinfo"The @"add_t", nil] retain];
Copy the code

MSG = “this API without user authorization”; ret=100030″

Abide by the agent

You need to comply with the TencentSessionDelegate protocol and implement the protocol methods in your code. There are many methods that are not listed here. For details, see the tencentoAuth.h file in TencentOpenAPI.framework /Headers

Invoking SDK login

1. Invoking the login method is simple

[_tencentOAuth authorize:permissions];
Copy the code

2. After the login is complete, the login protocol method in the TencentSessionDelegate is called

// Successful login: - (void)tencentDidLogin {if(_tencentOAuth. AccessToken. Length > 0) {/ / get the user information [_tencentOAuth getUserInfo]; }else {
        NSLog(@"Failed to log in, did not get accesstoken"); }} // Login failed due to non-network error: - (void)tencentDidNotLogin:(BOOL)cancelled {if (cancelled) {
        NSLog(@"User cancels login");
    } else {
        NSLog(@"Login failed"); }}Copy the code

– (void)getUserInfoResponse:(APIResponse*) response; Method to get the user’s personal information from Response

- (void)getUserInfoResponse:(APIResponse *)response {if (response && response.retCode == URLREQUEST_SUCCEED) {
        
        NSDictionary *userInfo = [response jsonResponse];
        NSString *nickName = userInfo[@"nickname"]; // Subsequent operations... }else {
        NSLog(@"QQ auth fail ,getUserInfoResponse:%d", response.detailRetCode); }}Copy the code

4. Incremental authorization When a third-party application invokes an API, if the server returns an unauthorized operation, the incremental authorization logic is triggered. Third party applications need to implement tencentNeedPerformIncrAuth: withPermissions: protocol interface to enter the incremental authorization logic, or third party applications to abandon incremental authorized by default. The following is an example:

- (BOOL)tencentNeedPerformIncrAuth:(TencentOAuth *)tencentOAuth withPermissions:(NSArray *)permissions { // IncrAuthWithPermissions is to be called incremental authorization login interface / / permissions is to incrementally authorized access list [tencentOAuth incrAuthWithPermissions: permissions];returnNO; // Returning NO indicates that the original request result of the unauthorized API is NO longer needed; // Otherwise return YES}Copy the code

conclusion

So far how to integrate QQ third-party login on the introduction of the end, I integrated when is also jumped a few pits, slowly always good. Please issue me if you have any questions.