概述我们平时也使用了很多的xcode插件,虽然官方对于插件制作没有提供任何支持,但是加载三方的插件,默认还是被允许的.第三方的插件,需要存放在 ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins文件夹中,后缀 ...
概述
我们平时也使用了很多的xcode插件,虽然官方对于插件制作没有提供任何支持,但是加载三方的插件,默认还是被允许的.第三方的插件,需要存放在 ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins文件夹中,后缀名必须是.xcplugin (不过其实际上是一种bundle).添加 三项我们默认工程生成的相关文件放在哪.都是 Build Locations指示的,通过Deployment Location 设置为 YES告诉工程,我们不使用这个默认的设置,而是我们自定义的
Wrapper extension 设置为 xcplugin,后缀名必须为xcplugin,否则不会被加载
接下来就是插件的实现过程了
在工程中添加一个文件 ,名称为 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点击 测试菜单可能你 会说,这样虽然是起作用了,但是,难道开发一个插件工程,没打单步调试么???,当然不是啊将Options下面的Core Location,XPC Services,View Debugging前面的勾都去掉,否则,你调试的时候,可能会直接crash当设置完后,你的工程的scheme的图标会从bundle图标变为xcode的图标再运行(这里是运行了,不是编译了)
原标题:Xcode7 插件制作入门
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。