轉自:http://joeyio.com/ios/2013/07/25/write_xcode4_plugin_of_your_own/ios
剛寫iOS程序的時候就知道Xcode支持第三方插件,好比ColorSense等很實用的插件,但Xcode的插件開發沒有官方的文檔支持,一直以爲很神祕,那今天就來揭開它的面紗。git
在Xcode啓動的時候,它會檢查插件目錄(~/Library/Application Support/Developer/Shared/Xcode/Plug-ins
)下全部的插件(擴展名爲.xcplugin
的bundle文件)並加載他們。其實到這裏咱們就猜到了,咱們作的插件最終會是一個擴展名爲.xcplugin
的bundle文件,放在插件目錄下供Xcode加載。github
OK,咱們先作一個簡單的插件,須要很簡單的幾個步驟便可完成,個人環境是Xcode 4.6.3 (4H1503)。xcode
Xcode插件其實就是一個Mac OS X bundle,因此能夠參考下圖建立一個Bundle。 app
給Project起個名字,並確保不要勾選Use automatic reference counting
,由於Xcode是使用GC來管理內存的,因此Xcode的插件也須要是用GC來管理內存的。Framework選擇Cocoa
。編輯器
像下圖同樣設置這些信息ui
XC4Compatible
= YES
XCPluginHasUI
= NO
XCGCReady
= YES
Principal Class
= Plugin
(這個設置爲你插件的名字
,本例中命名爲Plugin
)前三個可能Info裏缺省沒有,能夠本身添加,都選Boolean
類型,最後一個Principal Class
是String
類型。 spa
而後打開Build Setting Tab,設置這些:插件
Installation Build Products Location
爲${HOME}
,Xcode會自動轉換爲你當前用戶的Home路徑Installation Directory
爲/Library/Application Support/Developer/Shared/Xcode/Plug-ins
, Xcode會把拼接Installation Build Products Location
和Installation Directory
爲一個絕對路徑來查找你的插件Deployment Location
爲 YES
Set Wrapper extension
爲 xcplugin
##4. 添加 User-Defined 設置調試
GCC_ENABLE_OBJC_GC
爲 supported
GCC_MODEL_TUNING
爲 G5
有了這些設置,每次build這個Projct的時候,Xcode就會把build後的插件copy到plugin文件夾下,而後咱們須要重啓Xcode來從新加載新build的插件。開發插件相對來講簡單一些,調試插件就比較糾結了,惟一的辦法就是build以後,重啓Xcode,來加載最新build的插件。
準備工做已經結束,下面開始實現咱們的插件。
在第二步的時候咱們設置了一個Principal Class
,那麼在Xcode裏新建Objective-C類,名字和Principal Class
設置的值保持一致。在實現文件中添加上+ (void) pluginDidLoad: (NSBundle*) plugin
方法。 該方法會在Xcode加載插件的時候被調用,能夠用來作一些初始化的操做。一般這個類是一個單例,並Observe了NSApplicationDidFinishLaunchingNotification
,用來得到Xcode加載完畢的通知。
+ (void) pluginDidLoad: (NSBundle*) plugin { static id sharedPlugin = nil; static dispatch_once_t once; dispatch_once(&once, ^{ sharedPlugin = [[self alloc] init]; }); } - (id)init { if (self = [super init]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidFinishLaunching:) name:NSApplicationDidFinishLaunchingNotification object:nil]; } return self; }
一旦接收到Xcode加載完畢的通知,就能夠Observe須要的其餘notification或者在菜單中添加菜單項或者訪問Code Editor之類的UI組件。
在咱們的這個簡單例子中,咱們就在Edit
下添加一個叫作Custom Plugin
的菜單項,並設置一個⌥ + c
快捷鍵。它的功能是使用NSAlert
顯示出咱們在代碼編輯器中選中的文本。咱們須要經過觀察NSTextViewDidChangeSelectionNotification
並訪問接收參數中的NSTextView
,來得到被選中的文本。
- (void) applicationDidFinishLaunching: (NSNotification*) notification { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectionDidChange:) name:NSTextViewDidChangeSelectionNotification object:nil]; NSMenuItem* editMenuItem = [[NSApp mainMenu] itemWithTitle:@"Edit"]; if (editMenuItem) { [[editMenuItem submenu] addItem:[NSMenuItem separatorItem]]; NSMenuItem* newMenuItem = [[NSMenuItem alloc] initWithTitle:@"Custom Plugin" action:@selector(showMessageBox:) keyEquivalent:@"c"]; [newMenuItem setTarget:self]; [newMenuItem setKeyEquivalentModifierMask: NSAlternateKeyMask]; [[editMenuItem submenu] addItem:newMenuItem]; [newMenuItem release]; } } - (void) selectionDidChange: (NSNotification*) notification { if ([[notification object] isKindOfClass:[NSTextView class]]) { NSTextView* textView = (NSTextView *)[notification object]; NSArray* selectedRanges = [textView selectedRanges]; if (selectedRanges.count==0) { return; } NSRange selectedRange = [[selectedRanges objectAtIndex:0] rangeValue]; NSString* text = textView.textStorage.string; selectedText = [text substringWithRange:selectedRange]; } } - (void) showMessageBox: (id) origin { NSAlert *alert = [[[NSAlert alloc] init] autorelease]; [alert setMessageText: selectedText]; [alert runModal]; }
你會發如今出現selectedText的地方會報錯,在實現裏添加上NSString *selectedText
便可。
@implementation Plugin { NSString *selectedText; }
最終效果:
tail -f /var/log/system.log
命令來查看輸出的日誌~/Library/Application Support/Developer/Shared/Xcode/Plug-ins
目錄下,把插件刪掉,restart Xcode,查找問題在哪這只是一個簡單的Xcode插件的入門編寫示例,不過「麻雀雖小,五臟俱全」,能夠了解到Xcode的插件一些東西,好比Xcode插件本質上其實就是一個Mac OS X bundle等等,並且由於沒有Apple官方的文檔的支持,不少東西只能去Google,或者參考別人插件的一些實現。
本文主要參考和編譯自WRITING YOUR OWN XCODE 4 PLUGINS,感謝原做者Blacksmith Software
另: 前兩天咱們的小夥伴@onevcat寫了一個Xcode插件VVDocumenter,做用是在方法、類等前面輸入三個/就會自動生成規範的JavaDoc文檔(Xcode5中將支持JavaDoc類型的文檔,對於我這樣從Java轉過來的來講是真是雪中送炭),趕忙clone了一個,用起來很方便,很好很強大,強烈推薦! 趕忙把咱們的項目代碼文檔化起來,迎接Xcode5的到來吧,:)
Enjoy!!!