查看: 1788|回复: 0

[IOS开发教程] 微信分享

发表于 2017-1-9 12:00:01
知了云AD

参考文章网址:

http://www.jianshu.com/p/7a2f49c654df

1.申请APPKey

2.在AppDelegate.m文件中的代码示例

  1. #import "AppDelegate.h"
  2. #import "WXApi.h"
  3. @interface AppDelegate ()
  4. @end
  5. @implementation AppDelegate
  6. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  7. [WXApi registerApp:@"申请的APPkey"];
  8. return YES;
  9. }
复制代码

3.具体的方法实现

  1. #import "ViewController.h"
  2. #import "WXApi.h"
  3. @interface ViewController ()
  4. @property (nonatomic, strong) UIButton *button;
  5. @end
  6. @implementation ViewController
  7. - (void)viewDidLoad {
  8. [super viewDidLoad];
  9. self.button = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 100, 50)];
  10. self.button.backgroundColor = [UIColor blueColor];
  11. [self.button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
  12. [self.view addSubview:self.button];
  13. }
  14. - (void)buttonClick:(UIButton *) button{
  15. NSLog(@"点击");
  16. //创建发送对象实例
  17. SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
  18. sendReq.bText = NO;//不使用文本信息
  19. sendReq.scene = 0;//0 = 好友列表 1 = 朋友圈 2 = 收藏
  20. //创建分享内容对象
  21. WXMediaMessage *urlMessage = [WXMediaMessage message];
  22. urlMessage.title = @"我们";//分享标题
  23. urlMessage.description = @"大家一起过中秋";//分享描述
  24. [urlMessage setThumbImage:[UIImage imageNamed:@"test"]];//分享图片,使用SDK的setThumbImage方法可压缩图片大小
  25. //创建多媒体对象
  26. WXWebpageObject *webObj = [WXWebpageObject object];
  27. webObj.webpageUrl = @"https://www.baidu.com";//分享链接
  28. //完成发送对象实例
  29. urlMessage.mediaObject = webObj;
  30. sendReq.message = urlMessage;
  31. //发送分享信息
  32. [WXApi sendReq:sendReq];
  33. }
复制代码


回复

使用道具 举报