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]]) {

? ? ? ? //微信終端向第三方程序請(qǐng)求提供內(nèi)容的消息結(jié)構(gòu)體

//? ? ? ? if (self.wxDelegate

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

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

//? ? ? ? ? ? [self.wxDelegate WXApiUtilsDidRecvGetMessageReq:getMessageReq];

//? ? ? ? }

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

? ? ? ? //微信通知第三方程序,要求第三方程序顯示的消息結(jié)構(gòu)體

//? ? ? ? if (self.wxDelegate

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

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

//? ? ? ? ? ? [self.wxDelegate WXApiUtilsDidRecvShowMessageReq:showMessageReq];

//? ? ? ? }

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

? ? ? ? //微信終端打開第三方程序攜帶的消息結(jié)構(gòu)體

//? ? ? ? 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:@"回調(diào)信息" message:str delegate:self cancelButtonTitle:@"確認(rèn)" 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]]){

? ? ? ? //支付返回結(jié)果,實(shí)際支付結(jié)果需要去微信服務(wù)器端查詢

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

? ? ? ? switch (resp.errCode) {

? ? ? ? ? ? case 0:

? ? ? ? ? ? {

? ? ? ? ? ? ? ? payResoult = @"支付結(jié)果:成功!";

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

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

? ? ? ? ? ? ? ? [[NSNotificationCenter defaultCenter] postNotification:notification];

? ? ? ? ? ? }

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case -1:

? ? ? ? ? ? ? ? payResoult = @"支付結(jié)果:失?。?;

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case -2:

? ? ? ? ? ? {

? ? ? ? ? ? ? ? payResoult = @"用戶已經(jīng)退出支付!";


? ? ? ? ? ? }

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? default:

? ? ? ? ? ? ? ? payResoult = [NSString stringWithFormat:@"支付結(jié)果:失??!retcode = %d, retstr = %@", resp.errCode,resp.errStr];

? ? ? ? ? ? ? ? break;

? ? ? ? }

? ? ? ? UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付結(jié)果" 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:@"下一頁(yè)" 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(@"微信分享");

? ? //創(chuàng)建發(fā)送對(duì)象實(shí)例

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

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

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

? ? //創(chuàng)建分享內(nèi)容對(duì)象

? ? WXMediaMessage *urlMessage = [WXMediaMessage message];

? ? urlMessage.title = @"aaa";//分享標(biāo)題

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

? ? [urlMessage setThumbImage:[UIImage imageNamed:@"testImg"]];//分享圖片,使用SDK的setThumbImage方法可壓縮圖片大小

? ? //創(chuàng)建多媒體對(duì)象

? ? WXWebpageObject *webObj = [WXWebpageObject object];

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

? ? //完成發(fā)送對(duì)象實(shí)例

? ? urlMessage.mediaObject = webObj;

? ? sendReq.message = urlMessage;

? ? //發(fā)送分享信息

? ? [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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 項(xiàng)目中集成的有微信分享功能,之前一直都能正常分享.突然測(cè)試就提了一個(gè)BUG,點(diǎn)擊分享后沒有任何反應(yīng),我一試果真如此...
    小心韓國(guó)人閱讀 3,441評(píng)論 0 7
  • 看到個(gè)不錯(cuò)的微信集成帖子,分享給大家!(因?yàn)槲⑿胖Ц兑舱媸菈驉盒牡?,TM要配置什么環(huán)境都沒說,肯定很多新手會(huì)被惡心...
    白屏閱讀 1,228評(píng)論 0 48
  • iOS支付 iOS支付分為兩類,第三方支付和應(yīng)用內(nèi)支付(內(nèi)購(gòu))。 第三方支付包括:支付寶支付、微信支付、銀聯(lián)支付、...
    請(qǐng)輸入賬號(hào)名閱讀 6,478評(píng)論 3 22
  • 說明:最近公司要求做微信支付,而且還是從h5 界面 調(diào)取微信支付,支付成功之后結(jié)果返回h5 的需求,(使用WebV...
    chulijun閱讀 285評(píng)論 0 0
  • 好吧! 擠吧! 擠 既然, 你們眼中 我存在 只是滿身的 汁液。 但酸楚 相信我 真的 并非我的選擇 只是干枯后 ...
    揚(yáng)眉拂衣閱讀 281評(píng)論 0 0

友情鏈接更多精彩內(nèi)容