I am planning on using ShareKit in my App. I would also like to have printing (AirPrint). I was thinking to have all options( Facebook & Twitter & Printer ) in the same actionsheet. Is there any reason why I shouldn't do that? I have been looking for some tips on how to achieve this but have come up blank. There are tips to remove things from the ActionSheet but not add them. Would a printer option no be considered 'sharing'?
The process of adding an action to ShareKit is covered in "How to Add a New Action to ShareKit" This description assumes you have already implemented a core Sharekit functionallity (for example, you have implemented the ShareKit example program).
You will copy New Action templates from the ShareKit Template Src folder (.m and .h files) into your project, rename the files with the appropriate name (like "SHKPrint.m/.h"). There are some obvious things to change, like the class name and the header import file. Add #import "SHKSharer.h"
to the header file.
Add the class to the actions/services list in SHKSharers.plist
, as the linked page describes. (There really is no distinction at this point between an action and a service that I can see, and they may as well be grouped together, as far as I can tell, without problem. One distinction, though, is that actions are presumed to not require authentication. requiresAuthentication
is method in the template.)
After that, as the documentation says, get familiar with Understanding the share flow.
At least one of the canShare
methods needs to be overridden, and they are already set up in the template for you to modify and uncomment. I imaging that for your purpose, you might want to consider canShareText as an appropriate method to share, so change that one to return YES. Then you will be able to print using a printFormatter assigned to either UISimpleTextPrintFormatter or UIMarkupTextPrintFormatter (but not without setting up a UIPrintInteractionController). (I don't think you mentioned what you want to print, so you'll have to improvise.)
Modify the sharerTitle
method in your new new sharer class, to show an appropriate title.
At this point, you should be able to run your code and see an action sheet. Your print action won't show up on this one, but if you click the more... button, it should show up on the second sheet.
Click that action and nothing will happen yet except the sheet will hide. Now you need to add some code to do the printing. (You will see later, if you choose to share again, the new action has been added to the first action sheet, which is a most-recently-used list.)
I mentioned that it was assumed you have already implemented a core ShareKit functionality, such as the ShareKit example program. If you go to the method that invokes the sharekit action sheet (in your own code, such as responding to your pressing the share button), you will see a place to establish what kind of information you want to share -- e.g., URL, image, text, ... This is independent of where you want to send your shared information -- printer, facebook, twitter, ... (which I think puts the cart before the horse, and I have modified my own implementation to fix this.) But, ignoring that for the time being, you will want to set it up something like this:
- (void) shareButtonPressed {
SHKItem *item = [SHKItem text:yourSimpleTextOrMarkupText];
// Get the ShareKit action sheet
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
// Display the action sheet
[actionSheet showInView:self.view];
}
This puts the wheels into motion to share whatever is in the item object. The item object includes a shareType, which in this case is SHKShareTypeText, which is an enumerated type.
I won't go into the machinery that translates this into the shared result. I don't know it in my sleep yet, and might mislead you (if I haven't already).
When you click on the item in the action sheet, the SHKActionSheet class does some magic, converting the string name of your new action class (SHKPrint in this case) into a class, and using that to invoke a class method to allocate and initialize an instance of the same class. (I am doing this as I type, so I skipped the step on how this is accomplished from the "more..." second sheet. I'll leave it to the reader to trace that and see how it works.)
The sharing is done through a call chain running through share
, then show
methods in the SHKSharer class. (There is some business about autosharing that I won't get into here, but I think you can ignore that for the time being.) This finally lands on the send
method of your custom class (SHKPrint). Look at your template file, and it will give you some guidance on what to do.
For more information on printing, see this link, which seems to give a pretty good example of what you can do to print.
I'll stop here for a couple of reasons. I think I answered your question, how to add an action sheet. Also, I don't have AirPrint capability to test with here, so I can't really go much further on my own.
Let me know if you have any other questions.