一、涉及到的AppDelegate.h的改造代码
#import <Cordova/CDVViewController.h>
#import <Cordova/CDVAppDelegate.h>
#import "WXApi.h"
@interface AppDelegate : CDVAppDelegate<WXApiDelegate>
@property (nonatomic, strong) NSMutableDictionary *resp;
@property (nonatomic, strong) NSDictionary *params4WeiXin;
@property (nonatomic, strong) CDVInvokedUrlCommand *command;
@property (nonatomic, strong) NSString *code;
@end
二、涉及到的AppDelegate.m的改造代码
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
//
// AppDelegate.m
// 贵州沃受理
//
// Created by ___FULLUSERNAME___ on ___DATE___.
// Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved.
//
#import "AppDelegate.h"
#import "MainViewController.h"
#import "LTMHSDKManager.h"
#import "WeiXinInterfacePlugin.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.viewController = [[MainViewController alloc] init];
NSLog(@"PortalPlugin application didFinishLaunchingWithOptions...");
NSString * token = @"C8D39C17C81BC9AB";
NSString * appId = @"51add930f363cc9a";
NSString * appSecret = @"953d06b5743643c8b890bf74dd95390d";
NSString * url = @"http://58.16.11.245/ubssapp";
//初始化SDK实例,并传入代理实例
//thirdPlatformUrl为第三方服务端地址,用于获取accessToken后再调用接口获取用户信息,这边不写,改在app前端另外调用
[[LTMHSDKManager shareManager]setDelegete:(id)self token:token thirdPlatformUrl:url appID:appId appSecret:appSecret];
//return YES;
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
/**
此函数手工添加到AppDelegate.m里 step2
当使用openURL从其他APP跳转至当前APP时, 该方法会自动调用
更新程序调用URL,当程序被其他应用调起时,向SDK传入调起URL信息以及其他参数
*/
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
// NSLog(@"PortalPlugin application openURL...:%@...from:%@", url, sourceApplication);
// [[LTMHSDKManager shareManager] ltmhUpdateOpenInfo:url];
// return YES;
if(sourceApplication){
//判断请求来源 如果来源贵州任我行
if([sourceApplication isEqualToString:@"com.bonc.MovePortalBase.UNICOM.GZ"]){
NSLog(@"%@ sourceApplication",sourceApplication);
[[LTMHSDKManager shareManager] ltmhUpdateOpenInfo:url];
return YES;
}else if([sourceApplication isEqualToString:@"com.tencent.xin"]){//如果来源于微信
NSLog(@"%@ sourceApplication",sourceApplication);
return [WXApi handleOpenURL:url delegate:self];
}
}
return YES;
}
//重写微信handleOpenURL
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [WXApi handleOpenURL:url delegate:self];
}
//重写微信回调
-(void)onResp:(BaseResp *)resp
{
//调用微信插件中的回调处理方法
// WeiXinInterfacePlugin *weiXinInterfacePlugin = [[WeiXinInterfacePlugin alloc]init];//初始化WeiXinInterfacePlugin对象
// NSLog(@"onResp command is %@",self.command.callbackId);
// WeiXinInterfacePlugin *weiXinInterfacePlugin = (WeiXinInterfacePlugin*)[[UIApplication sharedApplication] delegate];
// [weiXinInterfacePlugin onResp:resp withAppParams:self.params4WeiXin withCallbackId:self.command];//传入微信返回报文和appId和appSecret 调用WeiXinInterfacePlugin方法
NSLog(@"微信回调处理");
// 处理 登录授权请求 回调
if ([resp isKindOfClass:[SendAuthResp class]]) {
switch (resp.errCode) {
case WXSuccess:
{
// 返回成功,获取Code
SendAuthResp *sendResp = (SendAuthResp*)resp;
NSString *code = sendResp.code;
NSLog(@"返回的code-->%@",sendResp.code);
self.code = code;
}
break;
}
}
}
/**
成功回调
*/
- (void)ltmhOnComplete:(LTMHSDKManager *)manager withToken:(NSDictionary *)accessTokenInfo{
NSLog(@"PortalPlugin ltmhOnComplete......:%@", [self objToString:accessTokenInfo]);
//NSMutableDictionary *resp = [@{} mutableCopy]; //返回信息
self.resp = [@{} mutableCopy]; //返回信息
[self.resp setValue:@"0000" forKey:@"respCode"];
[self.resp setValue:@"东方国信任沃行门户获取用户信息成功" forKey:@"respDesc"];
NSMutableDictionary *data = [@{} mutableCopy]; //返回data信息
NSString *accesstoken = [accessTokenInfo objectForKey:@"accesstoken"];//注意大小写
[data setValue:accesstoken forKey:@"accesstoken"]; //
//[data setValue:appId forKey:@"appid"]; //
//[data setValue:appSecret forKey:@"appsecret"]; //
[self.resp setObject:data forKey:@"data"];
//返回数据
// [self sendRespInfo:self.resp withCallBackID: callbackId];
}
/**
失败回调
*/
- (void)ltmhOnError:(LTMHSDKManager *)manager withError:(NSString *)errMsg{
NSLog(@"PortalPlugin ltmhOnError......:%@", errMsg);
//NSMutableDictionary *resp = [@{} mutableCopy]; //返回信息
self.resp = [@{} mutableCopy]; //返回信息
[self.resp setValue:@"0001" forKey:@"respCode"];
[self.resp setValue:@"东方国信任沃行门户获取accesstoken失败" forKey:@"respDesc"];
NSMutableDictionary *data = [@{} mutableCopy]; //返回data信息
[data setValue:@"" forKey:@"errorCode"]; //
[data setValue:errMsg forKey:@"errorDesc"];
[self.resp setObject:data forKey:@"data"];
//返回数据
//[self sendRespInfo:self.resp withCallBackID: callbackId];
}
/**
对象转json字符串
@param object <#object description#>
@return <#return value description#>
*/
-(NSString*)objToString:(id)object
{
NSString *jsonString = nil;
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
if (! jsonData) {
NSLog(@"objToString get an error: %@", error);
} else {
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
return jsonString;
}
@end