PhoneGap 微信插件 for iOS

来源:互联网 发布:巨人网络退市市值 编辑:程序博客网 时间:2024/04/28 11:05
Weixin 
微信插件
Adding the Plugin to your project


1.add weixin.js to your www folder and include it to your html file below cordova.js
  1. <script type="text/javascript" charset="utf-8" src="cordova.js">
  2. <script type="text/javascript" charset="utf-8" src="weixin.js">
复制代码
2.Add WeChatSDK & SinaWeixinPlugin src files to your project.

3.Add Weixin-SinaWeixinPlugin [key-value] to Cordova.plist->Plugins

4.Modify project info.plist : add URL types -> URl Schemes -> Item0-'your appId' (key-value)

Usage


sina.weixin.registerApp(onSuccess,onError,appId)


在微信终端程序中注册第三方应用说明:需要在每次启动第三方应用程序时调用。第一次调用后,会在微信的可用应用列表中出现。

  •     appId 微信开发的ID (通过http://open.weixin.qq.com/ 申请)
  •     onSuccess 注册成功时的回调函数
  •     onError 注册失败时的回调函数
             errCode 错误值
             errStr 错误说明
demo

  1. sina.weixin.registerApp(function(){
  2.                             registed=true;
  3.                             },onError,"XXXXXXXXX");// 填入申请的微信应用开发appId
  4. function onError(response){
  5.     var detail = document.getElementById("detail");
  6.     detail.innerHTML="error:"+response.errCode;
  7.     detail.innerHTML=detail.innerHTML+"
  8. "+response.errStr;
  9. }
复制代码
sina.weixin.getWXAppInstallUrl(onSuccess,onError)

获取微信的itunes安装地址

  •     onSuccess 获取成功时回调函数
  •     onError 获取失败时回调函数
             errCode 错误值
             errStr 错误说明

function onSuccess(url){ } url 为微信的itunes安装地址

function onError(error){ }
demo

  1. sina.weixin.getWXAppInstallUrl(function(resultUrl){
  2.                                    console.log(resultUrl);
  3.                                    },function(error){
  4.                                    console.log(error.errCode);
  5.                                    console.log(error.errStr);
  6.                                    });
复制代码

sina.weixin.isWeixinInstalled(onSuccess,onError)

检查微信是否已被用户安装

  •     onSuccess 微信已安装的回调函数
  •     onError 微信未安装的回调函数
             errCode 错误值
             errStr 错误说明

demo

  1. sina.weixin.isWeixinInstalled(function(){
  2.                           console.log('is installed');
  3.                           },function(){
  4.                           console.log('not installed');
  5.                           });

  6. sina.weixin.isSupportApi(onSuccess,onError)
复制代码

判断当前微信的版本是否支持OpenApi

  •     onSuccess 当前微信版本支持OpenApi时的回调函数
  •     onError 当前微信版本不支持OpenApi时的回调函数
             errCode 错误值
             errStr 错误说明

demo

  1. sina.weixin.isSupportApi(function(){
  2.                                   console.log('is support api');
  3.                                   },function(){
  4.                                   console.log('not support api');
  5.                                   });
复制代码

sina.weixin.openWXApp(onSuccess,onError)

打开微信

  •     onSuccess 成功时的回调函数
  •     onError 失败时的回调函数
             errCode 错误值
             errStr 错误说明

demo

  1. sina.weixin.openWXApp(function(){
  2.                           console.log('open success');
  3.                           },function(){
  4.                           console.log('open error');
  5.                           });
复制代码

sina.weixin.textContent(onSuccess, onError, types, text)



发送/获取 文本信息

发送:发送请求到微信,等待微信返回应答

获取:收到微信的请求,发送文本类型应答给微信,并切换到微信界面

  •     onSuccess 成功时的回调函数
  •     onError 失败时的回调函数
  •     types 设置对文本信息的处理类型
             send 表示:发送文本信息请求到微信
             get 表示:收到微信的请求,发送文本类型应答给微信
  •     text 文本信息内容

demo
  1. function getTextContent() {
  2.     sina.weixin.textContent(onSuccess,onError,"get","Sina App Engine");
  3. }

  4. function sendTextContent() {
  5.     sina.weixin.textContent(onSuccess,onError,"send","hello world");
  6. }

  7. function onSuccess(){
  8.     console.log('success');
  9. }

  10. function onError(response){
  11.     var detail = document.getElementById("detail");
  12.     detail.innerHTML="error:"+response.errCode;
  13.     detail.innerHTML=detail.innerHTML+"
  14. "+response.errStr;
  15. }
复制代码

sina.weixin.imageContent(onSuccess, onError, types, imageUrl, options)

发送/获取 图片信息

发送:发送图片信息请求到微信,等待微信返回应答

获取:收到微信的请求,发送图片类型应答给微信,并切换到微信界面

  •     onSuccess 成功时的回调函数
  •     onError 失败时的回调函数
  •     types 设置对图片信息的处理类型
             send 表示:发送图片信息请求到微信
             get 表示:收到微信的请求,发送图片类型应答给微信
  •     imageUrl 图片的Url链接
  •     options 相关参数项,字典类型。包括
             title
             description

demo

  1. function sendImageContent(){
  2. //从相册选择图片,并发送到微信应用
  3.     var app={
  4.     onCameraSuccess:function(imageURI){
  5.         sina.weixin.imageContent(onSuccess,onError,"send",imageURI,{
  6.                                      title:"kris",
  7.                                      description:"picture",
  8.                                      });
  9.     },
  10.     onCameraFail:function(msg){
  11.         console.log('error msg:'+msg);
  12.     },
  13.     getPicture:function(){
  14.         navigator.camera.getPicture(app.onCameraSuccess, app.onCameraFail, {
  15.                                     quality: 50,
  16.                                     destinationType: Camera.DestinationType.FILE_URI,
  17.                                     sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
  18.                                     saveToPhotoAlbum: false
  19.                                     });
  20.     }
  21.     };
  22.     app.getPicture();
  23. }

  24. function getImageContent(){
  25. //收到来自微信的请求后,发送图片应答给微信
  26.     sina.weixin.getImageContent(onSuccess,onError,"http://pluginlist.sinaapp.com/client/images/music.png",{
  27.                                 title:"kris",
  28.                                 description:"picture",
  29.                                 });
  30. }

  31. function onSuccess(){
  32.     console.log('success');
  33. }

  34. function onError(response){
  35.     var detail = document.getElementById("detail");
  36.     detail.innerHTML="error:"+response.errCode;
  37.     detail.innerHTML=detail.innerHTML+"
  38. "+response.errStr;
  39. }
复制代码

sina.weixin.musicContent(onSuccess, onError, types, musicUrl, options)


发送/获取 音乐信息。musicUrl和lowBandUrl不能同时为空。

发送:发送音乐信息请求到微信,等待微信返回应答

获取:收到微信的请求,发送音乐类型应答给微信,并切换到微信界面

  •     onSuccess 成功时的回调函数
  •     onError 失败时的回调函数
  •     types 设置对音乐信息的处理类型
             send 表示:发送音乐信息请求到微信
             get 表示:收到微信的请求,发送音乐类型应答给微信
  •     musicUrl 音乐数据的url地址,不支持本地音乐URL。musicUrl和lowBandUrl不能同时为空。
  •     options 相关参数项,字典类型。包括
             title 音乐信息标题
             description 音乐信息描述内容
             lowBandUrl 音乐lowband数据的url地址,不支持本地音乐URL。musicUrl和lowBandUrl不能同时为空。
             thumbUrl 音乐信息缩略图url。当thumbUrl和thumbData同时设置时,采用thumbData。
             thumbData 音乐信息缩略图base64数据,大小不能超过32K。当thumbUrl和thumbData同时设置时,采用thumbData。

demo

  1. function sendMusicContent(){
  2.     sina.weixin.musicContent(onSuccess,onError,"send",
  3.                                  "http://pluginlist.sinaapp.com/client/music/Sunshine.mp3",
  4.                                  {
  5.                                  title:"Sunshine",
  6.                                  description:"Happy Music",
  7.                                  thumbUrl:"http://pluginlist.sinaapp.com/client/images/music.png"
  8.                                  });
  9. }
  10. function getMusicContent(){
  11.     sina.weixin.musicContent(onSuccess,onError,"get",
  12.                                     "http://pluginlist.sinaapp.com/client/music/Sunshine.mp3",
  13.                                     {
  14.                                     title:"Sunshine",
  15.                                     description:"Happy Music",
  16.                                     thumbUrl:"http://pluginlist.sinaapp.com/client/images/music.png"
  17.                                     });
  18. }
  19. function onSuccess(){
  20.     console.log('success');
  21. }
  22. function onError(response){
  23.     var detail = document.getElementById("detail");
  24.     detail.innerHTML="error:"+response.errCode;
  25.     detail.innerHTML=detail.innerHTML+"
  26. "+response.errStr;
  27. }
复制代码

sina.weixin.videoContent(onSuccess, onError, types, videoUrl, options)

发送/获取 视频信息。videoUrl和lowBandUrl不能同时为空。

发送:发送视频信息请求到微信,等待微信返回应答

获取:收到微信的请求,发送视频类型应答给微信,并切换到微信界面

  •     onSuccess 成功时的回调函数
  •     onError 失败时的回调函数
  •     types 设置对视频信息的处理类型
             send 表示:发送视频信息请求到微信
             get 表示:收到微信的请求,发送视频类型应答给微信
  •     videoUrl 视频数据的url地址,不支持本地视频URL。videoUrl和lowBandUrl不能同时为空。
  •     options 相关参数项,字典类型。包括
             title 视频信息标题
             description 视频信息描述内容
             lowBandUrl 视频lowband数据的url地址,不支持本地视频URL。videoUrl和lowBandUrl不能同时为空。
             thumbUrl 视频信息缩略图url。当thumbUrl和thumbData同时设置时,采用thumbData。
             thumbData 视频信息缩略图base64数据,大小不能超过32K。当thumbUrl和thumbData同时设置时,采用thumbData。

demo

  1. function getVideoContent(){
  2.     sina.weixin.videoContent(onSuccess,onError,"get",
  3.                              "http://www.tudou.com/listplay/0nYp1obVv60/mA_xdJq7lSo.html",
  4.                              {
  5.                              title:"video",
  6.                              description:"Get Happy Video",
  7.                              thumbUrl:"http://pluginlist.sinaapp.com/client/images/video.png"
  8.                              });
  9. }
  10. function sendVideoContent(){
  11.     sina.weixin.videoContent(onSuccess,onError,"send",
  12.                                  "http://www.tudou.com/listplay/0nYp1obVv60/mA_xdJq7lSo.html",
  13.                                  {
  14.                                  title:"video",
  15.                                  description:"Happy Video",
  16.                                  thumbUrl:"http://pluginlist.sinaapp.com/client/images/video.png"
  17.                                  });
  18. }
复制代码


sina.weixin.webpageContent(onSuccess, onError, types, webpageUrl, options)


发送/获取 网页信息

发送:发送网页信息请求到微信,等待微信返回应答

获取:收到微信的请求,发送网页类型应答给微信,并切换到微信界面

  •     onSuccess 成功时的回调函数
  •     onError 失败时的回调函数
  •     types 设置对网页信息的处理类型
             send 表示:发送网页信息请求到微信
             get 表示:收到微信的请求,发送网页类型应答给微信
  •     webpageUrl 网页url地址
  •     options 相关参数项,字典类型。包括
             title 网页信息标题
             description 网页信息描述内容
             thumbUrl 网页信息缩略图url。当thumbUrl和thumbData同时设置时,采用thumbData。
             thumbData 网页信息缩略图base64数据,大小不能超过32K。当thumbUrl和thumbData同时设置时,采用thumbData。

demo

  1. function sendWebpageContent(){
  2.     sina.weixin.webpageContent(onSuccess,onError,"send",
  3.                                    "http://sae.sina.com.cn/?m=devcenter&catId=235",
  4.                                    {
  5.                                    title:"新浪移动云平台介绍",
  6.                                    description:"新浪移动云是在SAE基础上的子平台,专注于为移动设备同时提供云+端的能力。\n为方便开发者使用,移动云直接集成在SAE在线管理平台中。",
  7.                                    thumbUrl:"http://pluginlist.sinaapp.com/client/images/icon.png"
  8.                                    });
  9. }
  10. function getWebpageContent(){
  11.     sina.weixin.webpageContent(onSuccess,onError,"get",
  12.                                   "http://sae.sina.com.cn/?m=devcenter&catId=235",
  13.                                   {
  14.                                   title:"新浪移动云平台介绍",
  15.                                   description:"新浪移动云是在SAE基础上的子平台,专注于为移动设备同时提供云+端的能力。\n为方便开发者使用,移动云直接集成在SAE在线管理平台中。",
  16.                                   thumbUrl:"http://pluginlist.sinaapp.com/client/images/icon.png"
  17.                                   });
  18. }
复制代码

sina.weixin.APPContent(onSuccess, onError, types, options)

发送/获取 APP扩展信息。微信需要处理这种APP扩展信息时,会调用该第三方应用的监听回调方法来处理。监听回调方法的设置及相应操作,请参考sina.weixin.setResponser(responseString)。

发送:发送APP扩展信息请求到微信,等待微信返回应答

获取:收到微信的请求,发送APP扩展类型应答给微信,并切换到微信界面

  •     onSuccess 成功时的回调函数
  •     onError 失败时的回调函数
  •     types 设置对APP扩展信息的处理类型
             send 表示:发送APP扩展信息请求到微信
             get 表示:收到微信的请求,发送APP扩展类型应答给微信
  •     options 相关参数项,字典类型。包括
             title APP扩展信息标题
             description APP扩展信息描述内容
             thumbUrl APP扩展信息缩略图url。当thumbUrl和thumbData同时设置时,采用thumbData。
             thumbData APP扩展信息缩略图base64数据,大小不能超过32K。当thumbUrl和thumbData同时设置时,采用thumbData。
             extInfo 自定义简单数据,长度不能超过2K。微信应用会回传给第三方应用处理。extInfo与fileData不能同时为空。
             fileData APP文件数据,JSON对象,大小不能超过10M。该数据发送给微信好友,微信好友需要点击后下载数据,微信应用会回传给第三方应用处理。extInfo与fileData不能同时为空。
             url 若第三方应用不存在,微信应用会打开该url所指的App下载地址。

demo

  1. function getAPPContent(){
  2.     sina.weixin.APPContent(onSuccess,onError,"get",
  3.                            {
  4.                            title:"App消息",
  5.                            description:"您的App消息到啦,快去看看吧",
  6.                            thumbUrl:"http://pluginlist.sinaapp.com/client/images/icon.png",
  7.                            extInfo:"dictionary",
  8.                            url:"http://sae.sina.com.cn",
  9.                            fileData:JSON.stringify(
  10.                                                    {title:"App消息名称",
  11.                                                    description:"内容描述",
  12.                                                    thumbUrl:"缩略图链接地址"}
  13.                                                    )
  14.                            });
  15. }
  16. function sendAPPContent(){
  17.     sina.weixin.APPContent(onSuccess,onError,"send",
  18.                            {
  19.                            title:"App消息",
  20.                            description:"您的App消息到啦,快去看看吧",
  21.                            thumbUrl:"http://pluginlist.sinaapp.com/client/images/icon.png",
  22.                            extInfo:"send APP",
  23.                            url:"http://www.sina.com.cn",
  24.                            fileData:JSON.stringify(
  25.                                                    {title:"App消息名称",
  26.                                                    description:"内容描述",
  27.                                                    thumbUrl:"缩略图链接地址"}
  28.                                                    )
  29.                            });
  30. }
复制代码


sina.weixin.setResponser(responseString)


设置监听回调函数,接收来自微信应用的请求

  •     responseString 监听回调方法的名字,默认为receiveResponse

function receiveResponse(response);

  •     type 回调信息的类型
             0 表示显示APP扩展格式信息。
             4 表示来自微信的请求信息,获取内容后返回给微信应用。
  •     当type==0时,response还包括下面几项:
             extInfo 第三方应用自定义简单数据,微信应用会回传给第三方应用处理。[说明:长度不能超过2K]
             fileData APP文件数据,JSON对象。该数据发送给微信好友,微信好友需要点击后下载数据,微信应用会回传给第三方应用处理。[说明:大小不能超过10M]
             thumbData APP扩展格式信息缩略图base64数据。

demo

  1. sina.weixin.setResponser("receiveResponse");
  2. function receiveResponse(response){
  3.     if(response.type==0)
  4.     {//显示来自第三方的APP扩展信息
  5.         var detail = document.getElementById("detail");
  6.         detail.innerHTML="extInfo:"+response.extInfo;
  7.         
  8.         var a=JSON.parse(response.fileData);
  9.         detail.innerHTML=detail.innerHTML+"
  10. title:"+a.title;
  11.         detail.innerHTML=detail.innerHTML+"
  12. description:"+a.description;
  13.         detail.innerHTML=detail.innerHTML+"
  14. thumbUrl:"+a.thumbUrl;
  15.         
  16.         var viewport = document.getElementById('viewport');
  17.         viewport.style.display = "";
  18.         document.getElementById("picture").src = "data:image/jpeg;base64," + response.thumbData;
  19.     }
  20.     else if(response.type==4)
  21.     {// 来自微信的请求信息,跳转到获取信息页面
  22.         var btn1=document.getElementById("btn1");
  23.         btn1.onclick=function(){sina.weixin.textContent(onSuccess,onError,"get","Sina App Engine");};
  24.         btn1.innerHTML="获取文本";
  25.     }
  26. }
复制代码