统一登陆还是非常有必要的,安全,放心。代码
/*cordov 微信自己定义插件*/
(function (cordova) {
var define = cordova.define; define("cordova/plugin/wx", function (require, exports, module) {
var argscheck = require('cordova/argscheck'),
exec = require('cordova/exec');
exports.send_webpage = function (content, successCB, failCB) {
/*
content 数组 0--url 1--title 2--description
3--分享目的地(0--选择好友对话框 1--我的朋友圈 2--我的收藏) */
argscheck.checkArgs('AFF', 'wx.send_webpage', arguments);
if (!content) {
failCB && failCB("请输入网页信息");
} else {
exec(successCB, failCB, "WX", "send_webpage", content);
}
};
exports.send_img = function (content, successCB, failCB) {
argscheck.checkArgs('AFF', 'wx.send_img', arguments);
if (!content) {
failCB && failCB("请输入图片信息");
} else {
exec(successCB, failCB, "WX", "send_img", content);
}
};
exports.wxlogin = function (content, successCB, failCB) {
argscheck.checkArgs('AFF', 'wx.wxlogin', arguments);
if (!content) {
failCB && failCB("參数错误");
} else {
exec(successCB, failCB, "WX", "wxlogin", content);
}
};
exports.checkwx = function (content, successCB, failCB) {
argscheck.checkArgs('AFF', 'wx.checkwx', arguments);
if (!content) {
failCB && failCB("參数错误");
} else {
exec(successCB, failCB, "WX", "checkwx", content);
}
};
exports.checkQQ = function (content, successCB, failCB) {
argscheck.checkArgs('AFF', 'wx.checkQQ', arguments);
if (!content) {
failCB && failCB("參数错误");
} else {
exec(successCB, failCB, "WX", "checkQQ", content);
}
};
exports.checkSina = function (content, successCB, failCB) {
argscheck.checkArgs('AFF', 'wx.checkSina', arguments);
if (!content) {
failCB && failCB("參数错误");
} else {
exec(successCB, failCB, "WX", "checkSina", content);
}
};
});
cordova.addConstructor(function () {
if (!window.plugins) {
window.plugins = {};
}
console.log("将插件注入cordovawx...");
window.plugins.wx = cordova.require("cordova/plugin/wx");
console.log("wx注入结果:" + typeof (window.plugins.wx));
});
})(cordova);
android
public class WXPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException { // 分享url
if ("send_webpage".equals(action)) {
WXWebpageObject webpage = new WXWebpageObject();
// url
String webpageUrl = args.getString(0);
webpage.webpageUrl = webpageUrl;
WXMediaMessage msg = new WXMediaMessage(webpage);
// 标题
String title = args.getString(1);
msg.title = title;
// 描写叙述
String description = args.getString(2);
msg.description = description;
// Bitmap thumb = BitmapFactory.decodeResource(getResources(),
// R.drawable.send_music_thumb);
// msg.thumbData = Util.bmpToByteArray(thumb, true);
//
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("webpage");
req.message = msg;
// 0--选择好友对话框 1--我的朋友圈 2--我的收藏
int scene = args.getInt(3);
// isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline :
// SendMessageToWX.Req.WXSceneSession;
req.scene = scene;
Constants.api.sendReq(req);
callbackContext.success();
return true; } else if ("wxlogin".equals(action)) {
boolean isInstallWX = Constants.api.isWXAppInstalled();
if (isInstallWX) {
// send oauth request
SendAuth.Req req = new SendAuth.Req();
// req.openId = Constants.APP_ID;
req.scope = "snsapi_userinfo";
req.state = "wechat_zzy";
Constants.api.sendReq(req);
callbackContext.success();
return true;
}else{
String jsCode = "appCallBack.noInstallWX()";
yooshow.instance.ToJS(jsCode);
callbackContext.success();
return true;
}
}else if ("checkwx".equals(action)) {
boolean isInstallWX = Constants.api.isWXAppInstalled();
if (isInstallWX) {
callbackContext.success();
return true;
}else{
String jsCode = "appCallBack.checkWXInstall()";
yooshow.instance.ToJS(jsCode);
callbackContext.success();
return true;
}
} else if ("send_img".equals(action)) { // 分享img
return true;
} else {
return false;
}
} private String buildTransaction(final String type) {
return (type == null) ? String.valueOf(System.currentTimeMillis())
: type + System.currentTimeMillis();
}
}
public class WXEntryActivity extends Activity implements IWXAPIEventHandler { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Constants.api.handleIntent(getIntent(), this);
} @Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
Constants.api.handleIntent(getIntent(), this);
} @Override
public void onReq(BaseReq arg0) {
// TODO Auto-generated method stub } @Override
public void onResp(BaseResp resp) {
// TODO Auto-generated method stub
if(resp.getType()==ConstantsAPI.COMMAND_SENDAUTH){
String result = "0";
switch (resp.errCode) {
case BaseResp.ErrCode.ERR_OK:
result = "ok";
String code = ((SendAuth.Resp)resp).code;
//允许授权之后
String jsCode = "appCallBack.wxLogin('"+code+"')";
yooshow.instance.ToJS(jsCode);
break;
case BaseResp.ErrCode.ERR_USER_CANCEL:
result = "cancel";
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED:
result = "denied";
break;
default:
result = "unknown";
break;
} } else if(resp.getType()==ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX){
String result = "0";
switch (resp.errCode) {
case BaseResp.ErrCode.ERR_OK:
result = "ok";
boolean flag = ((SendMessageToWX.Resp)resp).checkArgs();
if(flag){
String jsCode = "appCallBack.wxShareSuccess()";
yooshow.instance.ToJS(jsCode);
} break;
case BaseResp.ErrCode.ERR_USER_CANCEL:
result = "cancel";
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED:
result = "denied";
break;
default:
result = "unknown";
break;
} }
finish();
}
}
<activity
android:name="注意包名!!! .WXEntryActivity"
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Translucent" >
<intent-filter>
<action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="sdksample" />
</intent-filter>
</activity>
iOS
#import <UIKit/UIKit.h>
#import <Cordova/CDV.h>
#import "WXApiObject.h" @interface CDVWX : CDVPlugin @property (nonatomic,copy) NSString*callbackID;
//Instance Method
-(void) send_webpage:(CDVInvokedUrlCommand*)command ;
-(void) send_img:(CDVInvokedUrlCommand*)command ;
-(void) checkwx:(CDVInvokedUrlCommand*)command ;
-(void) wxlogin:(CDVInvokedUrlCommand*)command ;
-(void) checkQQ:(CDVInvokedUrlCommand*)command ;
-(void) checkSina:(CDVInvokedUrlCommand*)command ;
@end
#import "CDVWX.h"
#import "WXApi.h"
#import "AppDelegate.h"
#import "QQConnection/ISSQQApp.h"
#import <ShareSDK/ShareSDK.h>
#import <SinaWeiboConnection/ISSSinaWeiboApp.h>
#import "TencentOpenAPI/QQApiInterface.h"
#import <TencentOpenAPI/TencentOAuth.h>
@implementation CDVWX
@synthesize callbackID;
-(void)send_webpage:(CDVInvokedUrlCommand *)command {
WXMediaMessage *message = [WXMediaMessage message];
//url
NSString* webpageUrl = [command.arguments objectAtIndex:0];
WXWebpageObject *ext = [WXWebpageObject object];
ext.webpageUrl = webpageUrl;
message.mediaObject = ext;
//title
NSString* title = [command.arguments objectAtIndex:1];
message.title =title;
//description
NSString* description = [command.arguments objectAtIndex:2];
message.description = description; SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
//(0--选择好友对话框 1--我的朋友圈 2--我的收藏)
// int scene = [command.arguments objectAtIndex:3]; int scene =[(NSNumber *)[command.arguments objectAtIndex:3] intValue];
req.scene = scene;
NSLog(@"fenxiang >>>>>> %@", title); NSLog(@"fenxiang >>>>>> %D", scene);
// [WXApi sendReq:req];
[[AppDelegate appDelegate] sendLinkContent:req]; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@""];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
-(void)send_img:(CDVInvokedUrlCommand *)command
{ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@""];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
-(void)checkwx:(CDVInvokedUrlCommand *)command
{
BOOL isInstallWX = [WXApi isWXAppInstalled];
if(isInstallWX){ }else{
NSString *js = @"appCallBack.checkWXInstall()";
[[AppDelegate appDelegate] runJS:js];
} CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@""];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
-(void)checkQQ:(CDVInvokedUrlCommand *)command
{
// id<ISSQQApp> appttt = (id<ISSQQApp>)[ShareSDK getClientIconWithType:ShareTypeQQSpace];
//
// BOOL isInstall = [appttt isClientInstalled ];
// BOOL isInstall = [QQApiInterface isQQInstalled];
// NSLog(@"qq >>>>>> %@", isInstall?@"yes":@"no");
if([QQApiInterface isQQInstalled]){ }else{
NSLog(@"未安装");
NSString *js = @"appCallBack.checkQQInstall()";
[[AppDelegate appDelegate] runJS:js];
} CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@""];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
-(void)checkSina:(CDVInvokedUrlCommand *)command
{
// id<ISSSinaWeiboApp> temp = (id<ISSSinaWeiboApp>)[ShareSDK getClientIconWithType:ShareTypeSinaWeibo];
//
// BOOL isInstall = [temp isClientInstalled];
// if(isInstall){
//
// }else{
// NSString *js = @"appCallBack.checkSinaInstall()";
// [[AppDelegate appDelegate] runJS:js];
// } CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@""];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
-(void)wxlogin:(CDVInvokedUrlCommand *)command
{
SendAuthReq* req = [[[SendAuthReq alloc] init] autorelease];
req.scope = @"snsapi_userinfo";
req.state = @"wechat_zzy"; [WXApi sendReq:req]; CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@""];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} @end
didFinishLaunchingWithOptions方法中
//向微信注冊
[WXApi registerApp:WX_ID];
-(void) onResp:(BaseResp*)resp
{ if([resp isKindOfClass:[SendAuthResp class]]){
SendAuthResp * aresp = (SendAuthResp *) resp;
if(aresp.errCode==0){
NSString* code= aresp.code;
NSString *js = [[NSString alloc]initWithFormat:@"appCallBack.wxLogin('%@')", code ];
[[AppDelegate appDelegate] runJS:js]; }
}else if ([resp isKindOfClass:[SendMessageToWXResp class]]){
SendMessageToWXResp * smtwxResp = (SendMessageToWXResp *) resp;
if(smtwxResp.errCode==0){
NSString *js = [[NSString alloc]initWithFormat:@"appCallBack.wxShareSuccess()"];
[[AppDelegate appDelegate] runJS:js]; }
}else if ([resp isKindOfClass:[SendMessageToQQResp class]]){
SendMessageToQQResp * smtwxResp = (SendMessageToQQResp *) resp; NSString *js = [[NSString alloc]initWithFormat:@"appCallBack.qqShareSuccess()"];
[[AppDelegate appDelegate] runJS:js]; } }
handleOpenURL方法中
[WXApi handleOpenURL:url delegate:self];
return YES;
iOS 中别忘加入 URL Types
最后,有问题找我