1. 초기 설정
- https://developers.facebook.com/docs/facebook-login/ios 에서 참고한다.
- 단 클래스이름이 좀 많이 변경되었으나 메뉴얼이 업데이트가 되지 않았다.
1) pod
pod 'FacebookCore'
pod 'FacebookLogin'
2) 권한 (info.plist)
<!-- 앱권한 -->
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
<key>FacebookAppID</key>
<string>[페북 키값]</string>
<key>FacebookDisplayName</key>
<string>보여질이름</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>fb[페북 키값]</string>
</array>
</dict>
</array>
3) AppDelegate
import UIKit
import FBSDKLoginKit
@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.
ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
KOSession.handleDidEnterBackground()
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
KOSession.handleDidBecomeActive()
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
var kakaoHandler = false
var fbHandler = false
if KOSession.isKakaoAccountLoginCallback(url) {
print("kakao open")
kakaoHandler = KOSession.handleOpen(url)
}
else
{
fbHandler = ApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
if kakaoHandler {
return true
}
else {
return false
}
}
func application(_ app: UIApplication, open url: URL, options: [String : AnyObject] = [:]) -> Bool {
if KOSession.isKakaoAccountLoginCallback(url) {
return KOSession.handleOpen(url)
}
return true
}
/*
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: AnyObject) -> Bool {
if KOSession.isKakaoAccountLoginCallback(url) {
return KOSession.handleOpen(url)
}
return true
}
func application(_ app: UIApplication, open url: URL, options: [String : AnyObject] = [:]) -> Bool {
if KOSession.isKakaoAccountLoginCallback(url) {
return KOSession.handleOpen(url)
}
return true
}
*/
}
2. 프로필 호출하기
import FacebookLogin
import FBSDKLoginKit
class LoginViewController: UIViewController {
...
@IBAction func facebookLogin(_ sender: Any) {
let loginManager = LoginManager()
loginManager.logIn(permissions: [.publicProfile], viewController: self) { (LoginResult) in
switch LoginResult {
case .failed(let error):
print(error)
case .cancelled:
print("user cancelled login")
case .success(granted: _, declined: _, token: _): // 로그인 성공
print("login")
// 프로필 가져오기
Profile.loadCurrentProfile(completion: {(profile, error) in
print(profile?.name)
print(profile?.userID)
var thumb:FBProfilePictureView = FBProfilePictureView()
thumb.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
thumb.profileID = profile?.userID ?? ""
self.view.addSubview(thumb)
})
}
}
}
...
}
'앱 개발자 역량 > IOS' 카테고리의 다른 글
swift ] addChild() (0) | 2019.05.15 |
---|---|
swift ] Codable (0) | 2019.05.13 |
Swift ] addSubView 내비게이션 바에 가려지는 오류 (0) | 2019.05.07 |
Swift) TableView (0) | 2019.04.03 |
Swift) 이미지선택 이벤트 (0) | 2019.04.03 |