Write a Xcode4 plugin of your own

Source: Internet
Author: User

Transferred from: http://joeyio.com/ios/2013/07/25/write_xcode4_plugin_of_your_own/

Just write iOS program when you know that Xcode support third-party plug-ins, such as colorsense and other useful plug-ins, but Xcode plug-in development without official document support, has always felt very mysterious, then today to uncover its veil.

When Xcode starts, it checks all plugins ( ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins the bundle file with the extension) under the Plugins directory () .xcplugin and loads them. In fact, we have guessed that the plug-in will eventually be a .xcplugin bundle file extension, placed in the plugin directory for Xcode to load.

OK, let's start with a simple plugin that takes a few simple steps to complete, and my environment is Xcode 4.6.3 (4h1503).

1. Create a new Xcode Project

The Xcode plugin is actually a Mac OS X bundle, so you can refer to creating a bundle.

Give project a name and make sure not to tick Use automatic reference counting because Xcode uses a GC to manage memory, so Xcode's plug-in also needs to use GC to manage memory. Framework selection Cocoa .

2. Set the target Info

Set this information like the same

    • XC4Compatible=YES
    • XCPluginHasUI=NO
    • XCGCReady=YES
    • Principal Class= Plugin (This setting is for you 插件的名字 , in this case named Plugin )

The first three may not be the default in info, you can add it yourself, select Boolean the type, and the last one Principal Class is the String type.

3. Set up Build Settings

Then open the build Setting Tab and set these:

    • Set Installation Build Products Location to ${HOME} , Xcode will automatically convert to your current user's home path
    • Set Installation Directory to /Library/Application Support/Developer/Shared/Xcode/Plug-ins , Xcode will stitch Installation Build Products Location and Installation Directory Find your plugin as an absolute path
    • Set Deployment Location toYES
    • Set Set Wrapper extension toxcplugin

# #4. Add user-defined Settings

    • Set GCC_ENABLE_OBJC_GC tosupported
    • Set GCC_MODEL_TUNING toG5

With these settings, when you build this projct, Xcode will copy the build plugin to the plugin folder, and then we need to restart Xcode to reload the new build plugin. The development plug-in is relatively simple, debugging plug-ins is more tangled, the only way is to build, restart Xcode, to load the latest build plug-in.

The preparation is over, and the following is the implementation of our plugin.

5. Implement our Plugins

In the second step when we set up a Principal Class , then in Xcode new Objective-c class, the name and Principal Class set the value of the same. Add the methods in the implementation file + (void) pluginDidLoad: (NSBundle*) plugin . This method is called when Xcode loads the plug-in, and can be used to do some initialization operations. Usually this class is a singleton and observe to NSApplicationDidFinishLaunchingNotification get the notification that Xcode is loaded.

+(void)Plugindidload:(NSBundle*)Plugin{StaticIdSharedplugin=Nil;Staticdispatch_once_tOnce;Dispatch_once(&Once,^{Sharedplugin=[[SelfAlloc]Init];});}-(Id)Init{if  (self = [ Super init]) {[[defaultcenter] addobserver : self selector: @selector  Span class= "NL" >applicationdidfinishlaunching:) name:nsapplicationdidfinishlaunchingnotification object:nil];} return self;}         

Once you receive a notification that Xcode is loaded, you can observe other notification you need or add menu items to the menu or access UI components such as code Editor.

In our simple example, we Edit add a Custom Plugin menu item called, and set a ? + c shortcut key. Its function is to use NSAlert the text that shows us the selection in the Code Editor. We need to NSTextViewDidChangeSelectionNotification get the selected text by observing and accessing the received parameters NSTextView .

-(void)Applicationdidfinishlaunching:(Nsnotification*)Notification{[[NsnotificationcenterDefaultcenter]Addobserver:SelfSelector:@selector(Selectiondidchange:)Name:NstextviewdidchangeselectionnotificationObject:Nil];Nsmenuitem*Editmenuitem=[[NsappMainMenu]Itemwithtitle:@ "Edit"];If(Editmenuitem){[[EditmenuitemSubmenu]AddItem:[NsmenuitemSeparatoritem]];Nsmenuitem*Newmenuitem=[[NsmenuitemAlloc]Initwithtitle:@ "Custom Plugin"Action:@selector(ShowMessageBox:)Keyequivalent:@ "C"];[NewmenuitemSettarget:Self];[NewmenuitemSetkeyequivalentmodifiermask:Nsalternatekeymask];[[EditmenuitemSubmenu]AddItem:Newmenuitem];[NewmenuitemRelease];}}-(void)Selectiondidchange:(Nsnotification*)Notification{If([[NotificationObject]Iskindofclass:[NstextviewClass]]){Nstextview*TextView=(Nstextview*)[NotificationObject];Nsarray*Selectedranges=[TextViewSelectedranges];If(Selectedranges.Count==0){Return;}NsrangeSelectedrange=[[SelectedrangesObjectatindex:0]Rangevalue];NSString*Text=TextView.Textstorage.String;SelectedText=[TextSubstringwithrange:Selectedrange; }}- (void) Span class= "NF" >showmessagebox:  (id) origin Span class= "P" >{nsalert *alert = [ [[nsalert alloc] init autorelease[alert setmessagetext:  Selectedtext; [alert runmodal];}      

You will find that in the presence of SelectedText will be error, in the implementation of add can be added NSString *selectedText .

@implementation Plugin {    NSString *selectedText;}

Final effect:

6. Need to be aware of
    • ~~plugin can not use arc, need to manually manage the memory ~ ~ (Thanks to @onevcat's reminder, because it is GC, do not need to manually manage the memory)
    • Cannot debug directly, but can print out the log through NSLog in the program, and through tail -f /var/log/system.log the command to view the output of the log
    • If Xcode suddenly does not start up, it may be a problem with the plugin, run to the ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins directory, remove the plugin, restart Xcode, find out where the problem
    • If the 1-4 steps of the various settings you are more annoying, you can directly use this Xcode4 Plugin template to fix, how to use in its readme has detailed instructions,:)
Summarize

This is just a simple introduction to the Xcode plugin to write an example, but "small, perfectly formed", you can understand the Xcode plug-in something, such as the Xcode plugin is essentially a Mac OS X bundle, etc., and because there is no official Apple document support, A lot of things can only go to Google, or refer to some other plug-in implementations.

REF

This document is primarily referenced and compiled from writing YOUR OWN XCODE 4 PLUGINS, thanks to the original author blacksmith software

Another: Two days ago our little partner @onevcat wrote a Xcode plug-in Vvdocumenter, the role is in the method, class, etc. before entering three/will automatically generate the specification Javadoc document (XCODE5 will support the Javadoc type of document, For me to turn from the Java is really timely help, and quickly clone a, with very convenient, very good very strong, highly recommended! Let's quickly document our project code and greet the arrival of Xcode5,:)

Enjoy!!!

Write a Xcode4 plugin of your own

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.