我的项目
https://github.com/zhaochengxiang/flutter_lychee
flutter端
1.依赖sharesdk。
在https://pub.dartlang.org/packages/sharesdk中下载sharesdk1.0.8demo,并将其放入我们的项目中本地引用。这里我将其放入plugins目录下。
添加下列代码到pubspec.yaml进行本地引用:
dependencies:
sharesdk:
path: plugins/sharesdk-1.0.8
2.安装。
在flutter 中使用下列命令来安装:
$ flutter packages get
3.使用它。
现在你可以在Dart代码中使用sharesdk。
下面我们在main.dart中来实现微信分享功能:
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:sharesdk/sharesdk.dart';
void main() => runApp(ShareApp());
class ShareApp extends StatefulWidget {
@override
State<ShareApp> createState() {
return new _ShareAppState();
}
}
class _ShareAppState extends State<ShareApp> {
//初始化sharasdk
//这里只添加微信分享功能来进行测试
//请先在微信开放平台中申请微信分享权限 并使用生成的AppID和AppSecret。
initShareSDK() {
ShareSDKRegister register = ShareSDKRegister();
register.setupWechat(
"wxcb24d644e340e9cc", "34a7656f86280f75e16cd9094a8a1f54");
ShareSDK.regist(register);
}
@override
void initState() {
initShareSDK();
super.initState();
}
void showAlert(SSDKResponseState state, Map content, BuildContext context) {
String title = "失败";
switch (state) {
case SSDKResponseState.Success:
title = "成功";
break;
case SSDKResponseState.Fail:
title = "失败";
break;
case SSDKResponseState.Cancel:
title = "取消";
break;
default:
title = state.toString();
break;
}
showDialog(
context: context,
builder: (BuildContext context) =>
CupertinoAlertDialog(
title: new Text(title),
content: new Text(content != null ? content.toString() : ""),
actions: <Widget>[
new FlatButton(
child: new Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
)
]
)
);
}
userWeChatShare(BuildContext context) {
SSDKMap params = SSDKMap()
..setGeneral(
"title",
"text",
null,
null,
null,
null,
null,
null,
null,
SSDKContentTypes.text);
ShareSDK.share(ShareSDKPlatforms.wechatSession, params, (SSDKResponseState state, Map userdata, Map contentEntity, SSDKError error){
showAlert(state, error.rawData, context);
});
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData(primaryColor: Colors.white),
home: new Center(
child:RaisedButton(
onPressed:(){userWeChatShare(context);},
child:Text("微信分享"),
)
)
);
}
}
android端
1.重新mobsdk.gradle文件来选择你需要使用的平台,我们这里只使用了微信。
apply plugin: 'com.mob.sdk'
MobSDK {
appKey "2a6869394af80"
appSecret "796421d6838fe1ecbef3b419d2c2bcb0"
ShareSDK {
//平台配置信息
devInfo {
Wechat {
appId "wxcb24d644e340e9cc"
appSecret "34a7656f86280f75e16cd9094a8a1f54"
}
}
}
}
2.在Manifest清单文件中配置:tools:replace="android:name"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="你的包名">
<application
android:name = ".MyApplication"
tools:replace="android:name">
3.编译过程中报错如下:
屏蔽掉报错的内容即可!如果需要使用QQ,需把相应的代码还原!
注意:如果跳转不到微信,请注意包名以及keystore是否与微信开放平台中的注册信息一致。
iOS端
1.自定义需要导入的分享平台.
目前我们只加入了微信分享功能,所以需要重新编辑 sharesdk.podspec 文件设置 dependency。
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'sharesdk'
s.version = '0.0.1'
s.summary = 'flutter plugin for sharesdk.'
s.description = <<-DESC
ShareSDK is the most comprehensive Social SDK in the world,which share easily with 40+ platforms.
DESC
s.homepage = 'http://www.mob.com'
s.license = { :file => '../LICENSE' }
s.author = { 'Mob' => 'mobproducts@163.com' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.public_header_files = 'Classes/**/*.h'
s.dependency 'Flutter'
s.dependency 'mob_sharesdk'
# s.dependency 'mob_sharesdk/ShareSDKUI'
# s.dependency 'mob_sharesdk/ShareSDKPlatforms/QQ'
# s.dependency 'mob_sharesdk/ShareSDKPlatforms/SinaWeibo'
s.dependency 'mob_sharesdk/ShareSDKPlatforms/WeChat'
# s.dependency 'mob_sharesdk/ShareSDKPlatforms/Facebook'
# s.dependency 'mob_sharesdk/ShareSDKPlatforms/Twitter'
s.ios.deployment_target = '8.0'
end
2.在项目工程的Info.plist 中如图增加 MOBAppKey 和 MOBAppSecret 两个字段。
3.添加微信url scheme。
4.配置白名单。
效果图:
参考文献:
https://pub.dartlang.org/packages/sharesdk
https://pub.dartlang.org/documentation/sharesdk/latest/