@capacitor-community/facebook-login
Plug-in da comunidade Capacitor para login nativo do Facebook.
Mantenedor | GitHub | Social | Empresa Patrocinadora |
---|---|---|---|
Masahiko Sakakibara | rdlabo | @rdlabo | RELACION DESIGN LABO, GENERAL INC. |
Feito com contribuidores-img.
O código de demonstração está aqui.
Se você quiser saber a versão da biblioteca do Facebook, verifique:
% npm i --save @capacitor-community/facebook-login
% npx cap update
Os usuários do Capacitor v5 devem usar a versão v5 do Plugin.
% npm install @capacitor-community/facebook-login@5
No arquivo android/app/src/main/AndroidManifest.xml
, adicione os seguintes elementos XML em <manifest><application>
:
< meta-data android : name = " com.facebook.sdk.ApplicationId " android : value = " @string/facebook_app_id " />
< meta-data android : name = " com.facebook.sdk.ClientToken " android : value = " @string/facebook_client_token " />
No arquivo android/app/src/main/res/values/strings.xml
adicione as seguintes linhas:
< string name = " facebook_app_id " >[APP_ID]</ string >
< string name = " facebook_client_token " >[CLIENT_TOKEN]</ string >
Não se esqueça de substituir [APP_ID]
e [CLIENT_TOKEN]
pelo ID do seu aplicativo do Facebook.
Mais informações podem ser encontradas aqui: https://developers.facebook.com/docs/android/getting-started
Reinicie o Android Studio e faça uma compilação limpa.
No arquivo ios/App/App/AppDelegate.swift
adicione ou substitua o seguinte:
import UIKit
import Capacitor
import FBSDKCoreKit
@ UIApplicationMain
class AppDelegate : UIResponder , UIApplicationDelegate {
var window : UIWindow ?
func application ( _ application : UIApplication , didFinishLaunchingWithOptions launchOptions : [ UIApplication . LaunchOptionsKey : Any ] ? ) -> Bool {
// Override point for customization after application launch.
FBSDKCoreKit . ApplicationDelegate . shared . application (
application ,
didFinishLaunchingWithOptions : launchOptions
)
return true
}
...
func application ( _ app : UIApplication , open url : URL , options : [ UIApplication . OpenURLOptionsKey : Any ] = [ : ] ) -> Bool {
// Called when the app was launched with a url. Feel free to add additional processing here,
// but if you want the App API to support tracking app url opens, make sure to keep this call
if ( FBSDKCoreKit . ApplicationDelegate . shared . application (
app ,
open : url ,
sourceApplication : options [ UIApplication . OpenURLOptionsKey . sourceApplication ] as? String ,
annotation : options [ UIApplication . OpenURLOptionsKey . annotation ]
) ) {
return true;
} else {
return ApplicationDelegateProxy . shared . application ( app , open : url , options : options )
}
}
}
Adicione o seguinte no arquivo ios/App/App/info.plist
dentro do <dict>
mais externo:
< key >CFBundleURLTypes</ key >
< array >
< dict >
< key >CFBundleURLSchemes</ key >
< array >
< string >fb[APP_ID]</ string >
</ array >
</ dict >
</ array >
< key >FacebookAppID</ key >
< string >[APP_ID]</ string >
< key >FacebookClientToken</ key >
< string >[CLIENT_TOKEN]</ string >
< key >FacebookDisplayName</ key >
< string >[APP_NAME]</ string >
< key >LSApplicationQueriesSchemes</ key >
< array >
< string >fbapi</ string >
< string >fbapi20130214</ string >
< string >fbapi20130410</ string >
< string >fbapi20130702</ string >
< string >fbapi20131010</ string >
< string >fbapi20131219</ string >
< string >fbapi20140410</ string >
< string >fbapi20140116</ string >
< string >fbapi20150313</ string >
< string >fbapi20150629</ string >
< string >fbapi20160328</ string >
< string >fbauth</ string >
< string >fb-messenger-share-api</ string >
< string >fbauth2</ string >
< string >fbshareextension</ string >
</ array >
Mais informações podem ser encontradas aqui: https://developers.facebook.com/docs/facebook-login/ios
import { FacebookLogin } from '@capacitor-community/facebook-login' ;
// use hook after platform dom ready
await FacebookLogin . initialize ( { appId : '105890006170720' } ) ;
Mais informações podem ser encontradas aqui: https://developers.facebook.com/docs/facebook-login/web E você deve confirmar o tipo de retorno em https://github.com/rdlabo/capacitor-facebook-login/blob/master /src/web.ts#L55-L57 não é o mesmo tipo para login padrão do Facebook na web!
import {
FacebookLogin ,
FacebookLoginResponse ,
} from '@capacitor-community/facebook-login' ;
const FACEBOOK_PERMISSIONS = [
'email' ,
'user_birthday' ,
'user_photos' ,
'user_gender' ,
] ;
const result = await ( < FacebookLoginResponse > (
FacebookLogin . login ( { permissions : FACEBOOK_PERMISSIONS } )
) ) ;
if ( result . accessToken ) {
// Login successful.
console . log ( `Facebook access token is ${ result . accessToken . token } ` ) ;
}
import { FacebookLogin } from '@capacitor-community/facebook-login' ;
await FacebookLogin . logout ( ) ;
import {
FacebookLogin ,
FacebookLoginResponse ,
} from '@capacitor-community/facebook-login' ;
const result = await ( < FacebookLoginResponse > (
FacebookLogin . getCurrentAccessToken ( )
) ) ;
if ( result . accessToken ) {
console . log ( `Facebook access token is ${ result . accessToken . token } ` ) ;
}
import {
FacebookLogin ,
FacebookLoginResponse ,
} from '@capacitor-community/facebook-login' ;
const result = await FacebookLogin . getProfile < {
email : string ;
} > ( { fields : [ 'email' ] } ) ;
console . log ( `Facebook user's email is ${ result . email } ` ) ;
initialize(...)
login(...)
logout()
reauthorize()
getCurrentAccessToken()
getProfile(...)
logEvent(...)
setAutoLogAppEventsEnabled(...)
setAdvertiserTrackingEnabled(...)
setAdvertiserIDCollectionEnabled(...)
initialize ( options : Partial < FacebookConfiguration > ) => Promise < void >
Parâmetro | Tipo |
---|---|
options | Partial<FacebookConfiguration> |
login ( options : { permissions : string [ ] ; } ) = > Promise < FacebookLoginResponse >
Parâmetro | Tipo |
---|---|
options | { permissions: string[]; } |
Retorna: Promise<FacebookLoginResponse>
logout ( ) = > Promise < void >
reauthorize ( ) = > Promise < FacebookLoginResponse >
Retorna: Promise<FacebookLoginResponse>
getCurrentAccessToken ( ) = > Promise < FacebookCurrentAccessTokenResponse >
Retorna: Promise<FacebookCurrentAccessTokenResponse>
getProfile < T extends Record < string , unknown > > ( options : { fields : readonly string [ ] ; } ) => Promise < T >
Parâmetro | Tipo |
---|---|
options | { fields: readonly string[]; } |
Retorna: Promise<T>
logEvent ( options : { eventName : string ; } ) = > Promise < void >
Parâmetro | Tipo |
---|---|
options | { eventName: string; } |
setAutoLogAppEventsEnabled ( options : { enabled : boolean ; } ) = > Promise < void >
Parâmetro | Tipo |
---|---|
options | { enabled: boolean; } |
setAdvertiserTrackingEnabled ( options : { enabled : boolean ; } ) = > Promise < void >
Parâmetro | Tipo |
---|---|
options | { enabled: boolean; } |
setAdvertiserIDCollectionEnabled ( options : { enabled : boolean ; } ) = > Promise < void >
Parâmetro | Tipo |
---|---|
options | { enabled: boolean; } |
Suporte | Tipo |
---|---|
appId | string |
autoLogAppEvents | boolean |
xfbml | boolean |
version | string |
locale | string |
Suporte | Tipo |
---|---|
accessToken | AccessToken | null |
recentlyGrantedPermissions | string[] |
recentlyDeniedPermissions | string[] |
Suporte | Tipo |
---|---|
applicationId | string |
declinedPermissions | string[] |
expires | string |
isExpired | boolean |
lastRefresh | string |
permissions | string[] |
token | string |
userId | string |
Suporte | Tipo |
---|---|
accessToken | AccessToken | null |
Torne todas as propriedades em T opcionais
{ [P in keyof T]?: T[P]; }
Construa um tipo com um conjunto de propriedades K do tipo T
{ [P in K]: T; }