首先去微信开放平台注册账号(是微信开放平台 不是腾讯开放平台,两者不一样)
https://open.weixin.qq.com
注册完成之后记得创建应用,后边会用到。只需要注册就行, 拿到AppID 就行,不用上传app
然后搭建环境
下载微信最新的SDK
然后倒入SDK中 libWeChatSDK.a, WXApi.h, WXApiObject.h 到工程 注意路径问题
添加依赖库 SystemConfiguration.framework, CoreTelephony.framework, libsqlite3.0.tbd, liz.tbd, libc++.tbd 这里注意libc++.tbd一定要添加,微信api中是没有的。
之后就是代码问题了
AppDelegate.h
#import "WXApi.h"
@interface AppDelegate :UIResponder <UIApplicationDelegate,WXApiDelegate>
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//向微信注册
[WXApiregisterApp:@"AppID"];
return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [WXApihandleOpenURL:urldelegate:self];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [WXApihandleOpenURL:urldelegate:self];
}
//分享完成回调
//这个方法一定要在这里写 否则不会执行
- (void)onResp:(BaseResp *)resp {
// WXSuccess = 0, /**<成功 */
// WXErrCodeCommon = -1, /**<普通错误类型 */
// WXErrCodeUserCancel = -2, /**<用户点击取消并返回 */
// WXErrCodeSentFail = -3, /**<发送失败 */
// WXErrCodeAuthDeny = -4, /**<授权失败 */
// WXErrCodeUnsupport = -5, /**<微信不支持 */
if (resp.errCode ==0) {
NSLog(@"微信发送成功");
//分享成功你要做的操作
} else {
//分享失败的操作
}
}
在 ViewController.m 中执行分享操作
/*
WXSceneSession = 0, 聊天界面
WXSceneTimeline = 1, 朋友圈
WXSceneFavorite = 2, 收藏
*/
1.文字类型
3.音乐类型
4.视频类型
5.网页类型
另外附上自定义弹出分享菜单
self.MessageButton=[[UIButtonalloc]initWithFrame:CGRectMake(100,100,100,30)];
[self.MessageButtonsetTitle:@"分享"forState:UIControlStateNormal];
[self.MessageButtonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
self.MessageButton.backgroundColor = [UIColor orangeColor];
self.MessageButton.tag = 101;
[self.viewaddSubview:self.MessageButton];
[self.MessageButtonaddTarget:selfaction:@selector(test:)forControlEvents:UIControlEventTouchDown];
self.infoView = [[UIViewalloc]initWithFrame:CGRectMake(0,SCREENHEIGHT,SCREENWIDTH,100)];
self.infoView.backgroundColor = [UIColorgreenColor];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 25, 50, 50);
[button setBackgroundImage:[UIImageimageNamed:@"friend"]forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(testMessagesAct)forControlEvents:UIControlEventTouchUpInside];
[self.infoViewaddSubview:button];
[self.viewaddSubview:self.infoView];
//分享到朋友圈
-(void)testMessagesAct
{
//网页型消息
WXMediaMessage *message = [WXMediaMessagemessage];
message.title = @"测试";
message.description = @"测试一次啊";
[message setThumbImage:[UIImageimageNamed:@"Icon@2x"]];
WXWebpageObject *web = [WXWebpageObjectobject];
web.webpageUrl =@"www.baidu.com";
message.mediaObject = web;
SendMessageToWXReq* req = [[SendMessageToWXReqalloc]init];
req.bText = NO;
req.message = message;
req.scene =WXSceneTimeline;//分享到朋友圈
[WXApi sendReq:req];
}
//弹出分享菜单按钮
- (void)test:(UIButton *)button {
if (button.tag ==101) {
[selfsetInfoViewFrame:YES];
button.tag = 102;
} else if (button.tag ==102) {
[selfsetInfoViewFrame:NO];
button.tag = 101;
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.MessageButton.tag = 101;
[selfsetInfoViewFrame:NO];
}
//自定义分享菜单
- (void)setInfoViewFrame:(BOOL)isDown{
if(isDown == NO) {
//滑动动画
[UIViewanimateKeyframesWithDuration:0.4delay:0options:UIViewKeyframeAnimationOptionRepeatanimations:^{
[UIViewsetAnimationRepeatCount:1];
[_infoView setFrame:CGRectMake(0,SCREENHEIGHT,SCREENWIDTH,100)];
} completion:^(BOOL finished) {
}];
}else {
[UIViewanimateKeyframesWithDuration:0.4delay:0options:UIViewKeyframeAnimationOptionRepeatanimations:^{
[UIViewsetAnimationRepeatCount:1];
[_infoView setFrame:CGRectMake(0,SCREENHEIGHT-100,SCREENWIDTH,100)];
} completion:^(BOOL finished) {
}];
}
}
检测微信是否安装
在info.plist里面添加LSApplicationQueriesSchemes(Array类型),然后插入weixin, wechat, mqq的string类型子项,如下图:
// 检测是否安装了微信
if
([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@
"weixin://"
]]) {
// 检测是否安装了QQ
if
([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@
"mqq://"
]]) {
}
一定要注意图片,视频,音频文件的大小,文件过大是不能调起微信的