当前位置:网站首页>Uni native plug-in development -- Youmeng one click login

Uni native plug-in development -- Youmeng one click login

2022-07-23 12:23:00 Try to be a charterer

umeng SDK Get ready

download UMVerify SDK

Manually integrate Youmeng SDK, Open the official website of Youmeng , Select the Developer Center -SDK
 Insert picture description here
After entering, choose the end — Selection platform — Choose products and services for integration
 Insert picture description here

 Insert picture description here

take SDK Import the project

After downloading
.framework Import into the main project .
.bundle Import to Bundles Under the folder .
Here we need to pay attention to , All the third party's .bundle All documents need to be put here . Otherwise, you can't quote .

First step : download SDK

 Insert picture description here

The second step : Import of the main project framework Inside

 Insert picture description here

The third step :bundle Import to the specific folder of the main project .

 Insert picture description here

umeng SDK The configuration of can be in accordance with the official documents .

Problems encountered

If the following problems occur in the plug-in project , You can follow the steps below to refer to the solution .

1、 problem : The plug-in project references the header file of Youmeng and reports an error , Prompt: the file cannot be found . solve : Will download the header file of Youmeng , Copy all to the plug-in project file , Then put its path in targets—search paths—framework search paths Inside . Pictured

 Insert picture description here
 Insert picture description here

In this way, you can use the header file of Youmeng when compiling and running .

Code calls

iOS End code

Create a new one YMModule file , Inherit DCUniModule. stay YMModule.h Plug ins are introduced into the file 、 Header file required by Youmeng . Here's the picture
 Insert picture description here
The first header file is inherited by plug-in development , Must write
The second is the header file that Youmeng needs to reference , Must write
The third is for the third party sharing of the alliance .

The three in the box are the header files required for Youmeng one click login .
The first is one click login SDK The header file , Required
The second is the rewriting of Youmeng one click login page style .( Quote according to the actual situation )
The third is the wind and fire wheel .( Quote according to the actual situation )

.m file

@implementation YMModule
//  umeng   One click login . Macro definitions are used for uniapp call 
UNI_EXPORT_METHOD(@selector(UmLoginVerify:callback:))

#pragma mark -  One click login  js Method called 
- (void)UmLoginVerify:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback
{
    
   // options  by  js  Parameters passed when calling this method on the client side 
   //  The first parameter is returned to js End data , The second parameter is identification , Indicates whether the callback method supports multiple calls , If the native side needs multiple callbacks js At the end, the second parameter is transmitted to  YES;
    
    float timeout = 5.0;
    __weak typeof(self) weakSelf = self;
    [ProgressHUD show:@" One moment please ..." Interaction:YES];
    
    //  Initialize relevant configurations of Youmeng 
    [UMConfigure initWithAppkey:[options objectForKey:@"UMAppkey"] channel:@"App Store"];
    //  Set key 
    NSString*info = [NSString stringWithFormat:@"%@",[options objectForKey:@"VerifySDKInfo"]];

    // Set up SDK Parameters ,app It can be called once in the life cycle 
    [UMCommonHandler setVerifySDKInfo:info complete:^(NSDictionary*_Nonnull resultDic){
    
        NSLog(@"VerifySDKInf=%@",resultDic);
    }];
    //  Check whether the current environment supports one click login , Support knowing in advance  (UMPNSAuthTypeLoginToken  Check the one click login environment  UMPNSAuthTypeVerifyToken  Check the number authentication environment )
        __block BOOL support = YES;
    [UMCommonHandler checkEnvAvailableWithAuthType:UMPNSAuthTypeLoginToken complete:^(NSDictionary*_Nullable resultDic){
    
            support =[PNSCodeSuccess isEqualToString:[resultDic objectForKey:@"resultCode"]];
    }];
    // Is there a sim card 
    if ([UMCommonUtils simSupportedIsOK]){
    
        [UMCommonHandler accelerateLoginPageWithTimeout:timeout complete:^(NSDictionary * _Nonnull resultDic) {
    
            if ([PNSCodeSuccess isEqualToString:[resultDic objectForKey:@"resultCode"]] == NO) {
    
                [ProgressHUD showError:@" Take the number , Failed to pop up the acceleration authorization page "];
                if (callback) {
    
                    callback(resultDic,YES);
                }
                return ;
            }
                
            //2.  Call get login Token Interface , You can pop up the authorization page immediately 
            [ProgressHUD dismiss];
            UMCustomModel *model = [weakSelf buildCustomModel:NO];
            model.supportedInterfaceOrientations = UIInterfaceOrientationMaskAllButUpsideDown;
            
            [UMCommonHandler getLoginTokenWithTimeout:timeout controller:[YMModule dc_findCurrentShowingViewController] model:model complete:^(NSDictionary * _Nonnull resultDic) {
    
                NSLog(@"logintoken:%@",resultDic);
                if (callback) {
    
                    callback(resultDic,YES);
                }
                NSString *code = [resultDic objectForKey:@"resultCode"];
                if ([PNSCodeLoginControllerPresentSuccess isEqualToString:code]) {
    
                    [ProgressHUD showSuccess:@" Successfully pop up the authorization page "];
                } else if ([PNSCodeLoginControllerClickCancel isEqualToString:code]) {
    
                    [ProgressHUD showSuccess:@" Click return to the authorization page "];
                } else if ([PNSCodeLoginControllerClickChangeBtn isEqualToString:code]) {
    
                    [ProgressHUD showSuccess:@" Click the switch other login methods button "];
                } else if ([PNSCodeLoginControllerClickLoginBtn isEqualToString:code]) {
    
                    if ([[resultDic objectForKey:@"isChecked"] boolValue] == YES) {
    
                        [ProgressHUD showSuccess:@" Click the login button ,check box Choose ,SDK Internal will then go to get login Token"];
                    } else {
    
                        [ProgressHUD showSuccess:@" Click the login button ,check box Choose ,SDK Internal will not get login Token"];
                    }
                } else if ([PNSCodeLoginControllerClickCheckBoxBtn isEqualToString:code]) {
    
                    [ProgressHUD showSuccess:@" Click on check box"];
                } else if ([PNSCodeLoginControllerClickProtocol isEqualToString:code]) {
    
                    [ProgressHUD showSuccess:@" Click on the protocol rich text "];
                } else if ([PNSCodeSuccess isEqualToString:code]) {
    
                    // Click the login button   Get login Token Successful callback 
                    NSString *token = [resultDic objectForKey:@"token"];
                    //  Take token Go to the server and change your mobile number  
                    //  If it is a native function   This place can call the server interface . 
                    // Now it's plug-in development , This token It needs to be passed to uniapp 了 .
                    // Here is the whole result Pass to uniapp.
                   
                    dispatch_async(dispatch_get_main_queue(), ^{
    
                        [UMCommonHandler cancelLoginVCAnimated:YES complete:nil];
                    });
                }
            }];
        }];
    }else {
    
        [self debugLogin:[YMModule dc_findCurrentShowingViewController] model:nil];
    }
}

// debug Login method
-(void)debugLogin:(UIViewController *)controller model:(UMCustomModel *)model
// Call up the one click login page
-(UMCustomModel *)buildCustomModel:(BOOL)isAlert

//  This is debug Login method , Can not write .
-(void)debugLogin:(UIViewController *)controller model:(UMCustomModel *)model
{
    
    [UMCommonHandler debugLoginUIWithController:controller model:model complete:^(NSDictionary * _Nonnull resultDic) {
    

    }];
}
//  This method is to invoke the one click login page 
- (UMCustomModel *)buildCustomModel:(BOOL)isAlert {
    
    if (isAlert) {
    
        return [UMModelCreate createAlert];
    } else {
    
        return [UMModelCreate createFullScreen];
    }
}

// Get the currently displayed UIViewController
+(UIViewController *)dc_findCurrentShowingViewController
+(UIViewController *)findCurrentShowingViewControllerFrom:(UIViewController *)vc

because module Not included viewcontroller, So it's using viewcontroller The above method is needed where .

such as , The code in the native function is used like this .
[UMCommonHandler getLoginTokenWithTimeout:timeout controller:self model:model complete:^(NSDictionary * _Nonnull resultDic) { }];

there self In the plug-in project, it needs to be changed to [YMModule dc_findCurrentShowingViewController]
The complete code will become

[UMCommonHandler getLoginTokenWithTimeout:timeout controller:[YMModule dc_findCurrentShowingViewController] model:model complete:^(NSDictionary * _Nonnull resultDic) { }];


#pragma mark -  obtain viewcontroller
//  Get the currently displayed  UIViewController
+ (UIViewController *)dc_findCurrentShowingViewController {
    
    // Get the root view of the currently active window 
    UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *currentShowingVC = [self findCurrentShowingViewControllerFrom:vc];
    return currentShowingVC;
}
+ (UIViewController *)findCurrentShowingViewControllerFrom:(UIViewController *)vc
{
    
    //  Recursive method  Recursive method
    UIViewController *currentShowingVC;
    if ([vc presentedViewController]) {
    
        //  The current view is presented Coming out 
        UIViewController *nextRootVC = [vc presentedViewController];
        currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];

    } else if ([vc isKindOfClass:[UITabBarController class]]) {
    
        //  The root view is UITabBarController
        UIViewController *nextRootVC = [(UITabBarController *)vc selectedViewController];
        currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];

    } else if ([vc isKindOfClass:[UINavigationController class]]){
    
        //  The root view is UINavigationController
        UIViewController *nextRootVC = [(UINavigationController *)vc visibleViewController];
        currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];

    } else {
    
        //  The root view is a non navigational class 
        currentShowingVC = vc;
    }

    return currentShowingVC;
}


@end

iOS Data returned after successful login

Log in successfully , Get token Parameters will be returned where . According to what I got token value , Request server interface , To verify .

{
carrierFailedResultData = {
};
innerCode = 103000;
innerMsg = “”;
msg = “”;
requestId = c724a6248bd64c3f;
resultCode = 600000;
token = 123456;
}

uniapp End code


<template>
    <div>
        <button type="primary" @click="testAsyncFunc">testAsyncFunc</button>
        <button type="primary" @click="testSyncFunc">testSyncFunc</button>
		<button type="primary" @click="testUmLoginVerify">testUmLoginVerify</button>
    </div>
</template>

<script> //  First we need to pass  uni.requireNativePlugin("ModuleName")  obtain  module  var testModule = uni.requireNativePlugin("cymtestPlugins-YMModule") export default {
       methods: {
       testAsyncFunc() {
       //  Call asynchronous methods  testModule.testAsyncFunc({
       'name': 'uni-app', 'age': 1 }, (ret) => {
       uni.showToast({
       title:'cym-demo- Call asynchronous methods  ' + ret, icon: "none" }) }) }, testSyncFunc() {
       //  Call the synchronization method  var ret = testModule.testSyncFunc({
       'name': 'uni-app', 'age': 1 }) uni.showToast({
       title:' Call the synchronization method cym ' + ret, icon: "none" }) } testUmLoginVerify(){
       testModule.UmLoginVerify({
       'UMAppkey': '59892ebcaed179694b000104', 'VerifySDKInfo': 'WPsC0MdV+g0nHmn1HdpGlPp1aiL6IM8oTjxG0DI89yFBhYp7mTTOQOMSJJMcbiIGd+6XdDs8pILvvklioVfVGkUYsULKoIlh1UqWwNl9LRXW6/jgK55oxgPk20vffYdc85XTPtU8BacNSJgyItD1WXh2DryPX7RQoPW3vJyKVmPv3LH3XAd5MVvjsWHtTW30nF1ZySe5WMRyE7q7MTAJsfEBe2Fa3P6KQNffAA2fGcaM7d0aOnFAoD6MgxaGSoph/vAvDpDiW4A=' }, (ret) => {
       uni.showToast({
       title:'cym-demo-UmLoginVerify, Get token ' + ret.token, icon: "none" }) }) } } } </script>


Local run debugging

Be careful uniapp The package name after packaging needs to be consistent with iOS Inside control In the document appid bring into correspondence with .

uniapp Inside
 Insert picture description here

iOS Inside
 Insert picture description here

uniapp Export local package
 Insert picture description here

Pay attention here , The use of HBuilder X Tools should be consistent with iOS In the plug-in SDK Keep the version . Otherwise, the operation will report an error . Unification here is the latest 3.4.18 edition .
 Insert picture description here
Click this path to get the latest local package , Pull the bag to iOS Inside the project .
 Insert picture description here

So we can run iOSAPP 了 .

 Insert picture description here

 Insert picture description here

原网站

版权声明
本文为[Try to be a charterer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230538418305.html