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

/*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. Eclipse with ADT的安装和配置

    我们从安卓官方网站(https://developer.android.com/sdk/index.html#download)下载下来的eclipse是捆绑好了ADT的,所以不用自己安装插件,十分方 ...

  2. Eclipse 乱码 解决方案总结(UTF8 -- GBK)

    UTF8 --> GBK;   GBK --> UTF8 eclipse的中文乱码问题,一般不外乎是由操作系统平台编码的不一致导致,如Linux中默认的中文字体编码问UTF8, 而Wind ...

  3. Unexpected identifier in composer-common/lib/cardstore/businessnetworkcardstore.js:54

    c错误描述 Unexpected identifier in composer-common/lib/cardstore/businessnetworkcardstore.js:54 yo hyper ...

  4. eclipse 创建普通maven项目

  5. 【Spark】Spark-空RDD判断与处理

    Spark-空RDD判断与处理 SparkKafkaDemo - Streaming Statistics rdd isempty count_百度搜索 Spark RDD.isEmpty costs ...

  6. 【Scala】Scala-Option-Null的蹊跷

    Scala-Option-Null的蹊跷 scala Some(null)_百度搜索 scala - Why Some(null) isn't considered None? - Stack Ove ...

  7. WordPress 在function.php 文件中方法中the_XXX方法失效

    最近在使用WP给客户做一个企业网站,却出现从未遇到的问题. 事件是这样子的:我在function.php文件里写了一个根据分类ID获取文章的文章,因为该方法里的html元素是在多个页面共用的 但我在i ...

  8. OPENGL: WHY IS YOUR CODE PRODUCING A BLACK WINDOW?

      Introduction One of the most common problems for novice, and sometimes experienced, OpenGL program ...

  9. 20160205.CCPP体系具体解释(0015天)

    程序片段(01):01.杨辉三角.c 内容概要:杨辉三角 #include <stdio.h> #include <stdlib.h> #define N 10 //01.杨辉 ...

  10. 今天发现一个神奇的网站Greasy Fork

    Greasy Fork这个网站的神奇之处在于,提供了各路大神编写的脚本,可以在浏览器中实现各种神奇的功能,比如这个: 我下载使用了,确实好用,什么腾讯视频vip,爱奇艺视频vip,统统可以观看~ 并且 ...