•  


firebase-ios-sdk/FirebaseCombineSwift/README.md at master · NOUIY/firebase-ios-sdk · GitHub
Skip to content

Latest commit

 

History

History
162 lines (136 loc) · 4.93 KB

README.md

File metadata and controls

162 lines (136 loc) · 4.93 KB

Combine Support for Firebase

This module contains Combine support for Firebase APIs.

Note : This feature is under development and is supported only on a community basis. You can follow development on the project tracker

Installation

CocoaPods
  • Add pod 'Firebase/FirebaseCombineSwift' to your podfile:
platform
 :ios
,
 '14.0'


target
 'YourApp'
 do

  use_frameworks!


  pod
 'Firebase/Auth'

  pod
 'Firebase/Analytics'

  pod
 'Firebase/FirebaseCombineSwift'

end
Swift Package Manager
  • Follow the instructions in Swift Package Manager for Firebase Beta to add Firebase to your project
  • Make sure to import all of the following packages you intend to use:
    • FirebaseAuthCombine-Community
    • FirebaseFirestoreCombine-Community
    • FirebaseFunctionsCombine-Community
    • FirebaseStorageCombine-Community
  • In your code, import the respective module:
    • FirebaseAuthCombineSwift
    • FirebaseFirestoreCombineSwift
    • FirebaseFunctionsCombineSwift
    • FirebaseStorageCombineSwift

Usage

Auth

Sign in anonymously

  Auth
.
auth
(
)
.
signInAnonymously
(
)

    .
sink
 {
 completion 
in

      switch completion 
{

      case
 .
finished
:

        print
(
"
Finished
"
)

      case
 let
 .
failure
(
error
)
:

        print
(
"
\(
error
.
localizedDescription
)
"
)

      }

    }
 receiveValue
:
 {
 authDataResult 
in

    }

    .
store
(
in
:
 &
cancellables
)
  Auth
.
auth
(
)
.
signInAnonymously
(
)

    .
map
 {
 result 
in

      result
.
user
.
uid
    
}

    .
replaceError
(
with
:
 "
(unable to sign in anonymously)
"
)

    .
assign
(
to
:
 \
.
uid
,
 on
:
 self
)

    .
store
(
in
:
 &
cancellables
)

Sign in with a given 3rd-party credentials

In the sign(_:didSignInFor:withError:) method, get a Google ID token and Google access token from the GIDAuthentication object and asynchronously exchange them for a Firebase credential:

  func
 sign
(
_ signIn
:
 GIDSignIn
!
,
 didSignInFor user
:
 GIDGoogleUser
!
,
 withError error
:
 Error
?
)
 {

    // ...

    if 
let
 error 
=
 error 
{

      // ...

      return

    }


    guard 
let
 authentication 
=
 user
.
authentication 
else
 {
 return
 }

    let
 credential
 =
 GoogleAuthProvider
.
credential
(
withIDToken
:
 authentication
.
idToken
,

                                                   accessToken
:
 authentication
.
accessToken
)

    Auth
.
auth
(
)

      .
signIn
(
withCredential
:
 credential
)

      .
mapError
 {
 $0 
as
 NSError
 }

      .
tryCatch
(
handleError
)

      .
sink
 {
 /* ... */
 }
 receiveValue
:
 {
  /* ... */
  }

      .
store
(
in
:
 &
subscriptions
)

  }


  private
 func
 handleError
(
_ error
:
 NSError
)
 throws
 ->
 AnyPublisher
<
AuthDataResult
,
 Error
>
 {

    guard isMFAEnabled && error
.
code 
==
 AuthErrorCode
.
secondFactorRequired
.
rawValue
    
else
 {
 throw
 error 
}


    // The user is a multi-factor user. Second factor challenge is required.

    let
 resolver
 =
 error
.
userInfo
[
AuthErrorUserInfoMultiFactorResolverKey
]
 as!
 MultiFactorResolver

    let
 displayNameString
 =
 resolver
.
hints
.
compactMap
(
\
.
displayName
)
.
joined
(
separator
:
 "
 "
)


    return
 showTextInputPrompt
(
withMessage
:
 "
Select factor to sign in
\n
\(
displayNameString
)
"
)

      .
compactMap
 {
 displayName 
in

        resolver
.
hints
.
first
(
where
:
 {
 displayName 
==
 $0
.
displayName 
}
)
 as?
 PhoneMultiFactorInfo

      }

      .
flatMap
 {
 [
unowned self
]
 factorInfo 
in

        PhoneAuthProvider
.
provider
(
)

          .
verifyPhoneNumber
(
withMultiFactorInfo
:
 factorInfo
,
 multiFactorSession
:
 resolver
.
session
)

          .
zip
(
self
.
showTextInputPrompt
(
withMessage
:
 "
Verification code for 
\(
factorInfo
.
displayName 
??
 "
"
)
"
)
)

          .
map
 {
 (
verificationID
,
 verificationCode
)
 in

            let
 credential
 =
 PhoneAuthProvider
.
provider
(
)
.
credential
(
withVerificationID
:
 verificationID
,

                                                                     verificationCode
:
 verificationCode
)

            return
 PhoneMultiFactorGenerator
.
assertion
(
with
:
 credential
)

          }

      }

      .
flatMap
 {
 assertion 
in

        resolver
.
resolveSignIn
(
withAssertion
:
 assertion
)

      }

      .
eraseToAnyPublisher
(
)

  }

Functions

let
 helloWorld
 =
 Functions
.
functions
(
)
.
httpsCallable
(
"
helloWorld
"
)

helloWorld
.
call
(
)

  .
sink
 {
 completion 
in

    switch completion 
{

      case
 .
finished
:

        print
(
"
Finished
"
)

      case
 let
 .
failure
(
error
)
:

        print
(
"
\(
error
.
localizedDescription
)
"
)

    }

  }
 receiveValue
:
 {
 functionResult 
in

    if 
let
 result 
=
 functionResult
.
data 
as?
 String
 {

      print
(
"
The function returned: 
\(
result
)
"
)

    }

  }

  .
store
(
in
:
 &
cancellables
)
let
 helloWorld
 =
 Functions
.
functions
(
)
.
httpsCallable
(
"
helloWorld
"
)

helloWorld
.
call
(
"
Peter
"
)

  .
sink
 {
 completion 
in

    switch completion 
{

      case
 .
finished
:

        print
(
"
Finished
"
)

      case
 let
 .
failure
(
error
)
:

        print
(
"
\(
error
.
localizedDescription
)
"
)

    }

  }
 receiveValue
:
 {
 functionResult 
in

    if 
let
 result 
=
 functionResult
.
data 
as?
 String
 {

      print
(
"
The function returned: 
\(
result
)
"
)

    }

  }

  .
store
(
in
:
 &
cancellables
)
- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본