Egret白鹭引擎发布iOS原生项目(二)接入微信登录

一、微信开放平台创建移动应用通过后填写iOS平台信息

image.png

二、通过CocoaPods集成微信SDK

在工程的Podfile加入

pod 'WechatOpenSDK'

image.png

保存并在终端执行pod install,然后用后缀为.xcworkspace的文件打开工程。

如果提示pod命令不存在安装一下CocoaPods

在Xcode中,选择你的工程设置项,选中“TARGETS”一栏,在“info”标签栏的“URL type“添加“URL scheme”为你微信开放平台所注册的应用程序id

image.png

在Custom iOS Target Properties 新增LSApplicationQueriesSchemes如下

image.png

三、原生接入微信登录

1、在AppDelegate.h文件中import WXApi.h 头文件,并增加 WXApiDelegate 协议#import <UIKit/UIKit.h>

#import "WXApi.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

2、AppDelegate.m修改如下

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    //向微信注册

    [WXApi registerApp:@"wx9e7733bb10ba29f8"];

    return YES;

}

-(void) onReq:(BaseReq*)req

{

}

// 授权后回调

// AppDelegate.m

- (void)onResp:(BaseResp *)resp {

    // 向微信请求授权后,得到响应结果

    NSLog(@"-->%d",resp.errCode);

    NSString* tip = @"";

    switch(resp.errCode){

        case WXSuccess:

            if ([resp isKindOfClass:[SendAuthResp class]]) {

                SendAuthResp *temp = (SendAuthResp *)resp;

                NSString* code = temp.code;

                [ViewController callJSForCode:code];

                NSLog(@"登录成功");

                tip = @"授权成功";

            }

            else if([resp isKindOfClass:[SendMessageToWXResp class]]){

                NSLog(@"分享成功");

                tip = @"分享成功";

            }

            break;

        case WXErrCodeUserCancel:

            NSLog(@"用户取消");

            tip = @"用户取消";

            break;

        case WXErrCodeAuthDeny:

            NSLog(@"用户拒绝权限");

            tip = @"用户拒绝权限";

            break;

        case WXErrCodeUnsupport:

            NSLog(@"暂不支持");

            tip = @"暂不支持";

            break;

        default:

            NSLog(@"未知错误");

            tip = @"未知错误";

    }

    [CBToast showToast:tip location:@"bottom" showTime:3.0];

}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

{

    return  [WXApi handleOpenURL:url delegate:self];

}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

{

    BOOL isSuc = [WXApi handleOpenURL:url delegate:self];

//    NSLog(@"url %@ isSuc %d",url,isSuc == YES ? 1 : 0);

    return  isSuc;

}


3、ViewController.m文件注册接收消息与js通讯

    [EgretWebViewLib setExternalInterface:@"callNativeForLogin" Callback:^(NSString* msg) {

        NSLog(@"message: %@", msg);

        [self wechatLogin];

    }];

实现微信登录

- (void)wechatLogin {

    if ([WXApi isWXAppInstalled]) {

        SendAuthReq *req = [[SendAuthReq alloc] init];

        req.scope = @"snsapi_userinfo";

        req.state = @"App";

        [WXApi sendReq:req];

    }

    else {

        [self setupAlertController];

    }

}

#pragma mark - 设置弹出提示语

- (void)setupAlertController {

    

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请先安装微信客户端" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *actionConfirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];

    [alert addAction:actionConfirm];

    [self presentViewController:alert animated:YES completion:nil];

}

#pragma mark - 将获取到的code传给js端做后续处理

+ (void)callJSForCode:(NSString*)code{

    [EgretWebViewLib callExternalInterface:@"callJSForCode" Value:[[NSString alloc] initWithFormat:@"{\"code\":\"%@\"}",code]];

}


发表评论

(必填)

(必填)

(以便回访)

博客已稳定运行: 访问量:167,497 views 网站备案号:闽ICP备17026396号-1