IOS code analysis of third-party authorized QQ logon and Sina Weibo authorized Logon
The third-party APP login QQ and Sina Weibo authorization are implemented based on iOS, which clearly shows the differences and ease of use between the two sdks during development, here we will describe the basic steps for third-party SDK access:
1. Are you sure you want to use this feature? If you log on Via QQ, it seems that the APP registration is mainstream;
2, find the relevant open platform, such as QQ platform, http://connect.qq.com /;
3. After successful registration, create your own APP. Fill in a bunch of basic signals and obtain an App_Key, which is the unique identifier of your APP;
3. Download the corresponding SDK and read the development documentation or demo. Generally, the development documentation is intuitive. If you look at the demo, some SDK demos cannot be directly viewed;
4. embed the corresponding function in the SDK. After the test is successful, the function is submitted to a third-party platform for review. After the verification is passed, the function of the third-party SDK can be formally integrated;
5. Submit the App Store for review after completing all functions. It may take about a week.
Er, after talking about this, let's go to QQ login and Sina Weibo login authorization. This simple login method greatly simplifies user registration, it also extends the social nature of the APP, making it easier to share and promote the APP. After login, you can obtain the unique ID of the user, which is only bound to the APP background, just like a user who has registered the APP, it seems that it has become the standard login mode for all major apps. It seems that the drawer-style design has gradually changed back to the TabBar style, for specific causes, refer to network resources.
1. Authorized QQ login development:
1. Import the SDK according to the development documents and add the Key obtained after registration to the Url Schemes. For example, tencent110172.1616. Note that these documents are clearly written;
2. Implement the following method in AppDelegate. m. Note that you can directly copy the code and write either of them. Of course, do not forget to introduce the header file. This is the callback function used to return the APP.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ return [TencentOAuth HandleOpenURL:url];}- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ return [TencentOAuth HandleOpenURL:url];}3. Register and authorize. Note that problems may occur during authorization.
_ TencentOAuth = [[TencentOAuth alloc] initWithAppId: kQQ_KEY andDelegate: self]; // register
NSArray * _ permissions = [NSArray arrayWithObjects: kOPEN_PERMISSION_GET_INFO, Token, delimiter, nil]; [_ tencentOAuth authorize: _ permissions inSafari: NO]; // authorization
Note: The Authorization Code mentioned in the official document is: _ permissions = [[NSArray arrayWithObjects: @ "get_user_info", @ "add_t", nil] retain]; // The official authorization code
If you write it like this, you will find that you are always unable to log on, prompting you that the permissions are not enough. The correct authorization code is written as described above. Remember
4. Implement the callback function for Logon success or failure, and implement the TencentSessionDelegate protocol. The Code is as follows:
-(Void) tencentDidLogin {_ labelTitle. text = @ "Logon completed"; if (_ tencentOAuth. accessToken & 0! = [_ TencentOAuth. accessToken length]) {// record the login user's OpenID, Token, and expiration time _ labelAccessToken. text = _ tencentOAuth. accessToken;} else {_ labelAccessToken. text = @ "the logon fails. The accesstoken is not obtained." ;}}-(void) tencentDidNotLogin :( BOOL) canceled {if (canceled) {_ labelTitle. text = @ "";} else {_ labelTitle. text = @ "Logon Failed" ;}}- (void) tencentDidNotNetWork {_ labelTitle. text = @ "No network connection, please set the network ";}
5. If the code can be accessed and the accesstoken is obtained, the logon process is complete. The remaining information is used to obtain the user's specific information, such as the nickname, cities;
6. How can I get a nickname? The document provides this method.
[_tencentOAuth getUserInfo];
However, the return type is similar to BOOL, and many friends are depressed. What is the problem? after reading the code, I found that if the developer calls this method, if it succeeds, the callback function in the TencentSessionDelegate Protocol below will be implemented:
-(void)getUserInfoResponse:(APIResponse *)response{ // NSLog(@"respons:%@",response.jsonResponse); self.name.text = [response.jsonResponse objectForKey:@"nickname"];}
In this way, we can get the nickname and other information we want. Here, even if our QQ login authorization is completed, the rest is to send the login information to the background, and keep the token after the authorization is successful. Read the instructions in the official documentation.
2. Authorize Sina Weibo login for development:
1. Like QQ, configure Url Schemes, such as wb20454000052, and access the SDK according to the official documentation;
2. The following callback is also implemented in AppDelegate. m:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ return [TencentOAuth HandleOpenURL:url] || [WeiboSDK handleOpenURL:url delegate:self];}- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ return [TencentOAuth HandleOpenURL:url] || [WeiboSDK handleOpenURL:url delegate:self];}3. The successful login callback of Sina Weibo is written in this class for implementation, because self is written here. If you need to implement it in another class, you can modify it here.
[WeiboSDK handleOpenURL:url delegate:self];
The implementation protocol is WeiboSDKDelegate. The Protocol method is as follows:
-(void)didReceiveWeiboResponse:(WBBaseResponse *)response{ if ([response isKindOfClass:WBAuthorizeResponse.class]) { if ((int)response.statusCode == 0) { NSDictionary *dic = @{@"userID":[(WBAuthorizeResponse *)response userID], @"accessToken" :[(WBAuthorizeResponse *)response accessToken]}; } }}If you can obtain this userID, the token indicates that the logon is successful.
4. logon code:
- (IBAction)sinaLogin:(id)sender{ [WeiboSDK enableDebugMode:YES]; [WeiboSDK registerApp:kSINA_KEY]; WBAuthorizeRequest *request = [WBAuthorizeRequest request]; request.redirectURI = kRedirectURI; request.scope = @"all"; request.userInfo = @{@"myKey": @"myValue"}; [WeiboSDK sendRequest:request];}
This includes registration, debugging log opening, callback address, user-defined submitted info, etc. If the receiving function in AppDelegate can be correctly called back after logon, this means that our bride Weibo login is successful, and the rest is to save the relevant information and submit it to the background. It is worth noting that if your Bundle identifier is the same as the key application, otherwise, modify the Bundle identifier of your demo to be the Bundle identifier of the official Sina Weibo demo and use its key for testing. After your application is successful, you can replace it for debugging again;
Evaluation: after the two platforms are connected, the functions are very rich, but from the developer's point of view, the QQ login authorization method is relatively simple, and the integration test fee is relatively small, but the demo code is very difficult to understand, the official documentation may even mislead the code and make debugging very difficult. The logon authorization demo code on Sina Weibo is very concise and clear and easy to understand. In terms of code writing, sina Weibo's developers are better than QQ's developers, but Sina Weibo's successful callback writing lags behind. It needs to be obtained in AppDelegate, which is convenient without QQ callback, in addition, the Sina login authorization is bound with a Bundle identifier, which is confusing and time-consuming. The two methods have different styles, but it is still quite smooth in general, of course, if you do not want to integrate the login authorization of the two companies separately, you can recommend that Baidu development platform integrate their login SDK, which integrates major platforms such as QQ and Sina Weibo, renren.
Here is the success:
Note: here is just for the same development of friends to provide a little I encountered in the development of problems, such as the need for QQ and Sina Weibo authorized Login demo code, can mail me: mmw05@163.com, further discussion.