Xcode provides all the features you need to create an app. But because of its non-open source and the lack of documentation related to Xcode-plugin, it becomes inflexible when we need to add some of our own ideas and features. But we can use some unofficial means to expand our own xcode and share it with others.
What Xcode plug-in can do
Too much, we can automatically generate code comments (Vvdocumenter), we can directly display the color of our initialized Uicolor (colorsense-for-xcode) in the Code Editor, We can also directly display the image we want to add to the UIImage in the Code Editor (Ksimagenamed-xcode), we can also adjust the console color, modify the code style, and so on ....
We can also use Alcatraz to manage our plugins, simply convenient!
Where is Xcode plug-in put?
All Xcode plug-in will be placed in a call
~/library/application Support/developer/shared/xcode/plug-ins
File directory, and all plugins will be suffixed with. Xcplugin.
Develop your own Xcode plug-in
Preparatory work
Go to [xcode-plugin-template] (https://github.com/kattrali/Xcode-Plugin-Template) to download the templates developed by the Xcode plugin.
Copy the downloaded template to ~/library/developer/xcode/templates/project templates/application plug-in/xcode5 Plugin.xctemplate folder, if you do not have a corresponding folder, you can manually create a.
Restart Xcode and when you create a new project you can see a application plug-in option in OSX with a Xcode plug-in template.
Enter development
We created a new Xcode plug-in template project, and we can see that the template generates a lot of code for us. Look at the Initwithbundle method of the file with the same name as your project. After reading it, you'll know that the template fills us with a button that adds a do action in edit and then calls back the Domenuaction method.
We can run our project and you will find that a new Xcode is launched, because we are doing the Xcode plugin, which is of course Xcode, and it's interesting to write Xcode's code with Xcode. Try clicking on the new Xcode edit has a Do action button, click the Do action button, and then make a breakpoint in the callback to see if there is any callback method.
Dvtplugincompatibilityuuids
Yes, you will find your callback method wood has call, Wood has to call Ah, what a ghost! In fact, this is because your plugin does not know the version of Xcode you are running, it is good to introduce your Xcode version to it.
Open plug-in Project Info.plist, find Dvtplugincompatibilityuuids, open this dropdown box, here are all the Xcode versions that your plugin knows.
Enter defaults read/applications/xcode.app/contents/info dvtplugincompatibilityuuid in terminal, terminal will return a string of strings to you, This is your Xcode dvtplugincompatibilityuuid, add this string of strings to the dvtplugincompatibilityuuids.
One more run Xcode, is it possible ~
Tapping Xcode Notifications
Because Apple has not yet published the documentation for Xcode plugin. So we need to find a way through some other ideas. For example, we tap the various notifications that Xcode sends when we operate Xcode.
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (notificationlog:) Name:nil Object:nil];
Add this line of code to our project initialization
-(void) Notificationlog: (Nsnotification *) notify
{
NSLog (@ "%@", notify.name);
}
So we can listen to all the notifications that Xcode gave us when we were working on Xcode. Run a bit, is not the notice to brush the come ...
Implementing a Xcode plug-in feature
Find demand
Since we all know so much about notifications, can we optimize the xcode by grabbing one of the notifications. Now the Xcode plugin spicy, many of the features we can think of, in fact, the intentions of Google on the internet, can be found. Given the introduction, let's take a simpler function.
For example, when we want to show the location of this file in the Finder in the Xcode file navigation area, we need to right-click in this file and then click on the show in Finder from the show list, and this show in Finder button has no shortcut key. It would be nice to have a shortcut key to open it quickly.
IDEA Realization
We need to listen to the notification of the file change when the file navigation area is selected, and then save the path of the currently selected file
We need to create a new nsmenuitem and set up the shortcut keys we need.
When we press the shortcut key, we open our workspace and select the file to specify.
Talk is cheep,show u the Code
1. We need to listen to the file navigation area selected file changes when the notification, and then save the path of the currently selected file
Based on the previously monitored Xcode notification, we will find a notification called transition from one file to another, which is what we need when the selected file changes.
-(void) Notificationlog: (Nsnotification *) notify
{
if ([Notify.name isequaltostring:@ "transition from one file to another"]) {
Nsurl *originurl = [[Notify.object valueforkey:@ "Next"] valueforkey:@ "Documenturl"];
if (originurl! = Nil && [originurl absolutestring].length >= 7) {
Self.url = [originurl.absolutestring substringfromindex:7];
}
}
}
We will save this notice to our file path, why Substringfromindex:7, because the return URL is file:///User ... format, need to switch to/user ... The format.
2. We need to create a new Nsmenuitem and then set the shortcut keys we need.
This is a relatively simple modification of the template to help us generate the few lines of code, add shortcut keys can be
Nsmenuitem *menuitem = [[Nsapp mainMenu] itemwithtitle:@ "Edit"];
if (MenuItem) {
[[MenuItem submenu] Additem:[nsmenuitem Separatoritem]];
Nsmenuitem *actionmenuitem = [[Nsmenuitem alloc] initwithtitle:@ "Do action" action: @selector (Domenuaction) keyequivalent:@ "F"];
[Actionmenuitem Setkeyequivalentmodifiermask:nsshiftkeymask];
[Actionmenuitem settarget:self];
[[MenuItem submenu] additem:actionmenuitem];
}
Objective-c
The key is these two lines:
Nsmenuitem *actionmenuitem = [[Nsmenuitem alloc] initwithtitle:@ "Do action" action: @selector (Domenuaction) keyequivalent:@ "F"];
[Actionmenuitem Setkeyequivalentmodifiermask:nsshiftkeymask];
The shortcut key here is SHIFT + F, and the reader can modify it according to its own way.
3. When we press the shortcut key to open our workspace, and select to the specified file.
-(void) domenuaction
{
Self.url = [Self.url stringbyurldecodingstringparameter];
[[Nsworkspace SharedWorkspace] SelectFile:self.url Infileviewerrootedatpath:nil];
}
This is a better understanding. Pass the URL that you saved in the first step, but you need to do a urldecoding, because Xcode gives us a encode URL.
Xcode "API"
Xcode plugin development is not a new technology, so there are many developers dump out a lot of Xcode API. You can add to your project as needed.
Working with projects files
Xcodeeditor has a powerful set of APIs that manipulate project files, such as inspect headers, list frameworks, add classes, and more. If you want to work with your project files, don't forget the library.
Nstask
The Nstask function is as useful as executing command commands in terminal.
Nstask-tutorial
Cocoa
is cocoa, not cocoa touch, should be for Xcode is the app on MacOS. So writing the Xcode plugin will use a lot of cocoa APIs and need to understand cocoa programming.
Extended Reading
Introduction to XCODE5 Plugins development
Creating an Xcode plugin:a quick-start guide
Extending Xcode 6 with plugins
How to create Xcode plugin
Source: Sub-follow (subcycle)
Introduction to 17-XCODE6 plug-in development