国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 互联网 > iOS项目社会化分享-微信分享,朋友圈分享

iOS项目社会化分享-微信分享,朋友圈分享

来源:程序员人生   发布时间:2014-11-06 09:14:56 阅读次数:2206次

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


1. TARGETS - Info - URL Types 

identifier -> weixin

URL Schemes ->  利用id


2.在AppDelegate.h 引入头文件 

<pre name="code" class="objc">#import "WXApi.h" { /** * WXSceneSession 分享到会话 * WXSceneTimeline 分享到朋友圈 * WXSceneFavorite 分享到我的收藏 */ enum WXScene _scene; }


3.在AppDelegate.m

- (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]; }
//<span style="font-family: Arial, Helvetica, sans-serif;">onReq 和 </span><span style="font-family: Arial, Helvetica, sans-serif;">onResp 可以不写</span><span style="font-family: Arial, Helvetica, sans-serif;"> </span>-(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:@"标题:%@ 内容:%@ 附带信息:%@ 缩略图:%u bytes ", 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]; }









生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生