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

作者: LeeLom | 来源:发表于2017-07-28 14:18 被阅读182次

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

相关文章

网友评论

    本文标题:iOS 使用微信分享(Objective-C版)

    本文链接:https://www.haomeiwen.com/subject/pzmnkxtx.html