2

我正在使用 UIActivityViewController 从我的应用程序中共享一些文本。我可以将文本发送到消息、电子邮件、副本等,但我无法将其发送到打印。

我看到 UIActivityTypePrint 采用 NSData 的数据类型。如何创建 UIActivityViewController 以将我的字符串作为字符串和 NSData 以使视图具有可用的打印选项?

NSString *textToShare = self.note.note;
        NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare, nil];
        shareVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
        shareVC.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeSaveToCameraRoll,  UIActivityTypePostToWeibo, nil];
4

2 回答 2

9

知道了。它不直接采用 NSData,它采用 UISimpleTextPrintFormatter。

NSString *textToShare = self.note.note;

UISimpleTextPrintFormatter *printData = [[UISimpleTextPrintFormatter alloc]
                                                         initWithText:textToShare];
NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare,printData, nil];
shareVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
shareVC.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeSaveToCameraRoll,  UIActivityTypePostToWeibo, nil];     
于 2012-12-31T16:14:47.900 回答
0

Add another item of NSData type in the array itemsToShare

NSString *textToShare = self.note.note;
NSData* data = [textToShare dataUsingEncoding:NSUTF8StringEncoding];
NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare,data, nil];
shareVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
shareVC.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeSaveToCameraRoll,  UIActivityTypePostToWeibo, nil];

OR

use dictionary to send item

NSString *textToShare = self.note.note;
NSData* data = [textToShare dataUsingEncoding:NSUTF8StringEncoding];

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:textToShare forKey:@"shareString"];
[dictionary setObject:data forKey:@"shareData"];
NSArray *itemsToShare = [[NSArray alloc] initWithObjects:dictionary, nil];
[dictionary release];
shareVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
shareVC.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeSaveToCameraRoll,  UIActivityTypePostToWeibo, nil];
于 2012-12-31T15:21:32.020 回答