我的項目
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.使用它。
現(xiàn)在你可以在Dart代碼中使用sharesdk。
下面我們在main.dart中來實現(xiàn)微信分享功能:
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
//這里只添加微信分享功能來進行測試
//請先在微信開放平臺中申請微信分享權(quán)限 并使用生成的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.編譯過程中報錯如下:
屏蔽掉報錯的內(nèi)容即可!如果需要使用QQ姚淆,需把相應(yīng)的代碼還原!
注意:如果跳轉(zhuǎn)不到微信孕蝉,請注意包名以及keystore是否與微信開放平臺中的注冊信息一致。
iOS端
1.自定義需要導(dǎo)入的分享平臺.
目前我們只加入了微信分享功能腌逢,所以需要重新編輯 sharesdk.podspec 文件設(shè)置 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/