iOS 添加微信分享
2015-01-30 14:36
274 查看
微信官方文档: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
3、在 AppDelegate 中注册应用id,并添加微信的代理
4、然后我们就可以使用微信的api进行分享了。为了方便,我参考同事的代码写了个工具类,在需要分享的时候直接调用就可以了(只写了基本的,分享的时候定制界面啥的自己在这上面自由发挥吧!)
//先引入: #import"ShareToolViewController.h"
并加上代理:<ShareToolViewControllerDelegate>
下面是这个类的具体内容:
微信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
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
相关文章推荐
- 使用ShareSDK for cocos2dx为IOS端添加微信登陆分享功能
- iOS 添加微信分享
- iOS 添加微信分享sdk流程
- 解决iOS app集成分享到QQ空间,微信好友,朋友圈等功能,苹果审核无法通过的问题
- 微信分享代码之在网页中添加“分享到微信朋友圈”按钮的代码分享
- 如何为iOS app添加AirDrop文件分享功能
- ios微信分享---按照官方文档上配置缺不能正常启动微信客户端
- ios 关于微信获取手机里边分享过微信的app的想法
- iOS微信分享开发
- iOS:开放平台引用(二)--微信分享
- 在微信公众平台前端网页上添加分享到朋友圈,关注微信等按钮
- iOS学习代码下载—ShareSDK分享到微信、微信朋友圈、QQ
- 前端网页上添加分享到朋友圈,关注微信等按钮
- 微信分享iOS接入指南
- iOS QQ、QQ空间、微信好友、微信朋友圈分享成功如何显示图文
- 网站链接打开以后左边的小图标添加和微信分享左边图标
- 如何为iOS app添加AirDrop文件分享功能
- 微信公众账号添加关注按钮和分享到朋友圈按钮.
- ios ,微信分享
- ios第三方分享到qq、微信、人人网、微博总结