1-> 首先去微信开放平台 https://open.weixin.qq.com注册一个appid
2-> 然后去微信开发中心去下载需要的SDK
https://open.weixin.qq.com
3->下载好SDK导入到工程里面
4-> 导入相应的架包
5-> 设置linking (Build Settings- > linking->other linking flags)
中间的是libWebChatSDK.a
6->在xcode中注册 点击target ->Info ->URL Type ->把申请的aped注册一下(如果没有这一步将不能从微信返回原来的app)
Identifier填工程的Bundle Idetifier
URL Schemes 填在微信申请的aped
7->最后在plist文件中添加微信字段
重点内容
这时候运行一下,微信分享环境配置好了。
在AppDelegate.m文件
#import "AppDelegate.h"
#import "WXApi.h"
@interface AppDelegate ()<WXApiDelegate>
//被废弃的方法. 但是在低版本中会用到.建议写上
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [WXApi handleOpenURL:url delegate:self];
}
//被废弃的方法. 但是在低版本中会用到.建议写上
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [WXApi handleOpenURL:url delegate:self];
}
//新的方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
return [WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[WXApi registerApp:@"wxbbf0646591e4a6d0" withDescription:@"测试"];
return YES;
}
- (void)onResp:(BaseResp *)resp
{
/*
WXSuccess = 0, 成功
WXErrCodeCommon = -1, 普通错误类型
WXErrCodeUserCancel = -2, 用户点击取消并返回
WXErrCodeSentFail = -3, 发送失败
WXErrCodeAuthDeny = -4, 授权失败
WXErrCodeUnsupport = -5, 微信不支持
*/
NSString * strMsg = [NSString stringWithFormat:@"errorCode: %d",resp.errCode];
NSLog(@"strMsg: %@",strMsg);
NSString * errStr = [NSString stringWithFormat:@"errStr: %@",resp.errStr];
NSLog(@"errStr: %@",errStr);
NSString * strTitle;
//判断是微信消息的回调 --> 是支付回调回来的还是消息回调回来的.
if ([resp isKindOfClass:[SendMessageToWXResp class]])
{
// 判断errCode 进行回调处理
if (resp.errCode == 0)
{
strTitle = [NSString stringWithFormat:@"分享成功"];
}
}
//发出通知 从微信回调回来之后,发一个通知,让请求支付的页面接收消息,并且展示出来,或者进行一些自定义的展示或者跳转
NSNotification * notification = [NSNotification notificationWithName:@"WXShare" object:resp.errStr];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
实现控制器
#import "ViewController.h"
#import "WXApi.h"
#import "WechatAuthSDK.h"
#import "WXApiObject.h"
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"WXShare" object:nil];
[self basicSetting];
#pragma mark 文字类型分享
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(100, 100, 100, 100);
[button addTarget:self action:@selector(buttonClciked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
#pragma mark 图片类型分享
UIButton * button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.backgroundColor = [UIColor redColor];
button1.frame = CGRectMake(100, 210, 100, 100);
[button1 addTarget:self action:@selector(ButtonOneClciked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
UIButton * button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.backgroundColor = [UIColor redColor];
button2.frame = CGRectMake(100, 320, 100, 100);
[button2 addTarget:self action:@selector(buttonTwoClciked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
// 检查是否装了微信
if ([WXApi isWXAppInstalled])
{
}
}
- (void)getOrderPayResult:(NSNotification *)notification
{
// 注意通知内容类型的匹配
if (notification.object == 0)
{
NSLog(@"分享成功");
}
}
#pragma mark 文字类型分享
- (void)buttonClciked
{
/** SendMessageToWXReq 文字分享内容的类
1. text 文字分享的内容
2. bText 发送消息的类型
3. scene 发送的目标场景
*/
SendMessageToWXReq * req = [[SendMessageToWXReq alloc] init];
req.text = @"https://www.wenqian.cn";
req.bText = YES;
req.scene = WXSceneSession;
[WXApi sendReq:req];
}
#pragma mark 图片类型分享
- (void)ButtonOneClciked
{
/** WXMediaMessage 多媒体分享的类
1. setThumbImage 设置缩略图
*/
WXMediaMessage * message = [WXMediaMessage message];
[message setThumbImage:[UIImage imageNamed:@"black"]];
WXImageObject * imageObject = [WXImageObject object];
NSString * filePath = [[NSBundle mainBundle] pathForResource:@"seeall@1x" ofType:@"png"];
imageObject.imageData = [NSData dataWithContentsOfFile:filePath];
message.mediaObject = imageObject;
SendMessageToWXReq * req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneSession;
[WXApi sendReq:req];
}
#pragma mark 网页类型分享
- (void)buttonTwoClciked
{
WXMediaMessage * message = [WXMediaMessage message];
message.title = @"文签网络科技";
message.description = @"仝伟";
[message setThumbImage:[UIImage imageNamed:@"1024"]];
WXWebpageObject * webpageObject = [WXWebpageObject object];
webpageObject.webpageUrl = @"https://www.wenqian.cn";
message.mediaObject = webpageObject;
SendMessageToWXReq * req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneSession;
[WXApi sendReq:req];
}
#pragma mark - 实现方法
#pragma mark 基本设置
- (void)basicSetting
{
self.title = @"测试";
}
到此微信分享基本结束,运行可以把内容分享出去。