chatting

이승연
이승연
Jul 25, 2017 · 6 min read

[주의] 개인 공부용으로 기록하는 것입니다. 아래 사이트를 참고하세요.

18.06.29 — 별 영양가 없는 포스팅인데 조회수가 자꾸 늘어나서.. 삭제하긴 아까워서 제목을 변경하여 검색되지 않도록 수정.

위 사이트를 참고하여 만들어보겠다.

1. xcode에서 프로젝트 생성

singleview 템플릿 이용하여 프로젝트를 생성.

나는 프로젝트 이름을 ChatApp이라고 했다.

2. cocoapod 설치

루비가 있다면 gem을 이용하여 바로 cocoapod을 설치할 수 있다.

gem install cocoapods

나는 이 방법이 있는 줄 모르고 cocoapod 홈페이지에 가서 설치했다.ㅠㅠ

3. Podfile 생성

프로젝트 폴더로 가서 아래 코드를 입력한다.

pod init

참고로 프로젝트.xcodproj 파일이 있는 곳이다.

그러면 Podfile이 생성된다.

Podfile이 생성된 것을 알 수 있다.

4. Podfile 수정

Podfile을 연다.

open Podfile

#Pods for __프로젝트 이름__ 밑에 아래 코드를 추가한다.

pod ‘PusherSwift’
pod ‘AlamofireImage’

내 프로젝트 이름은 ChatApp이므로 #Pods for ChatApp 아래에 코드를 추가했다.

블럭한 곳이 추가한 코드이다.

참고로 내가 참고한 사이트에서 하라는 대로 따라하면 에러가 난다ㅠㅠ

end를 빼고 저 두줄만 넣어줘야 문제 없이 잘 돌아간다.

5. pod install

pod install

이렇게 나오면 성공한 것이다.

아래에 ChatApp.xcodeproj가 생성된 것을 볼 수 있다!

로그인 뷰 만들기

이제 로그인 뷰를 만들어보겠다.

  1. ViewController 를 LoginViewController로 이름을 바꿔준다. 이왕이면 클래스 이름도 같이 바꿔주기.

2. Main.storyboard에 아이디를 넣을 textfield와 로그인 button을 만들어줌.

class를 LoginViewController로 바꿔줌.

주의할 점! Main.storyboard에서 ViewController의 위쪽 노란색 동그라미를 누른 후에 class를 바꿔야한다.

노란색 버튼을 선택한 모습이다. 내가 선택했기 때문에 파란색 안에 노란색버튼이 보인다.

control을 누른 상태에서 드래그하여 추가

  1. textfield

connection: Outlet

이름: twitterHandle

2. button

connection: Action

이름: loginButtonClicked

LoginViewController.swift 의

@IBAction func loginButtonClicked(_ sender: Any) { }

안에 다음 코드를 추가

if(twitterHandle.hasText){
let messagesViewController = self.storyboard?.instantiateViewController(withIdentifier: "chatViewController") as! ChatViewController
messagesViewController.twitterHandle = twitterHandle.text!
self.present(messagesViewController, animated:true)
}
else{
print("No text in textfield")
}

챗 뷰(Chat View)만들기

위의 코드에서 등장하는 ChatViewController를 만들어보겠다.

  1. Main.storyboard에서 새로운 viewcontroller 생성

2. ChatViewController.swift 생성

Cocoa Touch Class 선택

subclass는 UIViewController선택

3. 추가한 viewcontroller와 ChatViewController를 연결.

앞에서 viewcontroller와 LoginViewController를 연결해준 것처럼 하면 된다.

4. 스토리보드의 ViewController에 TableView, Text Field, Button을 추가.

이렇게 배치한다.

5. pusher에 가입한다.

앱을 생성하고 Add Keys로 가서 자신의 키를 확인한다.

다시 xcode로 돌아간다.

ChatViewController의 viewDideLoad() 안에 아래 코드를 입력한다.

두번째 줄의 “” 안에는 자신의 pusher key를 넣어야한다.

만약 자신의 코드가 2323이라면 아래처럼 작성하면 된다.

pusher = Pusher(
key: "2323"
)
key가 abcdef~~~라는 가정하에 저렇게 작성하였다.

    이승연

    Written by

    이승연

    👩‍💻 세상의 모든 에러를 경험하는 중. https://github.com/sweepty

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade