微信分享前提:
1.需要成功在微信开发者平台注册了账号, 并取的对应的 appkey appSecret。
2. 针对iOS9 添加了微信的白名单,以及设置了 scheme url 。 这都可以参照上面的链接,进行设置好。
3. 分享不跳转的时候原因总结, 具体方法如下:
1. 首先检查下是否有向微信注册应用。
2. 分享参数是否拼接错误。 监听下面 isSuccess yes为成功, no为是否, 看看是否是分享的对象弄错了。 文本对象与多媒体对象只能选一种。
[objc] view plain
class="tracking-ad" data-mod="popu_168"> copy
- BOOL isSuccess = [WXApi sendReq:sentMsg];
[objc] view plain
copy
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-
-
- [WXApi registerApp:URL_APPID withDescription:@"wechat"];
- return YES;
- }
-
- -(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
-
-
-
- return [WXApi handleOpenURL:url delegate:self];
- }
-
-
- - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
- return [WXApi handleOpenURL:url delegate:self];
- }
-
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation{
- return [WXApi handleOpenURL:url delegate:self];
- }
微信分享的核心代码;
[objc] view plain
copy
- #pragma mark 微信好友分享
-
- -(void)isShareToPengyouquan:(BOOL)isPengyouquan{
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- UIImage *image = [UIImage imageNamed:@"消息中心 icon"];
- WXMediaMessage *message = [WXMediaMessage message];
- message.title = @"微信分享测试";
- message.description = @"微信分享测试----描述信息";
-
- message.thumbData = UIImagePNGRepresentation(image);
- [message setThumbImage:image];
-
-
- WXWebpageObject *ext = [WXWebpageObject object];
- ext.webpageUrl = @"http://www.baidu.com";
- message.mediaObject = ext;
- message.mediaTagName = @"ISOFTEN_TAG_JUMP_SHOWRANK";
-
- SendMessageToWXReq *sentMsg = [[SendMessageToWXReq alloc]init];
- sentMsg.message = message;
- sentMsg.bText = NO;
-
- if (isPengyouquan) {
- sentMsg.scene = WXSceneTimeline;
- }else{
- sentMsg.scene = WXSceneSession;
- }
-
-
-
- appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- appdelegate.wxDelegate = self; <span style="font-family: Arial, Helvetica, sans-serif;">
- BOOL isSuccess = [WXApi sendReq:sentMsg];
-
- }
完整的代码如下:
appDelegate.h
[objc] view plain
copy
- #import <UIKit/UIKit.h>
-
- @protocol WXDelegate <NSObject>
-
- -(void)loginSuccessByCode:(NSString *)code;
- -(void)shareSuccessByCode:(int) code;
- @end
-
- @interface AppDelegate : UIResponder <UIApplicationDelegate>
-
- @property (strong, nonatomic) UIWindow *window;
- @property (nonatomic, weak) id<WXDelegate> wxDelegate;
- @end
appDelegate.m
[objc] view plain
copy
- #import "AppDelegate.h"
- #import "WXApi.h"
-
- #define URL_APPID @"app id"
-
-
-
-
- @interface AppDelegate ()<WXApiDelegate>
-
-
- @end
-
-
-
- @implementation AppDelegate
-
-
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-
-
- [WXApi registerApp:URL_APPID withDescription:@"wechat"];
- return YES;
- }
-
- -(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
-
-
-
- return [WXApi handleOpenURL:url delegate:self];
- }
-
-
- - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
- return [WXApi handleOpenURL:url delegate:self];
- }
-
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation{
- return [WXApi handleOpenURL:url delegate:self];
- }
-
- -(void) onResp:(BaseResp*)resp{
- NSLog(@"resp %d",resp.errCode);
-
-
- if ([resp isKindOfClass:[SendAuthResp class]]) {
- if (resp.errCode == 0) {
-
- if ([_wxDelegate respondsToSelector:@selector(loginSuccessByCode:)]) {
- SendAuthResp *resp2 = (SendAuthResp *)resp;
- [_wxDelegate loginSuccessByCode:resp2.code];
- }
- }else{
- NSLog(@"error %@",resp.errStr);
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登录失败" message:[NSString stringWithFormat:@"reason : %@",resp.errStr] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];
- [alert show];
- }
- }
-
- if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
- SendMessageToWXResp *response = (SendMessageToWXResp *)resp;
- NSLog(@"error code %d error msg %@ lang %@ country %@",response.errCode,response.errStr,response.lang,response.country);
-
- if (resp.errCode == 0) {
-
- if (_wxDelegate) {
- if([_wxDelegate respondsToSelector:@selector(shareSuccessByCode:)]){
- [_wxDelegate shareSuccessByCode:response.errCode];
- }
- }
- }else{
- NSLog(@"error %@",resp.errStr);
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"reason : %@",resp.errStr] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];
- [alert show];
- }
- }
- } @end
ViewController.h
[objc] view plain
copy
- #import <UIKit/UIKit.h>
-
- @interface ViewController : UIViewController
-
-
- @end
ViewController.m
[objc] view plain
copy
- #import "ViewController.h"
- #import "WXApi.h"
- #import "AppDelegate.h"
- #define URL_APPID @"appid "
- #define URL_SECRET @"app secret"
- #import "AFNetworking.h"
- @interface ViewController ()<WXDelegate>
- {
- AppDelegate *appdelegate;
- }
- @end
-
- @implementation ViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- }
- #pragma mark 微信登录
- - (IBAction)weixinLoginAction:(id)sender {
-
- if ([WXApi isWXAppInstalled]) {
- SendAuthReq *req = [[SendAuthReq alloc]init];
- req.scope = @"snsapi_userinfo";
- req.openID = URL_APPID;
- req.state = @"1245";
- appdelegate = [UIApplication sharedApplication].delegate;
- appdelegate.wxDelegate = self;
-
- [WXApi sendReq:req];
- }else{
-
- }
- }
- #pragma mark 微信登录回调。
- -(void)loginSuccessByCode:(NSString *)code{
- NSLog(@"code %@",code);
- __weak typeof(*&self) weakSelf = self;
-
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- manager.requestSerializer = [AFJSONRequestSerializer serializer];
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
- manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json",@"text/plain", nil nil];
-
- [manager GET:[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",URL_APPID,URL_SECRET,code] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
-
- } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
-
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
- NSLog(@"dic %@",dic);
-
-
- NSString* accessToken=[dic valueForKey:@"access_token"];
- NSString* openID=[dic valueForKey:@"openid"];
- [weakSelf requestUserInfoByToken:accessToken andOpenid:openID];
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- NSLog(@"error %@",error.localizedFailureReason);
- }];
-
- }
-
- -(void)requestUserInfoByToken:(NSString *)token andOpenid:(NSString *)openID{
-
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- manager.requestSerializer = [AFJSONRequestSerializer serializer];
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
- [manager GET:[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",token,openID] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
-
- } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- NSDictionary *dic = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
-
- NSLog(@"login success dic ==== %@",dic);
-
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- NSLog(@"error %ld",(long)error.code);
- }];
- }
-
- #pragma mark 微信好友分享
- - (IBAction)weixinShareAction:(id)sender {
- [self isShareToPengyouquan:NO];
-
- }
-
- #pragma mark 微信朋友圈分享
- - (IBAction)friendShareAction:(id)sender {
-
- [self isShareToPengyouquan:YES];
- }
- -(void)isShareToPengyouquan:(BOOL)isPengyouquan{
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- UIImage *image = [UIImage imageNamed:@"消息中心 icon"];
- WXMediaMessage *message = [WXMediaMessage message];
- message.title = @"微信分享测试";
- message.description = @"微信分享测试----描述信息";
-
- message.thumbData = UIImagePNGRepresentation(image);
- [message setThumbImage:image];
-
-
- WXWebpageObject *ext = [WXWebpageObject object];
- ext.webpageUrl = @"http://www.baidu.com";
- message.mediaObject = ext;
- message.mediaTagName = @"ISOFTEN_TAG_JUMP_SHOWRANK";
-
- SendMessageToWXReq *sentMsg = [[SendMessageToWXReq alloc]init];
- sentMsg.message = message;
- sentMsg.bText = NO;
-
- if (isPengyouquan) {
- sentMsg.scene = WXSceneTimeline;
- }else{
- sentMsg.scene = WXSceneSession;
- }
-
-
-
- appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- appdelegate.wxDelegate = self;
- BOOL isSuccess = [WXApi sendReq:sentMsg];
-
-
-
-
- }
-
- #pragma mark 监听微信分享是否成功 delegate
- -(void)shareSuccessByCode:(int)code{
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"分享成功" message:[NSString stringWithFormat:@"reason : %d",code] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];
- [alert show];
- }
-
-
-
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
-
- }
-
- @end
源码下载地址:http://www.jinhusns.com/Products/Download