xcode macos app 개발하기

11596 단어 swiftmacXcodeXcode

요구사항

Torrent 파일을 실행 시, Transmission 서버로 업로드 됐으면 좋겠다.
업로드 후엔 자동으로 창이 닫히면 좋겠다.
Server url, username, password를 로컬에 저장해두고 쓰고 싶다.

구현

Torrent 파일을 실행 시 프로그램이 연결

이 방법으로 연결 하면 된다.
이렇게 연결하기 위해서는 .app 로 나와야한다.
처음에는 쉽게 Go로 만들어서 컴파일하면 실행 파일 경로가 매개변수로 넘어올 줄 알았지만 어림도 없었다.
.app 은 단순한 폴더 구조이다.

torrent-opener.app
- Contents
-- Info.plist
-- MacOS
--- 실행파일

단순히 프로그램을 실행 시키고 싶은 사람은 이렇게 직접 폴더 구조를 만들어도 된다.
하지만 파일 열기 같은 이벤트를 연결하기 위해서 나는 Xcode를 사용해서 만들었다.

찾아보니, 파일 열기로 APP 실행 시
AppDelegate - func application(_ application: NSApplication, open urls: [URL])
이벤트가 호출 된다고 한다.

직접 넣고 테스트 해보니 안된다.
어떤 파일을 넣을 것인지 미리 선언해야한다.

Xcode에서 설정하는걸로는 불가능해서 직접 테스트 에디터를 이용해서 값을 입력했다.
프로젝트 내 - Info.plist

<key>CFBundleDocumentTypes</key>
	<array>
		<dict>
			<key>CFBundleTypeName</key>
			<string>torrent</string>
			<key>CFBundleTypeRole</key>
			<string>Editor</string>
			<key>CFBundleURLTypes</key>
			<array>
				<dict>
					<key>CFBundleURLName</key>
					<string>me.mingky.torrentfile</string>
					<key>CFBundleURLSchemes</key>
					<array>
						<string>torrent</string>
					</array>
				</dict>
			</array>
			<key>LSHandlerRank</key>
			<string>Default</string>
			<key>LSItemContentTypes</key>
			<array>
				<string></string>
			</array>
		</dict>
	</array>

이걸 넣어야 저 이벤트가 호출된다.

transmission rpc를 이용해서 업로드 하면 되는데, 당연히 라이브러리가 존재할 것으로 생각하고 검색을 했다.
https://github.com/jameshurst/Transmission-Swift
이 라이브러리를 사용했다.
라이브러리 추가 방법


이렇게 추가하면 사용 할 수 있다.

이 라이브러리는 신기한 구조가 있어서 재밌었다.

import Combine
import Transmission

var cancellables = Set<AnyCancellable>()

let client = Transmission(baseURL: URL(string: "https://my.torrent.server")!, username: nil, password: nil)
client.request(.rpcVersion)
    .sink(receiveCompletion: { _ in }, receiveValue: { rpcVersion in
        print("RPC Version: \(rpcVersion)")
    })
    .store(in: &cancellables)

combine 이라는 라이브러리인가 보다. 자세히는 아직 모르는 상황이다.

프로그램 종료는

DispatchQueue.main.async {
	NSApplication.shared.terminate(self)
}

combine은 메인쓰레드에서 안돌아서 dispatchQueue를 이용해서 호출했다.

완성

https://github.com/mingkyme/torrent-opener

아이콘을 만들었는데 병따개 생각하고 만들었는데 만들고 보니 USB를 더 닮았다.

좋은 웹페이지 즐겨찾기