99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
月之殘骸 頭像
蘋果2袋
2
月之殘骸

Xcode7 插件制作入門

發(fā)布時間:2016-03-25 23:35  回復(fù):0  查看:2940   最后回復(fù):2016-03-25 23:35  

概述

我們平時也使用了很多的xcode插件,雖然官方對于插件制作沒有提供任何支持,但是加載三方的插件,默認還是被允許的.第三方的插件,需要存放在 ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins文件夾中,后綴名必須是.xcplugin (不過其實際上是一種bundle).
所以我們創(chuàng)建一個插件工程,直接創(chuàng)建bundle工程即可,然后通過修改后綴名為.xcplugin,將其放到~/Library/Application Support/Developer/Shared/Xcode/Plug-ins目錄中就可以了

第一個demo插件功能:在xcode的edit菜單中加入一個叫做 測試菜單 的項目,當點擊的時候,彈出一個警告框,顯示一句話,完整的工程放在TestPluginBundle

詳細過程

創(chuàng)建Bundle工程 TestPluginBundle
Xcode7 插件制作入門

工程名稱就是 TestPluginBundle

工程設(shè)置

插件工程和普通的bundle工程還是有區(qū)別的,所以需要進行特殊的設(shè)置

首先是工程的plist文件

Xcode7 插件制作入門

添加 三項

XCPluginHasUI = NO
XC4Compatible = YES
DVTPlugInCompatibilityUUIDs //這是一個數(shù)組.數(shù)組內(nèi)容字符串,指示了該插件兼容的xcode版本,只有對應(yīng)版本的xcode的UIID加入這個數(shù)組了,插件才能被加載,否則,即使你將插件放入xcode的插件文件夾,插件也不會被加載的
那么怎么獲取你當前版本的xcode的UUID呢?在terminal中輸入
defaults read /Applications/Xcode.app/Contents/Info DVTPlugInCompatibilityUUID

terminal會返回一串字符串給你,這就是你的Xcode的DVTPlugInCompatibilityUUID.

接下來是 Build Setting了

Xcode7 插件制作入門
Xcode7 插件制作入門
Installation Build Products Location 設(shè)置為 ${HOME} [顯示的時候,顯示的是你的用戶目錄],這個是products的根目錄

Installation Directory 設(shè)置為 /Library/Application Support/Developer/Shared/Xcode/Plug-ins,這個是指定你的插件安裝的目錄. 注意,這里填入的其實是相對目錄,插件的絕對目錄是這樣的,例如 /Users/yohunl/Library/Application Support/Developer/Shared/Xcode/Plug-ins/Alcatraz.xcplugin ,最后的絕對目錄是 Installation Build Products Location和Installation Directory的結(jié)合,這也是為什么兩者都要設(shè)置的原因

Deployment Location 設(shè)置為 YES,這個是指示該工程不使用設(shè)置里的build location,而是用Installation Directory來確定build后放哪兒
Xcode7 插件制作入門
我們默認工程生成的相關(guān)文件放在哪.都是 Build Locations指示的,通過Deployment Location 設(shè)置為 YES告訴工程,我們不使用這個默認的設(shè)置,而是我們自定義的

Wrapper extension 設(shè)置為 xcplugin,后綴名必須為xcplugin,否則不會被加載

接下來就是插件的實現(xiàn)過程了

在工程中添加一個文件 ,名稱為 TestPluginBundle (當然,名字隨便什么都可以),在其中添加代碼

@implementation
TestPluginBundle+(void)pluginDidLoad:(NSBundle *)plugin {
    NSLog(@"插件運行了!");
    [TestPluginBundle sharedInstance];}- (instancetype)init{    self = [super init];
        if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didApplicationFinishLaunchingNotification:) name:NSApplicationDidFinishLaunchingNotification object:nil];
    }
    return  self;
    }- (void)didApplicationFinishLaunchingNotification:(NSNotification*)noti{
        [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationDidFinishLaunchingNotification object:nil];
        NSMenuItem *menuItem = [[NSApp mainMenu] itemWithTitle:@"Edit"];
        if (menuItem) {
            [[menuItem submenu] addItem:[NSMenuItem separatorItem]];
            NSMenuItem *actionMenuItem = [[NSMenuItem alloc] initWithTitle:@"測試菜單" action:@selector(doMenuAction) keyEquivalent:@""];
            [actionMenuItem setTarget:self];
            [[menuItem submenu] addItem:actionMenuItem];
        }}- (void)doMenuAction{
            NSAlert *alert = [[NSAlert alloc] init];
            [alert setMessageText:@"測試菜單運行"];
            [alert runModal];}- (void)dealloc{
                [[NSNotificationCenter defaultCenter] removeObserver:self];
            }+ (instancetype)sharedInstance{    static id instance;
                    static dispatch_once_t onceToken;
                    dispatch_once(&onceToken, ^{        instance = [[self alloc] init];
                    });
        return instance;
}@end

ctrl+B來Build工程,查看路徑下/Library/Application Support/Developer/Shared/Xcode/Plug-ins,可以看到我們的插件TestPluginBundle.xcplugin存在了,接下來,重啟xcode
Xcode7 插件制作入門
點擊 測試菜單
Xcode7 插件制作入門
可能你 會說,這樣雖然是起作用了,但是,難道開發(fā)一個插件工程,沒打單步調(diào)試么???,當然不是啊
編輯工程的scheme,將Executable設(shè)置為Xcode.app,意思是工程調(diào)試的時候掛載到xcode中
Xcode7 插件制作入門
將Options下面的Core Location,XPC Services,View Debugging前面的勾都去掉,否則,你調(diào)試的時候,可能會直接crash
Xcode7 插件制作入門
當設(shè)置完后,你的工程的scheme的圖標會從bundle圖標變?yōu)閤code的圖標
Xcode7 插件制作入門
再運行(這里是運行了,不是編譯了)
不出意外的話,會出現(xiàn)xode啟動另外一個xcode,接下來和你普通的調(diào)試工程就是一樣的了!

說了這么多,其實只是想讓你明白一個插件的初始化的配置,調(diào)試等

上面的過程,已經(jīng)有國外大神制作成了一個 工程模板了,https://github.com/kattrali/Xcode-Plugin-Template 其支持OC和Swift,當你安裝它后,會在新建工程時候,看到 Xcode Plugin模板,使用這個模板創(chuàng)建一個新工程,以上的配置等,就都設(shè)置好了,直接運行就是一個demo了.

Xcode7 插件制作入門

您還未登錄,請先登錄

熱門帖子

最新帖子

?