IOS 微信分享、支付、登录

//

//  AppDelegate.m

//  NewAppBase

//

//  Created by ENERGY on 2018/5/17.

//  Copyright © 2018年 ENERGY. All rights reserved.

//

#import "AppDelegate.h"

#import "MainViewController.h"

#import "WXApi.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

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


    [WXApi registerApp:@"wxb853e68c5a22a756"];


    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColor whiteColor];

    MainViewController * mainVC = [[MainViewController alloc] init];

    [self.window setRootViewController:mainVC];


    return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

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

    return [WXApi handleOpenURL:url delegate:self];

}

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

    return [WXApi handleOpenURL:url delegate:self];

}

- (BOOL)application:(UIApplication *)app openURL:(nonnull NSURL *)url options:(nonnull NSDictionary *)options{

    [WXApi handleOpenURL:url delegate:self];

    return YES;

}

-(void) onReq:(BaseReq*)req{

    if ([req isKindOfClass:[GetMessageFromWXReq class]]) {

        //微信终端向第三方程序请求提供内容的消息结构体

//        if (self.wxDelegate

//            && [self.wxDelegate respondsToSelector:@selector(WXApiUtilsDidRecvGetMessageReq:)]) {

//            GetMessageFromWXReq *getMessageReq = (GetMessageFromWXReq *)req;

//            [self.wxDelegate WXApiUtilsDidRecvGetMessageReq:getMessageReq];

//        }

    } else if ([req isKindOfClass:[ShowMessageFromWXReq class]]) {

        //微信通知第三方程序,要求第三方程序显示的消息结构体

//        if (self.wxDelegate

//            && [self.wxDelegate respondsToSelector:@selector(WXApiUtilsDidRecvShowMessageReq:)]) {

//            ShowMessageFromWXReq *showMessageReq = (ShowMessageFromWXReq *)req;

//            [self.wxDelegate WXApiUtilsDidRecvShowMessageReq:showMessageReq];

//        }

    } else if ([req isKindOfClass:[LaunchFromWXReq class]]) {

        //微信终端打开第三方程序携带的消息结构体

//        if (self.wxDelegate

//            && [self.wxDelegate respondsToSelector:@selector(WXApiUtilsDidRecvLaunchFromWXReq:)]) {

//            LaunchFromWXReq *launchReq = (LaunchFromWXReq *)req;

//            [self.wxDelegate WXApiUtilsDidRecvLaunchFromWXReq:launchReq];

//        }

    }

}

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

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

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

        SendMessageToWXResp *sendResp = (SendMessageToWXResp *)resp;

        NSString *str = [NSString stringWithFormat:@"%d",sendResp.errCode];

        UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"回调信息" message:str delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];

        [alertview show];

    }

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

        SendAuthResp * rep = (SendAuthResp *)resp;

        if (rep.errCode == 0) {

            [[NSNotificationCenter defaultCenter] postNotificationName:WXAuthorizationSuccess object:@{@"code":rep.code}];

        }

    }

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

        //支付返回结果,实际支付结果需要去微信服务器端查询

        NSString *payResoult = [NSString stringWithFormat:@"errcode:%d", resp.errCode];

        switch (resp.errCode) {

            case 0:

            {

                payResoult = @"支付结果:成功!";

                NSDictionary *dict =@{@"key":@"2"};

                NSNotification *notification =[NSNotification notificationWithName:@"支付成功success" object:nil  userInfo:dict];

                [[NSNotificationCenter defaultCenter] postNotification:notification];

            }

                break;

            case -1:

                payResoult = @"支付结果:失败!";

                break;

            case -2:

            {

                payResoult = @"用户已经退出支付!";


            }

                break;

            default:

                payResoult = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];

                break;

        }

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付结果" message:payResoult delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alert show];

    }

}


//

//  FirstViewController.m

//  NewAppBase

//

//  Created by ENERGY on 2018/5/17.

//  Copyright © 2018年 ENERGY. All rights reserved.

//

#import "FirstViewController.h"

#import "OneOneViewController.h"

#import "WXApi.h"

@interface FirstViewController()

@property (nonatomic, strong)UIButton * skipBtn;

@property (nonatomic, strong)UIButton * wxLoginBtn;

@property (nonatomic, strong)UIButton * wxShareBtn;

@property (nonatomic, strong)NSString * wxLoginCode;

@end

@implementation FirstViewController(networking)

- (void)reportWXLoginCode{

    NSDictionary * dic = @{@"code":self.wxLoginCode};

    [[AFAppDotNetAPIClient sharedClient] POST:@"http://test.wingfac.com/tourism/home/Wxtest/login" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {


    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {


    }];

}

@end

@implementation FirstViewController

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];


}

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view addSubview:self.skipBtn];

    [self.view addSubview:self.wxLoginBtn];

    [self.view addSubview:self.wxShareBtn];

    //微信登录

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(WXLogin:) name:WXAuthorizationSuccess object:nil];

    //微信支付

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(paySuccess:) name:@"支付成功success" object:nil];

}

- (UIButton *)skipBtn{

    if (!_skipBtn) {

        _skipBtn = [[UIButton alloc] init];

        [_skipBtn setTitle:@"下一页" forState:UIControlStateNormal];

//        [_skipBtn setImage:[UIImage imageNamed:@"tab_bofang"] forState:UIControlStateNormal];

        [_skipBtn addTarget:self action:@selector(skipBtnClick) forControlEvents:UIControlEventTouchUpInside];

        [_skipBtn setBackgroundColor:[UIColor blackColor]];

        [_skipBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        _skipBtn.layer.cornerRadius = 6;

        _skipBtn.layer.masksToBounds = YES;

        _skipBtn.frame = CGRectMake(10/WIDTH_5S_SCALE, 10/WIDTH_5S_SCALE, 65/WIDTH_5S_SCALE, 20/WIDTH_5S_SCALE);

    }

    return _skipBtn;

}

- (UIButton *)wxLoginBtn{

    if (!_wxLoginBtn) {

        _wxLoginBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.skipBtn.left, self.skipBtn.bottom + 10/WIDTH_5S_SCALE, 30/WIDTH_5S_SCALE, 30/WIDTH_5S_SCALE)];

        [_wxLoginBtn setImage:[UIImage imageNamed:@"Wechat"] forState:UIControlStateNormal];

        [_wxLoginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        _wxLoginBtn.layer.cornerRadius = 6;

        _wxLoginBtn.layer.masksToBounds = YES;

        [_wxLoginBtn addTarget:self action:@selector(wxLoginBtnClick) forControlEvents:UIControlEventTouchUpInside];

    }

    return _wxLoginBtn;

}

- (UIButton *)wxShareBtn{

    if (!_wxShareBtn) {

        _wxShareBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.skipBtn.left, self.wxLoginBtn.bottom + 10/WIDTH_5S_SCALE, 70/WIDTH_5S_SCALE, 30/WIDTH_5S_SCALE)];

        [_wxShareBtn setTitle:@"微信分享" forState:UIControlStateNormal];

        [_wxShareBtn setBackgroundColor:getColor(@"dcdcdc")];

        [_wxShareBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

        _wxShareBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];

        [_wxShareBtn addTarget:self action:@selector(wxShareBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    }

    return _wxShareBtn;

}

- (void)skipBtnClick{

    OneOneViewController * vc = [[OneOneViewController alloc] init];

    vc.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:vc animated:YES];

}

#pragma mark - action

//分享

- (void)wxShareBtnClick:(UIButton *)sender{

    NSLog(@"微信分享");

    //创建发送对象实例

    SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];

    sendReq.bText = NO;//不使用文本信息

    sendReq.scene = 1;//0 = 好友列表 1 = 朋友圈 2 = 收藏

    //创建分享内容对象

    WXMediaMessage *urlMessage = [WXMediaMessage message];

    urlMessage.title = @"aaa";//分享标题

    urlMessage.description = @"bbb";//分享描述

    [urlMessage setThumbImage:[UIImage imageNamed:@"testImg"]];//分享图片,使用SDK的setThumbImage方法可压缩图片大小

    //创建多媒体对象

    WXWebpageObject *webObj = [WXWebpageObject object];

    webObj.webpageUrl = @"https://link.jianshu.com/?t=https://itunes.apple.com/us/app/hi-tian-qi/id1146330042?mt=8";//分享链接

    //完成发送对象实例

    urlMessage.mediaObject = webObj;

    sendReq.message = urlMessage;

    //发送分享信息

    [WXApi sendReq:sendReq];

}

//登录

- (void)wxLoginBtnClick{

    NSLog(@"微信登录");

    if ([WXApi isWXAppInstalled]) {

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

        //request.openID == @0;

        request.state = @"wx_oauth2_authorization_state";

        request.scope = @"snsapi_userinfo";

        [WXApi sendReq:request];

    }

}

//微信登录

- (void)WXLogin:(NSNotification *)notification{

    NSDictionary * dic = notification.object;

    self.wxLoginCode = dic[@"code"];

    [self reportWXLoginCode];

}

//微信支付

- (void)paySuccess:(NSNotification *)notification{

    NSLog(@"成功");

    NSDictionary * dic = notification.userInfo;

    NSString * mark = dic[@"key"];

    NSLog(@"%@", mark);

}

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

@end

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容

  • 项目中集成的有微信分享功能,之前一直都能正常分享.突然测试就提了一个BUG,点击分享后没有任何反应,我一试果真如此...
    小心韩国人阅读 2,946评论 0 7
  • 看到个不错的微信集成帖子,分享给大家!(因为微信支付也真是够恶心的,TM要配置什么环境都没说,肯定很多新手会被恶心...
    白屏阅读 1,160评论 0 48
  • iOS支付 iOS支付分为两类,第三方支付和应用内支付(内购)。 第三方支付包括:支付宝支付、微信支付、银联支付、...
    请输入账号名阅读 6,151评论 3 22
  • 说明:最近公司要求做微信支付,而且还是从h5 界面 调取微信支付,支付成功之后结果返回h5 的需求,(使用WebV...
    chulijun阅读 235评论 0 0
  • 好吧! 挤吧! 挤 既然, 你们眼中 我存在 只是满身的 汁液。 但酸楚 相信我 真的 并非我的选择 只是干枯后 ...
    扬眉拂衣阅读 243评论 0 0