Line
注册 line 账号
- 创立 Providers & channel
- 装备 bundle ID
留意设置 URL Type
代码
iOS
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
LoginManager.shared.setup(channelID: "xxxxxxx", universalLinkURL: nil)
return true
}
func lineAction(_ sender: UIButton) {
LoginManager.shared.login(permissions: [.profile], in: self) { result in
switch result {
case .success(let loginResult):
if let profile = loginResult.userProfile {
print("User ID: (profile.userID)")
print("User Display Name: (profile.displayName)")
print("User Icon: (String(describing: profile.pictureURL))")
}
case .failure(let error):
print(error)
}
}
}
func lineLoginOutAction() {
LoginManager.shared.logout { (result) in
switch result {
case .success(let success):
print(success)
case .failure(let error):
print(error)
}
}
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
LineSDK.instance.setup("xxxxxx").then((_) {});
runApp(const MyApp());
}
void lineLoginHandler() async {
try {
final result = await LineSDK.instance.login();
final userId = result.userProfile?.userId;
final name = result.userProfile?.displayName;
final avatar = result.userProfile?.pictureUrl;
print('userId: $userId name: $name avatar: $avatar');
} on PlatformException catch (e) {
print(e.toString());
}
}
void lineLoginOutHandler() async {
try {
await LineSDK.instance.logout();
} on PlatformException catch (e) {
print(e.message);
}
}
创立 OAuth 客户端 ID
装备 OAuth 同意画面
留意
设置 URL Type
- 留意设置 URL Schemes 是凭据 iOS 网址通讯协定
设置 OAuth
代码
iOS
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in
if error != nil || user == nil {
} else {
}
}
return true
}
func googleAction(_ sender: UIButton) {
let signInConfig = GIDConfiguration(clientID: "xxxxxxx-xxxxxxxxxxxxxx.apps.googleusercontent.com")
GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in
guard error == nil else { return }
}
}
func googleLoginOutAction() {
GIDSignIn.sharedInstance.signOut();
}
Flutter
final GoogleSignIn _googleSignIn = GoogleSignIn(
clientId:
'xxxxxxx-xxxxxxxxxx.apps.googleusercontent.com',
scopes: [
'email',
'https://www.googleapis.com/auth/contacts.readonly',
],
);
void googleLoginHandler() async {
try {
final result = await _googleSignIn.signIn();
final userId = result?.id;
final name = result?.displayName;
final email = result?.email;
final avatar = result?.photoUrl;
print('userId: $userId name: $name email: $email avatar: $avatar');
} catch (error) {
print(error);
}
}
void googleLoginOutHandler() async {
try {
await _googleSignIn.signOut();
} catch (error) {
print(error);
}
}
Facebook
创立使用,并授权
留意设置 URL Type 等信息
- 使用编号 (FacebookAppID)
- 客户端口令 (FacebookClientToken)
- 显现名称(FacebookDisplayName)
代码
iOS
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
var handled: Bool
handled = GIDSignIn.sharedInstance.handle(url)
if handled {
return true
}
ApplicationDelegate.shared.application(
app,
open: url,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation]
)
return LoginManager.shared.application(app, open: url)
}
@IBAction func facebookAction(_ sender: UIButton) {
let loginManager = LoginManager()
loginManager.logIn(permissions: ["public_profile"], from: self) { result, error in
if let error = error {
print("Encountered Erorr: (error)")
} else if let result = result, result.isCancelled {
print("Cancelled")
} else {
print("Logged In")
if let userID = result?.token?.userID {
print("userID: ===> (userID)")
}
if let tokenString = result?.token?.tokenString {
print("tokenString: ===> (tokenString)")
}
}
}
}
func facebookLoginOutAction() {
let loginManager = LoginManager()
loginManager.logOut()
}
Flutter
void facebookLoginHandler() async {
final fb = FacebookLogin();
final res = await fb.logIn(permissions: [
FacebookPermission.publicProfile,
FacebookPermission.email,
]);
switch (res.status) {
case FacebookLoginStatus.success:
final FacebookAccessToken? accessToken = res.accessToken;
print('Access token: ${accessToken?.token}');
final profile = await fb.getUserProfile();
print('Hello, ${profile?.name}! You ID: ${profile?.userId}');
final imageUrl = await fb.getProfileImageUrl(width: 100);
print('Your profile image: $imageUrl');
final email = await fb.getUserEmail();
if (email != null) print('And your email is $email');
break;
case FacebookLoginStatus.cancel:
break;
case FacebookLoginStatus.error:
print('Error while log in: ${res.error}');
break;
}
}
void facebookLoginOutHandler() async {
try {
final fb = FacebookLogin();
await fb.logOut();
} catch (error) {
print(error);
}
}
Demo
参考
官方文档
- Developers Line
-
LINE SDK for iOS Swift overview
- LINE SDK for iOS Objective-C overview
- LINE SDK for Android overview
- LINE SDK for Flutter
- Developers Facebook
- Developers Google
Flutter SDK
- flutter_line_sdk
- flutter_login_facebook
- google_sign_in