iOS 使用微信分享(Objective-C版)

1. 在微信开放平台中创建你应用


首先需要在微信开放平台中注册你的应用,取得微信给你的AppID。在分享的过程中,我们需要使用这个AppID。具体的注册步骤跟着微信的平台一步步走就好。审核的过程大概需要2-3天。审核通过后如下可以看到:

微信审核页面

2. 在你的应用中进行如下的操作


  • 第一:在你的应用中:TARGETS -> Build Phases -> Link Binary With Libraries添加如下的框架
    • 点击下方的 "+" 添加:
    libc++.tbd
    CoreTelephony.framework
    SystemConfiguration.framework
    libWeChatSDK.a
    
    • 点击下方的 "+" ,然后点击Add Other... ,按住cmd + Shift + G, 输入/usr/lib,点击Go
      搜索下面的两个库进行添加。
    libz.1.dylib
    libsqlite3.dylib
    
  • 第二:在你的AppDelegate.m文件中
    • 导入文件:#import "WXApi.h"
    • 在下面的函数中
      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      输入如下内容
    // 注册微信Api
    [WXApi registerApp:@"wx1234567890"]; //将这里的wx1234567890用你申请到的AppleID替换
    
  • 第三:用微信的官方文档发送请求就好。
    咳咳。为了方便使用,我自己进行了一个简单的二次封装,大家也可以直接拿来使用。

3. 使用封装好的文件到底有多简单!


觉得好用,来个赞好不好...

  • 第一:在BYRWechatShare页面中,下载
    BYRWechatShare.h; BYRWechatShare.m这两个文件,拖拽到你的Project当中;
  • 第二:在你需要使用分享的ViewController当中,先引入
    #import "BYRWechatShare.h"
    下面是使用的例子
    • 分享文字
    /**
     * 分享文字
     * content: 需要分享的文字内容
     * type: 0:分享给微信好友;1:分享到朋友圈
     **/
    + (void)shareToWechatWithText:(NSString *)content
                             type:(NSUInteger)type;
    
    在文件中调用:
    [BYRWechatShare shareToWechatWithText:@"LeeLom大帅比 哈哈" type:0];  
    
开个玩笑不要黑我.jpeg
  • 分享图片
  /**
 * 分享图片
 * image: 需要分享的图片
 * thumbImage: 缩略图
 * type: 0:分享给微信好友;1:分享到朋友圈
 **/
+ (void)shareToWechatWithImage:(UIImage *)image
                    thumbImage:(UIImage *)thumbImage
                          type:(NSUInteger)type;

调用的例子

  // image 和 thumbImage可以不一样
  UIImage *image = [UIImage imageNamed:@"test"];
  UIImage *thumbImage = [UIImage imageNamed:@"test"];
  [BYRWechatShare shareToWechatWithImage:image thumbImage:thumbImage type:0];
tupian.jpeg
  • 分享音乐
  /**
   * 分享音乐
   * title: 音乐标题
   * description: 音乐描述
   * thumbImage: 缩略图
   * musicUrl: 音乐url
   * musicDataUrl: 音乐数据url
   * type: 0:分享给微信好友;1:分享到朋友圈
   **/
  + (void)shareToWechatWithMusicTitle:(NSString *)title
                          description:(NSString *)description
                           thumbImage:(UIImage *)thumbImage
                             musicUrl:(NSString *)musicUrl
                         musicDataUrl:(NSString *)musicDataUrl
                                 type:(NSUInteger)type;

调用的例子

  UIImage *thumbImage = [UIImage imageNamed:@"test"];
  [BYRWechatShare shareToWechatWithMusicTitle:@"歌曲名"
                                  description:@"歌曲描述"
                                   thumbImage:thumbImage
                                     musicUrl:@"www.baidu.com" //替换你的URL
                                 musicDataUrl:@"www.baidu.com" //替换你的URL
                                         type:1];
歌曲.jpeg
  • 分享网页
  /**
   * 分享网页
   * title: 网页标题
   * description: 网页描述
   * thumbImage: 缩略图
   * webpageUrl: 网页url
   * type: 0:分享给微信好友;1:分享到朋友圈
   **/
  + (void)shareToWechatWithWebTitle:(NSString *)title
                        description:(NSString *)description
                         thumbImage:(UIImage *)thumbImage
                         webpageUrl:(NSString *)webpageUrl
                               type:(NSUInteger)type;
  [BYRWechatShare shareToWechatWithWebTitle:@"网页名字"
                                description:@"网页描述"
                                 thumbImage:thumbImage
                                 webpageUrl:@"www.baidu.com"
                                       type:1];
wangyemingzi.jpeg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,466评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • 前言:本篇文章目的在于梳理知识,巩固思想,学习总结。有什么好的建议,都可以留言。互相促进!总观,微信支付,也没心思...
    麦穗0615阅读 10,093评论 8 70
  • 注意:代码自己动手写,不要复制! GitHub 一、接入微信第三方登录准备工作。 移动应用微信登录是基于OAuth...
    大冲哥阅读 15,177评论 0 7
  • 盘点过和李连杰对阵的十大实力派高手,诸如于承惠、计春华、周比利、于荣光、甄子丹、邹兆龙等等。猛然间发现和杰哥合作的...
    电影聚焦阅读 1,072评论 5 8