iOS 添加微信分享

微信官方文档:http://open.weixin.qq.com/document/sdk/ios/index.html

微信SDK下载:http://open.weixin.qq.com/download/?lang=zh_CN

 

1、将SDK文件中包含的libWeChatSDK.a,WXApi.h, WXApiObject.h三个文件添加到你所建的工程中。

 

2、选择你的工程设置项,选中“TARGETS”一栏,在“info”标签栏的“URL type“添加“URL scheme”为你所注册的应用程序id

iOS 添加微信分享

 

3、在 AppDelegate 中注册应用id,并添加微信的代理

 

//  AppDelegate.h

#import <UIKit/UIKit.h>
#import "WXApi.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>

@property (strong, nonatomic) UIWindow *window;

@end
//  AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [WXApi registerApp:@"*********"]; //写上注册的应用id
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    return YES;
}

 

4、然后我们就可以使用微信的api进行分享了。为了方便,我参考同事的代码写了个工具类,在需要分享的时候直接调用就可以了(只写了基本的,分享的时候定制界面啥的自己在这上面*发挥吧!)

 

//先引入:  #import"ShareToolViewController.h"       并加上代理:<ShareToolViewControllerDelegate>

    NSString *imageURL=@"http://a1005.phobos.apple.com/us/r1000/081/Purple/e5/d6/73/mzl.bklrzcol.320x480-75.jpg";
    NSString *title=@"Pocket League Story";
    NSString *detailInfo=@"一款非常不错的物理益智游戏,《拯救橘子》益智Cover Or突然来了的追捧。";
    
    ShareToolViewController *shareToolViewController = [[ShareToolViewController alloc]
                                                        initWithNibName:@"ShareToolViewController" bundle:nil];
    shareToolViewController.delegate = self;
    [self addChildViewController:shareToolViewController];
    [shareToolViewController initWhithTitle:title detailInfo:detailInfo image:nil imageUrl:imageURL];
    [self.view addSubview:shareToolViewController.view];

 

下面是这个类的具体内容:

//  ShareToolViewController.h

#import <UIKit/UIKit.h>

typedef enum {
    kShareTool_WeiXinFriends = 0, // 微信好友
    kShareTool_WeiXinCircleFriends, // 微信朋友圈
} ShareToolType;

@protocol ShareToolViewControllerDelegate <NSObject>

@end

@interface ShareToolViewController : UIViewController<UIActionSheetDelegate>
{
    
}

@property (nonatomic, retain)NSString *shareTitle;
@property (nonatomic, retain)NSString *detailInfo;
@property (nonatomic, retain)UIImage *shareImage;
@property (nonatomic, retain)NSString *shareImageURL;


@property (nonatomic, assign)id<ShareToolViewControllerDelegate> delegate;


- (void)initWhithTitle:(NSString *)title detailInfo:(NSString*)detailInfo
                 image:(UIImage *)image imageUrl:(NSString *)imageUrl;

@end

 

//  ShareToolViewController.m


#import "ShareToolViewController.h"

#import "WXApi.h"

@interface ShareToolViewController ()

@end

@implementation ShareToolViewController
@synthesize shareTitle  = _shareTitle;
@synthesize detailInfo = _detailInfo;
@synthesize shareImage = _shareImage;
@synthesize shareImageURL = _shareImageURL; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    CGRect mainScreenBounds = [[UIScreen mainScreen] bounds];
    self.view.frame = mainScreenBounds;
}

#pragma mark - 分享
- (void)initWhithTitle:(NSString *)title detailInfo:(NSString*)info
                 image:(UIImage *)image imageUrl:(NSString *)imageUrl{
    _shareTitle = title;
    _detailInfo = info;
    _shareImage = image;
    _shareImageURL = imageUrl;
    UIActionSheet* actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:nil
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:nil
                                  otherButtonTitles:@"通过微信好友分享",@"通过微信朋友圈分享",nil];
    [actionSheet showInView:self.view];
}

#pragma mark - UIActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0: //通过微信好友分享
            [self shareInformationWithType:kShareTool_WeiXinFriends];
            break;
        case 1: //通过微信朋友圈分享
            [self shareInformationWithType:kShareTool_WeiXinCircleFriends];
            break;
        default:
            break;
    }
}

- (void)shareInformationWithType:(ShareToolType)shareToolType {
    switch (shareToolType) {
        case kShareTool_WeiXinFriends:{
            WXImageObject *imgObj = [WXImageObject object];
            imgObj.imageUrl = _shareImageURL;
            
            WXWebpageObject *webObj = [WXWebpageObject object];
            webObj.webpageUrl = _shareImageURL;
            
            WXMediaMessage *message = [WXMediaMessage message];
            message.title = _shareTitle;
            message.description = _detailInfo;
            message.mediaObject = webObj;
            
            UIImage *desImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_shareImageURL]]];
            UIImage *thumbImg = [self thumbImageWithImage:desImage limitSize:CGSizeMake(150, 150)];
            message.thumbData = UIImageJPEGRepresentation(thumbImg, 1);
//            NSLog(@"%@,%d",thumbImg,message.thumbData.length);
            
            SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
            req.scene = WXSceneSession;
            req.bText = NO;
            req.message = message;
            [WXApi sendReq:req];
            [self shareHasDone];
            break;
        }
        case kShareTool_WeiXinCircleFriends:{
            WXWebpageObject *webObj = [WXWebpageObject object];
            webObj.webpageUrl = _shareImageURL;
            
            WXMediaMessage *message = [WXMediaMessage message];
            message.title = _shareTitle;
            message.description = _detailInfo;
            message.mediaObject = webObj;
            
            UIImage *desImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_shareImageURL]]];
            UIImage *thumbImg = [self thumbImageWithImage:desImage limitSize:CGSizeMake(150, 150)];
            message.thumbData = UIImageJPEGRepresentation(thumbImg, 1);
//            NSLog(@"%@,%d",thumbImg,message.thumbData.length);
            
            SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
            req.scene = WXSceneTimeline;
            req.bText = NO;
            req.message = message;
            [WXApi sendReq:req];
            [self shareHasDone];
            break;
        }
        default:
            break;
    }
}
- (UIImage *)thumbImageWithImage:(UIImage *)scImg limitSize:(CGSize)limitSize
{
    if (scImg.size.width <= limitSize.width && scImg.size.height <= limitSize.height) {
        return scImg;
    }
    CGSize thumbSize;
    if (scImg.size.width / scImg.size.height > limitSize.width / limitSize.height) {
        thumbSize.width = limitSize.width;
        thumbSize.height = limitSize.width / scImg.size.width * scImg.size.height;
    }
    else {
        thumbSize.height = limitSize.height;
        thumbSize.width = limitSize.height / scImg.size.height * scImg.size.width;
    }
    UIGraphicsBeginImageContext(thumbSize);
    [scImg drawInRect:(CGRect){CGPointZero,thumbSize}];
    UIImage *thumbImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return thumbImg;
}
- (void)shareHasDone{
    self.shareImage = nil;
    self.shareTitle = nil;
    self.shareImageURL = nil;
    self.detailInfo = nil;
    
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

iOS 添加微信分享

上一篇:微信抢红包外挂


下一篇:TurboMail邮件系统新版本V7.0,和微信一起玩!