首页 / IOS / iOS 添加微信分享
iOS 添加微信分享
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了iOS 添加微信分享,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6577字,纯文字阅读大概需要10分钟。
内容图文
微信官方文档: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,并添加微信的代理
// 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
原文:http://www.cnblogs.com/fakemessi/p/4901304.html
内容总结
以上是互联网集市为您收集整理的iOS 添加微信分享全部内容,希望文章能够帮你解决iOS 添加微信分享所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。