pub.dev上不含支付的 fluwx sdk
1、在pubspec.yaml 中添加依赖:
fluwx_no_pay: ^3.9.1
pub get以后默认会把微信sdk安卓端导入,iOS端可以参考微信官网文档集成
2、iOS AppDelegate配置
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
// WXApi.registerApp(<#T##appid: String##String#>, universalLink: <#T##String#>)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
extension AppDelegate {//处理 Universal Links
override func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
let url = userActivity.webpageURL!
//handle url
print("application continueUserActivity url:\(url)")
}
return true
}
}
3、为iOS配置universal link
4、在微信开发者平台注册AppID
5、在main.dart里注册微信
import 'package:fluwx_no_pay/fluwx_no_pay.dart' as fluwx;
_initFluwx() async {
// await Fluwx.registerWxApi(
// appId: "APPID", //传入注册的应用id
// doOnAndroid: true, //在android上运行
// doOnIOS: true, // 在ios上运行
// );
print("这里进来了吗?");
bool result = await fluwx.registerWxApi(appId: "123",universalLink:"https://test-apple-app-site.3irobotix.net/app/Pawtis");
print("微信注册结果:${result.toString()}");
var isInstall = await fluwx.isWeChatInstalled;
print("微信安装结果:${isInstall.toString()}");
}
6、在登录页面监听授权回调
_test() async {
bool exist = await fluwx.isWeChatInstalled;
print("object-001");
if (!exist) {
Utils.showToast("请先安装微信");
print("object-002");
}else{
print("object-003");
fluwx.weChatResponseEventHandler.distinct((a,b)=>a == b).listen((res) {
print("object-004");
if(res is fluwx.WeChatAuthResponse){
print("object-005");
int errCode = res.errCode;
log("登录返回值:errCode:${errCode} code:${res.code}");
if(errCode == 0){
String code = res.code!;
//需要吧登录返回code传给后台,剩下的事就交给后台处理
//_presenter.getWeChatAccessToken(code);
Utils.showToast("用户同意授权成功");
}else if(errCode == -4){
Utils.showToast("用户拒绝授权");
}else if(errCode == -3){
Utils.showToast("用户取消");
}
print("object-006");
}
});
}
}
7、在登录页面发起微信登录授权
_wechatAuth() {
//微信登录授权
fluwx.sendWeChatAuth(
scope: "snsapi_userinfo",
state: 'wechat_sdk_demo_test')
.then((value) {
print(value);
}).catchError((e) {
print('weChatLogin e $e');
});
}