标签:
在我们IOS软件开发当中我们有事需要分享一些图片视屏和文档之类的东西。但是有可能用的是别的软件,这时候我们就需要学习分享。
我们举个分享微信的例子好了:
看代码
先看AppDelegate.m文件内容
在AppDelegate.m里面我们需要
[WXApi registerApp:@"wx252e7c172102f9cb" withDescription:@"微信分享 1.0"];
才能够分享微信
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. /* 只有写了这一句话才能够在具体的地方实现分享功能 wx252e7c172102f9cb 这个项目向微信注册的开发者账号 微信分享 1.0 */ [WXApi registerApp:@"wx252e7c172102f9cb" withDescription:@"微信分享 1.0"]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; ViewController * tempVC = [[ViewController alloc] init]; self.window.rootViewController = tempVC; [self.window makeKeyAndVisible]; return YES; }
再看控制器
.h文件
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property(nonatomic, strong) UIButton *wechatFriendBtn; @property(nonatomic, strong) UIButton *wechatFriendsBtn; @end
.m文件
#import "ViewController.h" #import "WXApi.h" @interface ViewController () @end #define kLoadImage(name, type) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:type]] #define ScreenWidth self.view.frame.size.width #define ScreenHeight self.view.frame.size.height @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _wechatFriendBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _wechatFriendBtn.frame = CGRectMake(ScreenWidth / 2 - 120, ScreenHeight / 2 - 120, 80, 80); [_wechatFriendBtn setShowsTouchWhenHighlighted:YES]; [_wechatFriendBtn setBackgroundImage:kLoadImage(@"btn_微信好友", @"png") forState:UIControlStateNormal]; _wechatFriendBtn.enabled = YES; _wechatFriendBtn.tag = 1; [_wechatFriendBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_wechatFriendBtn]; _wechatFriendsBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _wechatFriendsBtn.frame = CGRectMake(ScreenWidth / 2 + 40, ScreenHeight / 2 - 120, 80, 80); [_wechatFriendsBtn setShowsTouchWhenHighlighted:YES]; [_wechatFriendsBtn setBackgroundImage:kLoadImage(@"btn_朋友圈", @"png") forState:UIControlStateNormal]; _wechatFriendsBtn.tag = 2; [_wechatFriendsBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_wechatFriendsBtn]; } -(void)btnClicked:(UIButton *)btn { switch (btn.tag) { case 1: { //这种分享是带文字和图片的分享,属于网页分享,注意分享的图片大小不能超过32k。 WXMediaMessage *message = [WXMediaMessage message]; //分享内容的标题 message.title = @"分享的标题"; //分享内容的描述 message.description = @"分享的介绍"; //分享的图片 [message setThumbImage:kLoadImage(@"share", @"png")]; //分享链接 WXWebpageObject *ext = [WXWebpageObject object]; ext.webpageUrl = @"www.sougou.com"; message.mediaObject = ext; SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init]; /* 发送消息的类型,包括文本消息和多媒体消息两种,两者只能选择其一,不能同时发送文本和多媒体消息 */ req.bText = NO; req.message = message; //这个属性表示分享到好友 req.scene = WXSceneSession; [WXApi sendReq:req]; //这种分享的是纯文字的信息。 // SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init]; // req.text = _linkStr; // req.bText = YES; // req.scene = WXSceneSession; // [WXApi sendReq:req]; } break; case 2: { WXMediaMessage *message = [WXMediaMessage message]; message.title = @"分享的标题"; message.description = @"分享的介绍"; [message setThumbImage:kLoadImage(@"share", @"png")]; WXWebpageObject *ext = [WXWebpageObject object]; ext.webpageUrl = @"www.baidu.com"; message.mediaObject = ext; SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init]; req.bText = NO; req.message = message; //这个属性表示分享到朋友圈 req.scene = WXSceneTimeline; [WXApi sendReq:req]; // SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init]; // req.text = _linkStr; // req.bText = YES; // req.scene = WXSceneTimeline; // [WXApi sendReq:req]; } break; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
标签:
原文地址:http://www.cnblogs.com/YU411524/p/4955427.html