码迷,mamicode.com
首页 > 微信 > 详细

微信朋友圈api使用

时间:2014-12-29 15:24:17      阅读:865      评论:0      收藏:0      [点我收藏+]

标签:分享

1、去微信开发公共开发平台注册个应用(获取APPid)

2、把微信api导入工程中(这三个文件 libWeChatSDK.a 、 WXApiObject.h、 WXApi.h)

3、导入相关的库文件(官方文档有详细信息)编译出错的话检查导入libc++.dylib这个库没有

4、在appdelegate文件中导入#import “wxapi.h”文件

5、程序注册(以下几个方法在appdelegate中实现的几个方法)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
  BOOL success = [WXApi registerApp:APPID];
    if (success) {
        NSLog(@"成功");
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"分享不成功" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
        [alert show];
    }
    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    return [WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    return [WXApi handleOpenURL:url delegate:self];
}
- (void)onReq:(BaseReq *)req{
    
}
- (void)onResp:(BaseResp *)resp{
    if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
        NSString *message = nil;
        switch (resp.errCode) {
            case 0:
                message = @"分享成功";
                break;
                case -1:
                message = @"分享失败";
                
            default:
                break;
        }
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
        [alert show];
    }
}

6、在需要实现分享的地方创建
- (IBAction)goShare:(id)sender{
  //如果为纯文本信息的话,执行第一个if语句的判断
    if (originalImage == nil) {
//        [self showAlertWithTitle:@"提示" message:@"目前还没有照片"];
        _scene = WXSceneTimeline;
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc]init];
        req.text = @"这个相机怎么样";
        req.bText = YES;
        req.scene = _scene;
        [WXApi sendReq:req];
        return;
    }
    _scene = WXSceneTimeline;
    if (![WXApi isWXAppInstalled]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先安装微信客户端再进行分享" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }else if (![WXApi isWXAppSupportApi]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的微信客户端版本太低,请先升级后再进行分享" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
 //分享图片的话用这个
 WXMediaMessage *message = [WXMediaMessage message];
   //生成缩略图(缩略图的要求小于32k)
    UIImage *changeimage = [self scaleImage:originalImage toScale:0.1];
    
    [message setThumbImage:changeimage];
    
    NSData *imagedata = UIImageJPEGRepresentation(originalImage, 1);
    WXImageObject *ext = [WXImageObject object];
    ext.imageData = imagedata;
    
    message.description = @"这是一个微信分享";
    message.mediaObject = ext;
    message.mediaTagName = @"WECHAT_TAG_JUMP_APP";
    message.messageExt = @"这是第三方带的测试字段";
    message.messageAction = @"<action>dotalist</action>";
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.text = @"这个相机怎么样";
    req.message = message;
    req.scene = _scene;
    
    [WXApi sendReq:req];

}
//生成缩略图方法
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize));
    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height *scaleSize)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
                                UIGraphicsEndImageContext();
    return scaledImage;
    
}

这是一个基本的微信分享操作

微信朋友圈api使用

标签:分享

原文地址:http://blog.csdn.net/zhaoyya/article/details/42237683

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!