//

//  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

作者:林肯的红豆
链接:https://www.jianshu.com/p/6d2a106b3206
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

ios-微信支付登录分享-notification通知的更多相关文章

  1. 【微信支付】分享一个失败的案例 跨域405(Method Not Allowed)问题 关于IM的一些思考与实践 基于WebSocketSharp 的IM 简单实现 【css3】旋转倒计时 【Html5】-- 塔台管制 H5情景意识 --飞机 谈谈转行

    [微信支付]分享一个失败的案例 2018-06-04 08:24 by stoneniqiu, 2744 阅读, 29 评论, 收藏, 编辑 这个项目是去年做的,开始客户还在推广,几个月后发现服务器已 ...

  2. iOS微信支付集成

    概述 iOS微信支付集成 详细 代码下载:http://www.demodashi.com/demo/10735.html 支付宝和微信都是业界的老大哥,相信大家都有所觉得文档.SDK都是各种坑吧(纯 ...

  3. iOS微信第三方登录实现

    iOS微信第三方登录实现   一.接入微信第三方登录准备工作.移动应用微信登录是基于OAuth2.0协议标准构建的微信OAuth2.0授权登录系统.在进行微信OAuth2.0授权登录接入之前,在微信开 ...

  4. iOS 微信支付SDK与微信友盟分享两者同时集成时,出现的问题与解决之路。

    这两天改版一个旧的APP,要旧貌换新颜,拿到app后进行编译,一直报下面的错误. 报不认识的符号名PayReq错误.奇怪,啥也没动就这样,真不知道给的包是不是本来就是个报错的工程. 不管怎样,要对它修 ...

  5. iOS 微信支付总结

    1.支付流程 https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_3 商户系统和微信支付系统主要交互说明: 步骤1:用户在商户APP中选择 ...

  6. iOS 微信支付

    相关资料 SDK下载:https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=11_1 APP端开发步骤说明:https://pay.weixin ...

  7. iOS微信支付

    SDK接入 服务器签名版本 官方已经是建议使用服务器签名来接入微信支付,实际上从安全上考虑,确实是每个客户端不应该知道RAS密钥,也不需要每个客户端都写一遍签名的算法. 服务端接入流程文档:https ...

  8. ios微信支付成功后点击左上角返回不走回调的问题

    最近做微信支付发现ios9以后出现的跳转其他app后左上角有返回xxx功能会影响微信支付回调,情况如图 返回后不走下面的方法 - (BOOL)application:(UIApplication *) ...

  9. iOS 微信支付流程详解

    背景 自微信支付.支付宝支付入世以来,移动端的支付日渐火热.虚拟货币有取代实体货币的趋向(这句纯属扯淡,不用管),支付在app开发中是一项基本的功能,有必要去掌握.从难易程度上讲,不管是微信支付还是支 ...

随机推荐

  1. 【转】 ISP概述、工作原理及架构

    1.概述 ISP全称Image Signal Processing,即图像信号处理.主要用来对前端图像传感器输出信号处理的单元,以匹配不同厂商的图象传感器. ISP 通过一系列数字图像处理算法完成对数 ...

  2. http与rfc

    超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP最初的目的是为了提供一种发布和接 ...

  3. javascript的this关键字

    this永远指向函数运行时所在的对象!而不是函数被创建时所在的对象.也即:谁调用,指向谁.切记… 1.普通函数中的this 无论this身处何处,第一要务就是要找到函数运行时的位置. var name ...

  4. 高并发情况下Linux系统及kernel参数优化

    众所周知在默认参数情况下Linux对高并发支持并不好,主要受限于单进程最大打开文件数限制.内核TCP参数方面和IO事件分配机制等.下面就从几方面来调整使Linux系统能够支持高并发环境. Iptabl ...

  5. [luogu P3369]【模板】普通平衡树(Treap/SBT)

    [luogu P3369][模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删 ...

  6. 3D打印GCODE文件学习(一)

    我家有一个天威的入门级的3D打印机.它有一个配套的软件叫“Rrint-RiteCoLiDo Repetier-Host V1.5.5”,用来连接.控制打印机.同时它可以加载各种切片软件,对各种3D模型 ...

  7. Python之路-函数

    一.函数是什么: python中函数定义:函数是逻辑结构化和过程化的一种编程方法.定义函数的方法为: def function(): ""The function definiti ...

  8. day36-多进程多线程一

    多进程 概念:进程是程序在计算机上的一次执行活动.当你运行一个程序,你就启动了一个进程.显然,程序是死的(静态的),进程是活的(动态的).进程可以分为系统进程和用户进程.凡是用于完成操作系统的各种功能 ...

  9. linux的简单操作和安装

    1.学习linux,安装的方式在你的windows上安装一个 vmware虚拟化软件(基于它可以安装各种系统) 2.公司中 会提供一个云服务器给你用 服务器ip 123.206.16.61 公网ip账 ...

  10. GTS-800二次开发基本流程总结

    1.打开控制器 GT_Open 2.启动伺服使能    GT_ClrSts 3.轴规划位置清零 GT_SetPrfPos 4.轴运动模式 GT_PrfTrap 5.轴目标位置 GT_SetPos 6. ...