在android平台的微信分享后是ios平台的微信分享,ios分享也不复杂,原理和android 一样,js客户端代码调用oc代码。android和ios的截图分享到朋友圈没有给出来,在参数里多一个类型,需要截图分享到朋友圈的朋友可以参照下微信SDK的文档说明加入参数就可以了。
一、分享后的游戏图标
“share_icon”(这个图片名字代码里用,自己命名的)配置:点击➕add other把share_icon的图片加入资源
二、Object-C的函数头文件:
#import <Foundation/Foundation.h>
#import “WXApi.h”
@interface WXApiManager: NSObject<WXApiDelegate>
ger : NSObject<WXApiDelegate>
{
}
+(instancetype)sharedManager;
+(void)RegisterAppID:(NSString*)app_id;
+(void)SendAuthRequest:(NSString*)scope State:(NSString*)state;
+(BOOL)ShareToChatScene:(NSString*)content_txt;
+(BOOL)ShareToFriendCircle:(NSString*)content_txt;
+(BOOL)ShareLinkToChatScene:(NSString*)content_link Title:(NSString*)title_txt Description:(NSString*)desc_txt;
+(BOOL)ShareLinkToFriendCircle:(NSString*)content_link Title:(NSString*)title_txt Description:(NSString*)desc_txt;
+(BOOL)ShareImageToChatScene:(NSString*)path;
+(BOOL)ShareImageToChatScene:(NSString*)file_path Title:(NSString*)title_txt Description:(NSString*)desc_txt;
+(void)StartPay:(NSString* )partner_id PrepayId:(NSString*)prepay_id NonceStr:(NSString*)nonce_str TimeStamp:(UInt32)timestamp Sign:(NSString*)sign;
+(BOOL)CheckWXInstalled;
+(NSString *) getIPWithHostName:(const NSString *)hostName;
+(UIImage*)scaleToSize:(CGSize)size Target:(UIImage*)target_img;
@end
三、JS调用OC
jsb.reflection.callStaticMethod(“oc类”, “函数名”, “参数”)
四、IOS分享链接给好友
JS代码:
// 分享到好友聊天
// @share_link 分享链接
// @title 标题
// @desc 分享内容
ShareToFriend:function(share_link, title, desc){
this.Log(“ShareToFriend”)
if(cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS){
jsb.reflection.callStaticMethod(IOS_CLASS_NAME,"ShareLinkToChatScene:Title:Description:", share_link, title, desc)
} else if(cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
var _type = 1 // 1:聊天分享 2:朋友圈分享
var _methond_name = “ShareLinkToWeChat”
var _methond_singnature = "(" + JAVA_STRING + JAVA_STRING + JAVA_STRING +"I)Z”
jsb.reflection.callStaticMethod(ANDROID_CLASS_NAME, _methond_name, _methond_singnature, share_link, title, desc, _type)
}
},
OC代码:
+(BOOL)ShareLinkToChatScene:(NSString*)content_link Title:(NSString*)title_txt Description:(NSString*)desc_txt
{
WXWebpageObject *ext = [WXWebpageObject object];
ext.webpageUrl = content_link;
UIImage *thumbImage = [UIImage imageNamed:@"share_icon.png”];
WXMediaMessage *message = [WXMediaMessage message];
message.title = title_txt;
message.description = desc_txt;
message.mediaObject = ext;
message.messageExt = nil;
message.messageAction = nil;
message.mediaTagName = @“WECHAT_TAG_SHARELINK_CS”;
[message setThumbImage:thumbImage];
SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init] autorelease];
req.bText = false;
req.scene = WXScene::WXSceneSession;
req.message = message;
//curShareType = 1701;
return [WXApi sendReq:req];
}
五、IOS分享链接到朋友圈
JS代码:
// 分享到朋友圈
// @share_link 分享链接
// @title 标题
// @desc 分享内容
ShareToFriendCircle:function(share_link, title, desc){
this.Log(“ShareToFriendCircle”)
if(cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS){
jsb.reflection.callStaticMethod(IOS_CLASS_NAME,"ShareLinkToFriendCircle:Title:Description:", share_link, title, desc)
} else if(cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
var _type = 2 // 1:聊天分享 2:朋友圈分享
var _methond_name = “ShareLinkToWeChat”
var _methond_singnature = "(" + JAVA_STRING + JAVA_STRING + JAVA_STRING +"I)Z”
jsb.reflection.callStaticMethod(ANDROID_CLASS_NAME, _methond_name, _methond_singnature, share_link, title, desc, _type)
}
},
OC代码:
+(BOOL)ShareLinkToFriendCircle:(NSString*)content_link Title:(NSString*)title_txt Description:(NSString*)desc_txt
{
WXWebpageObject *ext = [WXWebpageObject object];
ext.webpageUrl = content_link;
UIImage *thumbImage = [UIImage imageNamed:@"share_icon.png”];
WXMediaMessage *message = [WXMediaMessage message];
message.title = title_txt;
message.description = desc_txt;
message.mediaObject = ext;
message.messageExt = nil;
message.messageAction = nil;
message.mediaTagName = @“WECHAT_TAG_SHARELINK_FC”;
[message setThumbImage:thumbImage];
SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init] autorelease];
req.bText = false;
req.scene = WXScene::WXSceneTimeline;
req.message = message;
//curShareType = 1702;
return [WXApi sendReq:req];
}
六、IOS分享文字内容给好友
JS代码:
// 微信文字内容分享
// @text 分析内容
ShareTextTotFriends:function(text){
if(cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS){
jsb.reflection.callStaticMethod(IOS_CLASS_NAME,"ShareToChatScene:", text)
} else if(cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
var _type = 1
var _methond_name = “ShareToWeChat”
var _methond_singnature = "("+ JAVA_STRING +"I)Z”
jsb.reflection.callStaticMethod(ANDROID_CLASS_NAME, _methond_name, _methond_singnature, text, _type)
}
},
OC代码:
+(BOOL)ShareToChatScene:(NSString*)content_txt
{
SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init] autorelease];
req.bText = true;
req.scene = WXScene::WXSceneSession;
req.text = content_txt;
return [WXApi sendReq:req];
}
七、IOS分享文字内容到朋友圈
JS代码:
// 微信文字内容分享到朋友圈
// @text 分析内容
ShareTextTotFriendCircle:function(text){
if(cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS){
jsb.reflection.callStaticMethod(IOS_CLASS_NAME,"ShareToFriendCircle:", text)
} else if(cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
var _type = 2
var _methond_name = “ShareToWeChat”
var _methond_singnature = "("+ JAVA_STRING +"I)Z”
jsb.reflection.callStaticMethod(ANDROID_CLASS_NAME, _methond_name, _methond_singnature, text, _type)
}
},
OC代码:
+(BOOL)ShareToFriendCircle:(NSString*)content_txt
{
SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init] autorelease];
req.bText = true;
req.scene = WXScene::WXSceneTimeline;
req.text = content_txt;
return [WXApi sendReq:req];
}
八、IOS截图分享给好友
JS代码:
// 微信截图分享
// @share_path 截图的路径
ShareImageToWeChat:function(share_path){
this.Log(“ShareImageToWeChat”)
if(cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS){
jsb.reflection.callStaticMethod(IOS_CLASS_NAME,"ShareImageToChatScene:", share_path)
} else if(cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
var _methond_name = “ShareImageToWeChat”
var _methond_singnature = "("+ JAVA_STRING +”)Z”
var _bShareResult = jsb.reflection.callStaticMethod(ANDROID_CLASS_NAME, _methond_name, _methond_singnature, share_path)
if(_bShareResult){
this.Log("wechat share image success”)
} else {
this.Log("wechat share image failed”)
}
}
},
// 截图分享
shareCaptureScreen: function () {
//注意,EditBox,VideoPlayer,Webview 等控件无法被包含在截图里面
//因为这是 OpenGL 的渲染到纹理的功能,上面提到的控件不是由引擎绘制的
if(CC_JSB) {
//如果待截图的场景中含有 mask,请使用下面注释的语句来创建 renderTexture
var renderTexture = cc.RenderTexture.create(1280,720, cc.Texture2D.PIXEL_FORMAT_RGBA8888, gl.DEPTH24_STENCIL8_OES);
var self = this
//实际截屏的代码
renderTexture.begin();
cc.director.getScene()._sgNode.visit();
renderTexture.end();
renderTexture.saveToFile("wechat_share_image_screenshot.png",cc.ImageFormat.PNG, true, function () {
// 调用微信sdk接口
var _share_path = jsb.fileUtils.getWritablePath()
self.ShareImageToWeChat(_share_path)
cc.log("path:" + _share_path)
});
}
},
OC代码:
+(BOOL)ShareImageToChatScene:(NSString*)path
{
cocos2d::log("begin share image”);
NSString *full_img_path = [path stringByAppendingString:@"wechat_share_image_screenshot.png”];
UIImage *temp_img = [UIImage imageWithContentsOfFile:full_img_path];
UIImage *thumbImage = [WXApiManager scaleToSize:CGSizeMake(320, 320) Target:temp_img];
// 多媒体消息中包含的图片数据对象
WXImageObject *imageObject = [WXImageObject object];
imageObject.imageData = [NSData dataWithContentsOfFile:full_img_path];
WXMediaMessage *message = [WXMediaMessage message];
//message.title = title_txt;
message.mediaObject = imageObject;
message.messageExt = nil;
message.messageAction = nil;
message.mediaTagName = @“WECHAT_TAG_SHARELINK_FC”;
[message setThumbImage:thumbImage];
SendMessageToWXReq *req = [[[SendMessageToWXReq alloc] init] autorelease];
req.bText = false;
req.scene = WXScene::WXSceneSession;
req.message = message;
//curShareType = 1705;
return [WXApi sendReq:req];
}
//等比例缩放
+(UIImage*)scaleToSize:(CGSize)size Target:(UIImage*)target_img
{
CGFloat width = target_img.size.width;
CGFloat height = target_img.size.height;
float verticalRadio = size.height*1.0/height;
float horizontalRadio = size.width*1.0/width;
float radio = 1;
if(verticalRadio>1 && horizontalRadio>1)
{
radio = verticalRadio > horizontalRadio ? horizontalRadio : verticalRadio;
}
else
{
radio = verticalRadio < horizontalRadio ? verticalRadio : horizontalRadio;
}
width = width*radio;
height = height*radio;
int xPos = (size.width - width)/2;
int yPos = (size.height-height)/2;
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[target_img drawInRect:CGRectMake(xPos, yPos, width, height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}
网友评论