LoginSignup
17
17

More than 5 years have passed since last update.

いつかに備えてXcode Plugin作成コトハジメ

Last updated at Posted at 2015-07-12

普段Androidをやってるんですが、仕事ではiOSをやり始めてます。

Xcodeをdisるわけじゃないけど、たまに○○な機能あったらいいなーと思うことがよくあって、Pluginとかないのかなと思って探してみたら非公式なんですが一応はありました。

ただ、欲しい機能はそれでもなかったので自分で作ってみようと考えたわけです。

で、Hello Worldレベルまで終わって、ここまででそれなりに詰まったので念のために残しておきます。

早速。

まずはプロジェクト作成お作法

プロジェクト新規作成

Bundleを選びます。

Bundle Extensionにはxcpluginと入れましょう。

info.plist

info.plistに下記の値を追加する。

key type value
XCGCReady Boolean YES
XCPluginHasUI Boolean NO
XC5Compatible Boolean YES
XC4Compatible Boolean YES
DVTPlugInCompatibilityUUIDs Array 7FDF5C7A-131F-4ABB-9EDC-8C5F8F0B8A90

DVTPlugInCompatibilityUUIDsはXcodeのVersionによって違うので、複数対応する場合は複数の値を入れれば良い。

過去のVersionなどを見たければAlcatraz-Info.plistなどを参照すると良い。

現状使ってるXcodeのDVTPlugInCompatibilityUUIDsを調べる場合は下記コマンドをTerminalで実行すれば良い。

% defaults read /Applications/Xcode.app/Contents/Info.plist DVTPlugInCompatibilityUUID

Build Setting

設定名
Installation Build Products Location $(HOME)
Installation Directory /Library/Application Support/Developer/Shared/Xcode/Plug-ins
Deployment Location YES
Embedded Content Contains Swift Code YES
Skip Install NO

Hello World

プログラム

適当にHelloPlugin.swiftというファイルを作って下記を書いてみる。

HelloPlugin.swift
import Foundation

class HelloPlugin: NSObject {

    class func pluginDidLoad(plugin: NSBundle) {
        NSLog("Hello Plugin!")
    }

}

そして、ビルド(Cmd + B)したのちに、「$(HOME)/Library/Application Support/Developer/Shared/Xcode/Plug-ins」に「HelloPlugin.xcplugin」ができていれば完了です。

下記コマンドで確認できます。

ls ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins 

確認

上記までやったら、一度Xcodeを閉じて、再度開きましょう。

すると、下記のようなダイアログがでてくるので「Load Budnle」を押します。

起動した時に、先ほどのpluginDidLoadは呼ばれ、さらにNSLogの出力は /var/log/system.logにはかれるのでこのlogを確認します。

ちなみに。。

NSLogを継承してなかったり、NSLogじゃなくてprintにした場合は出力されなかった。

まとめ

これから先が大変そうなんですが、ここまででも「Skip Install」を「No」にするとか幾つか見た限りではどこにも載ってなくて心折れかけました。

17
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
17