Chatting APi

도윤·2022년 1월 20일
0

프로젝트를 하기 앞서 채팅 기능을 사용하기로 하여 미리 샘플링을 해보기 위해 SendBird API를 이용하여 간단한 채팅 앱을 만들어보았습니다!


Requirement

  • Mac OS with developer mode enabled
  • Xcode
  • At least one device running iOS 9.0 and later
  • Swift 4.0 and later
  • Objective-C

앱을 만들기 앞서 위와 같은 요구사항을 충족하여야 사용이 가능합니다.

Before start

https://dashboard.sendbird.com/ 위 사이트에서 앱을 등록하고 APP_ID를 발급받아야 사용이 가능합니다!

또한 cocoaPod에

pod 'SendBirdUIKit'

을 추가하고 pod install을 해줘야 합니다!

Iniialize with APP_ID


위 Application ID를 복사하여 App Delegate에 작성해줘야 합니다!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        let APP_ID = "FCE91D92-1611-4134-BAC5-A3A0D5377CBD"
        // Specify your Sendbird application ID.
        
        SBUMain.initialize(applicationId: APP_ID) {
            // DB migration has started.
        } completionHandler: { error in
            // If DB migration is successful, proceed to the next step.
            // If DB migration fails, an error exists.
        }
        return true
    }

APP_ID를 작성해주고, SBUMain.initailize를 작성해야 합니다

// Initialize a SBDMain instance to use APIs in the client app.
SBDMain.initWithApplicationId(APP_ID, useCaching: false) {

} completionHandler: { error in

}

useCaching 파라미터는 app이 local storage를 채팅에 사용할지 안할지를 결정하고, local caching을 이용하기 원한다면 True로 설정해주면 된다.

MainTapBarController()

let channelListVC = ChatViewController()
        let naviVC = UINavigationController(rootViewController: channelListVC)
        naviVC.tabBarItem.image = UIImage(systemName: "message")

ChatViewController()

import SendBirdUIKit

class ChatViewController: SBUChannelListViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
    }
}

탭바의 메세지를 클릭하게 되면 다음과 같이 SBUChannelListViewController가 뜨는데, 따로 UI를 상세 지정 하지 않아도 위와 같이 UI를 사용할 수 있습니다.

rightBarButton을 클릭하면 새로운 채팅을 만드는 화면으로 이동합니다!

/// This is a function that shows the channel creation viewController with channel type.
    ///
    /// If you want to use a custom createChannelViewController, override it and implement it.
    /// - Parameter type: Using the Specified Type in CreateChannelViewController (default: `.group`)
    @objc open func showCreateChannel(type: SendBirdUIKit.ChannelType = .group)

채팅을 하고자하는 유저를 체크하고 CREATE 버튼을 누르게 되면 채팅이 만들어지게 됩니다!

이렇게 채팅창을 만들어서 대화를 하고 다시 ChannelListView로 들아가면 아래와 같이 chatting했던 방이 남아 있는것을 확인할 수 있습니다

많은 기능들이 내장되어 있지만 커스텀하는 경우가 더 많으므로 SendBirdUIKit을 확인하여 overriding하여 직접 커스텀해야 합니다!


https://sendbird.com/docs/chat/v3/ios/quickstart/send-first-message

위의 페이지를 참고하여 작성하였습니다!

0개의 댓글