
Entry and PR
Finally, it became possible to describe the process of developing the Share + plugin, which allows you to send photos directly to your photo album on VKontakte or to your wall. It is a little about the reasons which prompted the author to so frivolous an undertaking. The reason, as always, is one - laziness. It was too lazy to export from the iPhoto library to the file system those photos that you want to place on the social network VK, it is much easier to have a tool that will automatically do the majority. As a result, user actions are reduced to selecting photos and choosing how to export photos, and you don’t even need to go beyond the iPhoto interface. At the output we get approximately the following sequence of screens:

')


When you click on the Export button, the selected images will first be exported to the file system in a temporary folder, then read and sent to VK, and deleted in the final. If you want to leave a copy in the file system, you can set the appropriate setting. This process is due to the way the export system in iPhoto works and what is open to access. That is, in the closed part of the iPhoto API, there may be a way to bypass the file system and export the byte stream right away, which you then immediately send to VK, but this method is unknown to the author, although it would help to unload the file system.
Development process
Creating a plug-in preset
The main task at this stage is to see the created UI in the menu File -> Export. It is necessary to make changes to the project settings, edit the target and implement the necessary protocols. In fact, if you use the source code from the
article , there should be few changes. Although I had to try - rename classes and interface files (.xib).
It is important to remember that the entire project needs to be created without using ARC. This is due to the fact that the source code of the reference plugin was developed for iPhoto version 6, and was written before the advent of ARC. However, it is possible to use other libraries or categories written using ARC. For this you need:
1. Such categories or libraries (files of their components) add directly to the project
2. Then in the Build Phases of the project target find the Compiled Sources drop-down list.
3. Find all the files included in the necessary new libraries or categories and set the flag for the compiler: -fobjc-arc

To debug, as stated in the article, you need to specify iPhoto as the starting application. It is necessary to set the exectuable parameter in iPhoto in Edit Scheme (Edit Scheme):

After all these settings, you need to achieve the work of this plug-in work in its original form. The original implementation can only export to the file system. Next, we will expand the functionality to the final goal: export to VK.
We draw the UI for a plug-in
The reference plugin already provides the UI necessary for setting the quality of exported photos, adding EXIF information. Our task is to add elements necessary for working with VK. We need a login button (toggle Sign In / Sign Out), a drop-down list where all existing user albums will be added, a checkbox for saving a copy of the exported image in the file system and a text field for entering the name of a new album - it will appear when the corresponding item is selected. in the drop down list of albums. You need to get something similar on the output:

Further, as usual, you need to connect all our controls with their variables in the .h file (interface).
In the future, in this article I’ll bring in the code sections that relate directly to the export of photos to VK, since the rest of the code is needed to implement the process of exporting from iPhoto to the file system.
Authorization
We will send photos using the Vkontakte API (VK API). The first thing you need to log in to use the VK API. Authorization happens through OAuth2. That is, it will be necessary to work with Http requests via WebView. Add an NSWindowController with a UI that contains only a WebView, then you need to connect this WebView with a variable in the interface created by NSWindowController. Next, we implement several WebFrameLoadDelegate protocol methods. All code is entirely available on
GitHub , so I’ll give you a piece where all the magic happens. The code can be divided into two parts. The first part works out if one photo is selected for export, and the second one if a lot. In the current example, I will show only for a set of photos, since the code for one is the same as for one iteration of the loop:
int i; for(i=0; mCancelExport==NO && succeeded==YES && i<count; i++) { dest = [[self exportDir] stringByAppendingPathComponent: [NSString stringWithFormat:@"shareplus-%d.jpg", i]]; succeeded = [mExportMgr exportImageAtIndex:i dest:dest options:&imageOptions]; if (succeeded) { switch (mAlbumPopUp.selectedTag) { case ToWall: [SPVkApiClient.sharedInstance exportImageToWall:dest completion:^(NSArray *rawPhotos, NSError *error) { [fileMgr removeItemAtPath:dest error:nil]; }]; break; case ToNewAlbum: [SPVkApiClient.sharedInstance makeNewAlbumWith:[mAlbumNameTextbox stringValue] accessToken:VkUser.current.accessToken completion:^(VkAlbum *album, NSError *error) { [SPVkApiClient.sharedInstance exportImageToAlbum:dest albumId:album.Id completion:^(NSArray *rawPhotos, NSError *error) { [fileMgr removeItemAtPath:dest error:nil]; }]; }]; break; default: [SPVkApiClient.sharedInstance exportImageToAlbum:dest albumId:mAlbumPopUp.selectedTag completion:^(NSArray *rawPhotos, NSError *error) { [fileMgr removeItemAtPath:dest error:nil]; }]; break; } } }
Of course, the code still needs to be refactored, but for demonstration purposes, I will leave it in its current form. So, it all comes down to getting the full path in the file system, where we will export the selected photos. Using this path we call the method
[mExportMgr exportImageAtIndex:i dest:dest options:&imageOptions];
where
mExportMgr is the main export manager we use. The export method returns a
boolean indicating the success of the export.
Next, we check that the user chooses in the drop-down list;
mAlbumPopUp.selectedTag is responsible for this, where
mAlbumPopUp is the variable connected to the UI element in the form of the issuing list. Depending on what the user has chosen, we perform one of three operations. The names of the methods speak for themselves; they are exported to the user's wall, to a specific album and saved to a new album with the automatic creation of this album.
Conclusion
In this part of the article, it was shown how you can create your own simple plugin for iPhoto, which can make your life easier if you often use VKontakte and / or prefer to store your photos in iPhoto. For the author of the article both statements are true.
But time does not stand still; last year, Apple introduced a new way of creating extensions for applications — App Extensions; now such extensions have become systemic, that is, not for a specific program, but installed in the system and any program can use them. Plus, at the beginning of this year, OS X Yosemite 10.10.3 was released, which included a new Photos for Mac application, it was rewritten from scratch and plug-in support was no longer available. So in the next article I will tell you the process of creating an app extension, which can do the same (export photos, directly to VKontakte), but in a new way.
You can view and criticize the source code on
GitHub .
Information sources:
1.
cuboidal.org/iphoto-export-plugin . Only thanks to this article and the source code in it, I was able to start digging.
2.
habrahabr.ru/post/133504 . And this publication has helped to realize the basics of working with the VK API. Many sections of the code on the interaction of my plug-in with the VK API even migrated almost unchanged.
3rd party tools used in development:
1.
AFNetworking 2.5.0 . In my opinion, the most convenient framework for working with HTTP requests. Actively supported.
2.
SSKeychain . A very simple and convenient way to work with Keychain on Mac OS X.