统一登陆还是非常有必要的,安全,放心。代码

/*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

最后,有问题找我

phonegap(cordova) 自己定义插件代码篇(五)----android ,iOS 集成微信登陆的更多相关文章

  1. phonegap(cordova) 自己定义插件代码篇(六)----android ,iOS 微信支付工具整合

    还是那句话,在使用插件代码篇的时候,请先了解插件机制(如整合原生插件先阅读原生插件文档.非常重要.非常重要!非常重要!),如未了解,请先阅读入门篇.这里就专贴关键代码 必须先把官方sdk 依照要求一步 ...

  2. phonegap(cordova) 自己定义插件代码篇(三)----支付宝支付工具整合

    建议读者,先阅读官方文档,知晓其支付流程之后再来使用此代码,比方客户须要做什么,服务端须要做什么(非常重要!非常重要! 非常重要!),由于这几个篇幅都是纯代码篇,由于阅读前面的入门篇之后看这些应该毫无 ...

  3. phonegap(cordova) 自己定义插件代码篇(四)----读取本地图片

    有时候确实知道本地图片地址,要获取到base64  /** * 获取本地图片,包括路径和压缩后的 base64 */ (function (cordova) { var define = cordov ...

  4. Python 爬虫五 进阶案例-web微信登陆与消息发送

    首先回顾下网页微信登陆的一般流程 1.打开浏览器输入网址 2.使用手机微信扫码登陆 3.进入用户界面 1.打开浏览器输入网址 首先打开浏览器输入web微信网址,并进行监控: https://wx.qq ...

  5. 开源代码分析之Android/iOS Hybrid JSBridge框架

    Hybrid开发是现在的主流形式,对于业务快速迭代的公司尤其重要.曾将在鞋厂接触了很多关于Hybrid的理念,在这里分享一些Hybrid框架思想. Hybrid框架包括Native与H5的通信,Web ...

  6. Cordova开发总结(插件篇)

    最近刚刚做完一个用Cordova开发了一款电子商务的应用.在选用Cordova前,我有考察过,国内的Appcan, Apicloud等等的解决方案.其实Appcan,ApiCloud的混合方案挺完整的 ...

  7. Android移动APP开发笔记——Cordova(PhoneGap)通过CordovaPlugin插件调用 Activity 实例

    引言 Cordova(PhoneGap)采用的是HTML5+JavaScript混合模式来开发移动手机APP,因此当页面需要获取手机内部某些信息时(例如:联系人信息,坐标定位,短信等),程序就需要调用 ...

  8. Cordova应用的JavaScript代码和自定义插件代码的调试

    我之前写过三篇Cordova相关的技术文章.当我们使用Cordova将自己开发的前端应用打包安装到手机上后,可能会遇到需要调试Cordova应用的时候. 本文就介绍Cordova应用的调试步骤. 如果 ...

  9. Android Cordova 插件开发之编写自己定义插件

    前言 本文适合Android+web的复合型人才,由于cordova本身就是混合开发,所以在Android开发的基础上,还要懂web相关技术(HTML+CSS+JS).可是也有例外,比方我.仅仅需负责 ...

随机推荐

  1. TextView中文文档

    十分感谢农民伯伯的翻译:http://www.cnblogs.com/over140/archive/2010/08/27/1809745.html xml 属性: 属性名称 描述  android: ...

  2. Kubernetes 存储系统 Storage 介绍

    本文环境为Kubernetes V1.11,操作系统版本为 CentOs 7.3,Kubernetes集群安装可以参考 kubeadm安装kubernetes V1.11.1 集群 容器中的存储都是临 ...

  3. [转]如何将PHP作为Shell脚本语言使用

    From : http://www.linuxfly.org/post/559/ 我们都知道,PHP是一种非常好的动态网页开发语言(速度飞快,开发周期短……).但是只有很少数的人意识到PHP也可以很好 ...

  4. 不知道哪里alert undefined 用下面的语句是js报错.F12能提示报错的地方window.alert=function(aa){ if (typeof (aa)"undefined"){ throw "就是这";}};

    不知道哪里alert undefined 用下面的语句是js报错.F12能提示报错的地方 var oldalert=window.alert; window.alert=function(aa){ i ...

  5. hyper-V下虚拟机连接外网,怎么才能将Hyper-V 的虚拟机接入互联网?

    现在情况是这样的: windows hyper-V主机IP: 192.168.20.3 hyper-v虚拟网卡IP:192.168.20.13 虚拟机1IP:192.168.20.21 同一局域网主机 ...

  6. Linux 动态链接库(.so)的使用

    1. 背景 库:就是已经编写好的,后续可以直接使用的代码. c++静态库:会合入到最终生成的程序,使得结果文件比较大.优点是不再有任何依赖. c++动态库:动态库,一个文件可以多个代码同时使用内存中只 ...

  7. 【Spark】SparkStreaming-Kafka-集成-终极参考资料

    SparkStreaming-Kafka-集成-终极参考资料 Spark Streaming和Kafka整合开发指南(二) – 过往记忆 Streamingkafka零丢失 | 等英博客 spark- ...

  8. F分布

    定义:设X1服从自由度为m的χ2分布,X2服从自由度为n的χ2分布,且X1.X2相互独立,则称变量F=(X1/m)/(X2/n)所服从的分布为F分布,其中第一自由度为m,第二自由度为n.[1] F分布 ...

  9. Fixing the JavaScript typeof operator

    https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/javascript 类 ...

  10. uboot mmc read/write命令用法

    mmc read用来读取mmc内容到内存, mmc write用来写入内存内容到mmc中 具体用法, mmc read <device num> addr blk# cnt [partit ...