向 ShareKit 添加打印选项
我计划在我的应用程序中使用 ShareKit。我还想要打印 (AirPrint)。我想在同一个操作表中包含所有选项(Facebook、Twitter 和打印机)。 我有什么理由不应该这样做吗? 我一直在寻找一些关于如何实现这一目标的技巧,但一无所获。有一些提示可以从 ActionSheet 中删除内容但不添加它们。打印机选项不会被视为“共享”吗?
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'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在“如何向 ShareKit 添加新操作”中介绍了向 ShareKit 添加操作的过程。描述假设您已经实现了核心 Sharekit 功能(例如,您已经实现了 ShareKit 示例程序)。
您将把新操作模板从 ShareKit Template Src 文件夹(.m 和 .h 文件)复制到您的项目中,并使用适当的名称重命名这些文件(例如“SHKPrint.m/.h”)。有一些明显的事情需要更改,例如类名称和头导入文件。将
#import "SHKSharer.h"
添加到头文件中。将类添加到
SHKSharers.plist
中的操作/服务列表中,如链接页面所述。 (在这一点上,我认为动作和服务之间确实没有区别,据我所知,它们也可以毫无问题地组合在一起。不过,一个区别是,动作被假定为不需要身份验证。requiresAuthentication
是模板中的方法。)之后,如文档所述,熟悉 了解分享流。
至少需要重写其中一个
canShare
方法,并且它们已在模板中设置,供您修改和取消注释。我想,出于您的目的,您可能希望将 canShareText 视为一种适当的共享方法,因此将该方法更改为返回 YES。然后,您将能够使用分配给 UISimpleTextPrintFormatter 或 UIMarkupTextPrintFormatter 的 printFormatter 进行打印(但必须设置 UIPrintInteractionController)。 (我认为您没有提到要打印的内容,因此您必须即兴发挥。)修改新共享器类中的
sharerTitle
方法,以显示适当的标题。此时,您应该能够运行代码并查看操作表。您的打印操作不会显示在这一张纸上,但如果您单击“更多...”按钮,它应该显示在第二张纸上。
单击该操作,除了隐藏工作表外,什么也不会发生。现在您需要添加一些代码来进行打印。 (稍后您会看到,如果您选择再次共享,新操作已添加到第一个操作表中,这是最近使用的列表。)
我提到假设您已经实现了核心 ShareKit 功能,如ShareKit示例程序。如果您转到调用 sharekit 操作表的方法(在您自己的代码中,例如响应您按下的共享按钮),您将看到一个位置来建立您想要共享的信息类型 - 例如,URL,图像、文本……这与您想要将共享信息发送到的位置无关——打印机、facebook、twitter……(我认为这是本末倒置,我已经修改了自己的实现来修复这个。)但是,暂时忽略这一点,你会想要设置如下:
这将使轮子开始运动以共享项目对象中的任何内容。 item 对象包含一个 shareType,在本例中为 SHKShareTypeText,它是一个枚举类型。
我不会深入探讨将其转化为共享结果的机制。我在睡梦中还不知道这一点,可能会误导你(如果我还没有的话)。
当您单击操作表中的项目时,SHKActionSheet 类会执行一些魔法,将新操作类(本例中为 SHKPrint)的字符串名称转换为一个类,并使用它调用一个类方法来分配和初始化同一类的实例。 (我一边打字一边做这件事,所以我跳过了“更多...”第二张纸中如何完成此操作的步骤。我将把它留给读者跟踪并看看它是如何工作的。
)是通过 SHKSharer 类中的
share
和show
方法运行的调用链来完成的。 (有一些关于自动共享的事情我不会在这里讨论,但我想你可以暂时忽略它。)这最终落在你的自定义类(SHKPrint)的send
方法上。查看您的模板文件,它将为您提供一些操作指导。有关打印的更多信息,请参阅此链接,这似乎提供了一个漂亮的打印的好例子。
我将在这里停下来有几个原因。我想我回答了你的问题,如何添加操作表。另外,我没有 AirPrint 功能来测试这里,所以我自己不能走得更远。
如果您还有其他问题,请告诉我。
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:
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
, thenshow
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 thesend
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.