반응형

여러번의 구글 로그인을 iOS 에서 구현했으나 할때마다 뭔가 동일하게 구현이 안되는듯 하는 느낌이여서 한번 정리를 해둔다.

 

Google 클라우드 콘솔에서 프로젝트 생성 키 발급 등은 

https://developers.google.com/identity/sign-in/ios/start-integrating?hl=ko 

 

iOS 및 macOS용 Google 로그인 시작하기  |  Authentication  |  Google for Developers

이 페이지는 Cloud Translation API를 통해 번역되었습니다. Switch to English 의견 보내기 iOS 및 macOS용 Google 로그인 시작하기 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류

developers.google.com

에서 주는 가이드를 따라하고,

 

SPM 을 통해 패키지를 설치하면 라이브러리 인식이 안되는 이슈가 종종 발생해서 Pod install 을 통해 패키지를 설치했다.

 

  1. pod 'GoogleSignIn'

info.plist에

<key>GIDClientID</key>
<string>YOUR_IOS_CLIENT_ID</string>
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string>
    </array>
  </dict>
</array>

넣어주고

 

코드상으로는 구글 사인버튼을 만들어서 어쩌니 하는거 다 귀차나서 어차피 커스텀 버튼으로 만들꺼 스토리보드에서 버튼 만들어서 액션 연결.

 

라이브러리 임포트

import GoogleSignIn

 

델리게이트 설정

class SignVC: UIViewController,GIDSignInDelegate{

 

 // MARK: - Google Login
    @IBAction func loginGoogle(_ sender: Any) {
        GIDSignIn.sharedInstance()?.presentingViewController = self
        GIDSignIn.sharedInstance().delegate = self
        GIDSignIn.sharedInstance().signIn()

    }
    
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if let error = error {
            if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
                print("The user has not signed in before or they have since signed out.")
            } else {
                print("\(error.localizedDescription)")
            }
            return
        }
            
        // 사용자 정보 가져오기
        if let userId = user.userID,                  // For client-side use only!
            let idToken = user.authentication.idToken, // Safe to send to the server
            let fullName = user.profile.name,
            let givenName = user.profile.givenName,
            let familyName = user.profile.familyName,
            let email = user.profile.email {
                
            print("Token : \(idToken)")
            print("User ID : \(userId)")
            print("User Email : \(email)")
            print("User Name : \((fullName))")

        } else {
            print("Error : User Data Not Found")
        }
    }
        
    // 구글 로그인 연동 해제했을때 불러오는 메소드
    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
        print("Disconnect")
    }

 

 

해서 액션걸고 로그인하려면 OAuth 어쩌고 저쩌고 하면서 액세스 디나이 뜨는 경우 있는데

 

https://console.cloud.google.com/apis/credentials/consent?project=

 

Google 클라우드 플랫폼

로그인 Google 클라우드 플랫폼으로 이동

accounts.google.com

 

들어가서 사용자유형 외부(난 외부메일로 테스트 할꺼라) 상태 테스트, 하고 테스트 사용자에 로그인 테스트 할 계정 추가해주면 해결.

Posted by npre
,
반응형

LoginView 라는 SwiftUI 파일 생성

 

기존 UIKit 의 VC파일에서

SwiftUI를 import

 

let loginView = LoginView()

let hostCon = UIHostingController(rootView: loginView)

 

와 같이 UIHostingController를 사용하여 swiftUI 파일을 UIViewcontrollelr등을 땡기는 것과 같이 사용 가능

 

 

 

//
//  LoginView.swift
//  GUIVINGKOREA
//
//  Created by JangHyun on 2023/07/20.
//

import SwiftUI

struct LoginView: View {
    var body: some View {
        VStack{
            Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
            Text("2")
        }
        
    }
}

struct LoginView_Previews: PreviewProvider {
    static var previews: some View {
        LoginView()
    }
}
//
//  SplashVC.swift
//  GUIVINGKOREA
//
//  Created by JangHyun on 2023/07/20.
//

import UIKit
import SwiftUI
class SplashVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
       
        self.swiftUIMoveTest()
    }
    
    func swiftUIMoveTest(){
        let loginView = LoginView()
        let hostCon = UIHostingController(rootView: loginView)
        APP.nav.pushViewController(hostCon, animated: true)
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
Posted by npre
,
반응형

iOS13으로 업데이트 되면서 Scene delegate 라는것이 생겼었다.

새로 학습할 시간도 없고 내용파악도 귀찮아서

info.plist의

	<key>UIApplicationSceneManifest</key>
	<dict>
		<key>UIApplicationSupportsMultipleScenes</key>
		<false/>
		<key>UISceneConfigurations</key>
		<dict>
			<key>UIWindowSceneSessionRoleApplication</key>
			<array>
				<dict>
					<key>UISceneConfigurationName</key>
					<string>Default Configuration</string>
					<key>UISceneDelegateClassName</key>
					<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
					<key>UISceneStoryboardFile</key>
					<string>Main</string>
				</dict>
			</array>
		</dict>
	</dict>

를 지워버리고

 

AppDelegate의

 

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }

를 날려버려서 기존 AppDelegate만 있을 때 와 동일하게 프로젝트를 생성, 관리했었으나

 

이제는 SceneDelegate에 대하여 정확히 이해하고 사용하려고 메모를 남긴다.

 

골자는 iOS13부터 멀티 window를 지원하므로

 

앱이 백그라운드로 떨어지는것, 다시 올라오는것 등의 UI변화를 SceneDelegate로 이관한다는 것.

 

크게 변하는것 없이 기존 AppDelegate didFinish~ 에  구현하던 초기화 코드를

 

SceneDelegate wiillConnet~ 으로 옮겨준다.

 

나는 navigationController를 앱 전역에서 자주 접근하므로

 

appdelegate에 nav con을 만들어 두고

 

Scene에서 아래와 같이 구현,

 

        APP.nav = UINavigationController.init(rootViewController: vc)

        APP.nav.setNavigationBarHidden(true, animated: false)

        APP.nav.modalPresentationStyle = .fullScreen

        self.window?.rootViewController = APP.nav

        self.window?.makeKeyAndVisible()

 

 

 

SceneDelegate로 전환 후 APP.window 접근 에러가 발생하는데

 

https://xodhks0113.blogspot.com/2020/03/ios13-window-scenedelegate.html

 

iOS13 window 접근 에러 (SceneDelegate 변경 접근하기)

iOS 개발 및 일상에 대한 블로그 입니다.

xodhks0113.blogspot.com

 

를 참고하여 해결.

Posted by npre
,
반응형

iOS 13으로 업데이트 되면서 Modal로 띄우던 VC에 대한 기본값이 수정되었다 한다.

 

이게 문제가 되는게 상단영역에 대한 스타일만 수정이 되면 괜찮은데

해당 default 값으로 모달이 뜨는경우 하단에 뷰가 남아 있는 상태로 인지되어서인지 모달로 뜬 VC를 Dismiss해도 밑에 깔려 있는 VC에서 DidAppear 등이 콜이 되지 않는 현상이 생긴다.

 

기본 설정값이나 여타 사항들은 타 블로그에 잘 설명되어있고, 

현재 개발 중이던, 운영중인 프로젝트에서 해당 변경 사항에 대하여 조치는 취해야 하니 급한대로 적용한 코드

 

        let con:ChatSelectImgViewController = ChatSB.instantiateViewController(withIdentifier: "ChatSelectImgViewController") as! ChatSelectImgViewController
        con.modalPresentationStyle = .fullScreen
        self.present(con, animated: false, completion: nil)

 

위처럼 현재 VC에서 present 할 VC의 스타일 속성을 .fullScreen으로 두어 우선은 기존의 didAppear 등의 코드가 작동하도록 처리.

 

.present 로 전체검색 후 각각 변경하는 적당한 노가다는 함..

 

Posted by npre
,