博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS - 社会化分享-微信分享,朋友圈分享
阅读量:7229 次
发布时间:2019-06-29

本文共 4194 字,大约阅读时间需要 13 分钟。

我仅仅做了文字和图片分享功能大笑

1. TARGETS - Info - URL Types 

identifier -> weixin

URL Schemes ->  应用id

2.在AppDelegate.h 引入头文件 

#import "WXApi.h"{/** *      WXSceneSession   分享到会话 *      WXSceneTimeline  分享到朋友圈 *      WXSceneFavorite  分享到我的收藏 */    enum WXScene _scene;}
 
- (id)init
{    if(self = [super init]){        _scene = WXSceneSession;    }    return self;}
-(void) changeScene:(NSInteger)scene{    _scene = scene;}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{	    // 其他代码    // 向微信注冊应用ID    [WXApi registerApp:@"xxooxoxoxoxoxoxo"];}#pragma mark - 重写AppDelegate的handleOpenURL和openURL方法- (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];}
onReq 和 onResp 能够不写-(void) onReq:(BaseReq*)req{    if([req isKindOfClass:[GetMessageFromWXReq class]])    {        // 微信请求App提供内容, 须要app提供内容后使用sendRsp返回        NSString *strTitle = [NSString stringWithFormat:@"微信请求App提供内容"];        NSString *strMsg   = @"微信请求App提供内容,App要调用sendResp:GetMessageFromWXResp返回给微信";                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];        alert.tag = 1000;        [alert show];        [alert release];    }    else if([req isKindOfClass:[ShowMessageFromWXReq class]])    {        ShowMessageFromWXReq* temp = (ShowMessageFromWXReq*)req;        WXMediaMessage *msg = temp.message;                //显示微信传过来的内容        WXAppExtendObject *obj = msg.mediaObject;                NSString *strTitle = [NSString stringWithFormat:@"微信请求App显示内容"];        NSString *strMsg   = [NSString stringWithFormat:@"标题:%@ \n内容:%@ \n附带信息:%@ \n缩略图:%u bytes\n\n", msg.title, msg.description, obj.extInfo, msg.thumbData.length];                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];        [alert show];        [alert release];    }    else if([req isKindOfClass:[LaunchFromWXReq class]])    {        //从微信启动App        NSString *strTitle = [NSString stringWithFormat:@"从微信启动"];        NSString *strMsg   = @"这是从微信启动的消息";                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];        [alert show];        [alert release];    }}-(void) onResp:(BaseResp*)resp{    if([resp isKindOfClass:[SendMessageToWXResp class]])    {        NSString *strTitle = [NSString stringWithFormat:@"发送媒体消息结果"];        NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];        [alert show];        [alert release];    }}

4.这是我写好的在微信会话和朋友圈分享文字或者图片的方法

直接调用就能够, 也能够按需求整到一起

#pragma mark - 微信, 朋友圈分享#pragma mark 文字分享- (void)sharedByWeChatWithText:(NSString *)WeChatMessage sceneType:(int)sceneType{    SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init]autorelease];    req.text  = WeChatMessage;    req.bText = YES;    req.scene = sceneType;    [WXApi sendReq:req];}#pragma mark 图片分享- (void)sharedByWeChatWithImage:(NSString *)imageName sceneType:(int)sceneType{    WXMediaMessage *message = [WXMediaMessage message];    [message setThumbImage:[UIImage imageNamed:imageName]];        WXImageObject *ext = [WXImageObject object];    NSString *filePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];    ext.imageData  = [NSData dataWithContentsOfFile:filePath];    UIImage *image = [UIImage imageWithData:ext.imageData];    ext.imageData  = UIImagePNGRepresentation(image);        message.mediaObject = ext;        SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init]autorelease];    req.bText   = NO;    req.message = message;    req.scene   = sceneType;        [WXApi sendReq:req];}
 
你可能感兴趣的文章
什么是死锁和如何解决死锁
查看>>
iOS:switch case中报错expected expression
查看>>
转 抽象类和接口联系与区别
查看>>
NSUserDefaults读取和写入自定义对象(转载)
查看>>
成员变量与静态变量的区别
查看>>
找出1-100中缺失的两个数
查看>>
Work with Unicode, CCSID & DBCS
查看>>
scanf()总结--从网上收来的,感觉很好,用来提醒自己,c语言真是博大精深!!【转】...
查看>>
Linux环境下段错误的产生原因及调试方法小结【转】
查看>>
Linux进程上下文切换过程context_switch详解--Linux进程的管理与调度(二十一)【转】...
查看>>
ubantu系统安装ssh
查看>>
js实现对上传图片的路径转成base64编码,并且对图片进行压缩,实现预览功能...
查看>>
LeetCode OJ:Multiply Strings (字符串乘法)
查看>>
LeetCode OJ:Search in Rotated Sorted Array(翻转排序数组的查找)
查看>>
VLC打开.264文件
查看>>
常用的一些webService
查看>>
Codeigniter开发技巧:连接多个数据库(可实现DB读写分离)
查看>>
Mybatis注意点之#与$区别
查看>>
IP地址分类(A类 B类 C类 D类 E类)
查看>>
RedirectResult,RedirectToRoute
查看>>