大锐哥的iOS开发教程(5)微信iOS SDK

iOS Dev (5) 微信iOS SDK

1 登记应用

登录微信开放平台网站,登记你的应用(前提是你得有QQ号),目前只支持iOS和Android平台。然后会得到一个AppID,需要在你自己的iOS或Android应用中用到,

2 API

Weixin iOS SDK主要有以下三个文件:

  • libWeChatSDK.a
  • WXApp.h
  • WXAppObject.h

Weixin iOS SDK可以发送以下几种内容:

  • 文本
  • Photo
  • Video
  • App内容
  • News
  • Music
  • GIF

Weixin iOS SDK提供了两种发送方式:

  • 发送消息给微信好友或群,一般称为发给session
  • 发送到朋友圈,一般称为发到timeline

以下说几个主要的基本API的用法。

init

- (id)init{
    if(self = [super init]){
        _scene = WXSceneSession;
    }
    return self;
}

doAuth

- (void)doAuth
{
    SendAuthReq* req = [[[SendAuthReq alloc] init] autorelease];
    req.scope = @"post_timeline";
    req.state = @"xxx";

    [WXApi sendReq:req];
}

sendTextContent

- (void) sendTextContent:(NSString*)nsText
{
    SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease];
    req.bText = YES;
    req.text = nsText;
    req.scene = _scene;

    [WXApi sendReq:req];
}

sendImageContent

- (void) sendImageContent
{
    //发送内容给微信
    WXMediaMessage *message = [WXMediaMessage message];
    [message setThumbImage:[UIImage imageNamed:@"res1thumb.png"]];

    WXImageObject *ext = [WXImageObject object];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res1" ofType:@"jpg"];
    ext.imageData = [NSData dataWithContentsOfFile:filePath] ;

    message.mediaObject = ext;

    SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease];
    req.bText = NO;
    req.message = message;
    req.scene = _scene;

    [WXApi sendReq:req];
}

3 Reference

相关推荐