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

/*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. 《深入理解Android内核设计思想》

    <深入理解Android内核设计思想> 基本信息 作者: 林学森 出版社:人民邮电出版社 ISBN:9787115348418 上架时间:2014-4-25 出版日期:2014 年5月 开 ...

  2. Intelli系列代理部分报错:You have JVM property https.proxyHost set..

    You have JVM property https.proxyHost set to '...'. This may lead to incorrect behaviour. Proxy shou ...

  3. unix时间戳time_t与UTC时区的关系

    一般我用C写unix时间戳是这样子的 #include<stdio.h> #include<time.h> void printfDateTimeStr(struct tm * ...

  4. Swift:宏定义

    一.简述 swift中并没有加入宏系统,C语言使用#define定义的基本常量在导入Swift时被Swift编译自动转为Swfit语言的全局变量.但复杂的宏定义不能被Swift转换.Swift中类似宏 ...

  5. C/C++ signal 信号处理函数

    软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号. 内核也可以因为内部事件而给进程发送信号,通知进程发生了某个事件. 注意,信号只 ...

  6. IIS配置中出现HRESULT:0X80070020错误

    Win7 IIS启动失败.手工启动它,提示:“另一个程序正在使用此文件,进程无法访问!” 此时是因为另一个程序占用了IIS的端口号,IIS一般用的是80端口,是谁占用了这个端口呢? 方法如下:开始菜单 ...

  7. JAVA-Eclipse中web-inf和meta-inf文件夹

    WEB-INF     /WEB-INF/web.xml        你的Web应用程序配置文件,这是一个XML文件,其中描述了 servlet 和其他的应用组件配置及命名规则:  /WEB- IN ...

  8. linux里tmpfs文件系统

    linux里tmpfs文件系统 是一个虚拟内存文件系统,它不同于传统的用块设备形式来实现的Ramdisk,也不同于针对物理内存的Ramfs.Tmpfs可以使用物理内存,也可以使用交换分区. umoun ...

  9. (转)在NGUI使用图片文字(数字、美术字)(直接可用于UILable)

    本文永久地址:http://www.omuying.com/article/24.aspx,[文章转载请注明出处!] 在 Unity 开发过程中,我们经常会使用到美术提供的图片文字(数字)来美化我们的 ...

  10. esUtil.h中的m变量报错

    引用了OpenGL ES自带的esUtil.h, 编译的时候报错:     typedef struct     {         GLfloat m[4][4];     } ESMatrix; ...