Skip to content

sauravexodus/RxFacebook

Repository files navigation

Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

import FacebookLogin
import FacebookCore

class Example {
	let loginManager = LoginManager()

	func signIn() {
			loginManager.rx.logIn(with: [.publicProfile]).flatMap { [weak self] (loginResult) -> Observable<EmailResult> in
							guard let strongSelf = self else { return .just((nil, "Something went wrong")) }
							switch loginResult {
							case .cancelled: return .just((nil, "User cancelled the result"))
							case let .failed(error): return .just((nil, error.localizedDescription))
							// Extract the facebook login result here and chain it with your graph request
							case let .success(acceptedPermissions, declinedPermissions, accessToken): return strongSelf.fetchEmail()
							}
					}
					.subscribe(onNext: { email, error in
							if let `email` = email {
									print(email)
							} else if let `error` = error {
									print(error)
							}
					})
					.disposed(by: disposeBag)
	}

	func fetchEmail() -> Observable<EmailResult> {
			let graphRequest  = GraphRequest(graphPath: "/me")
			return graphRequest.rx.getResponse().map { result in
					switch result {
					case let .success(response):
							guard let objectMap = response.dictionaryValue, let email = objectMap["email"] as? String else { return (nil, "Couldn't find email") }
							return (email, nil)
					case let .failed(error):
							return (nil, error.localizedDescription)
					}
			}
	}
}

Requirements

pod 'RxSwift'
pod 'FacebookCore'
pod 'FacebookLogin'

Installation

RxFacebook is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'RxFacebook'

Author

Sourav Chandra RxSwift Enthusiast saurav.chandra1992@live.com

License

RxFacebook is available under the MIT license. See the LICENSE file for more info.