问题描述
- iOS如何连接打印机,打印一段字符串
-
我想要再ipad程序里面,用代码给打印机发送一段字符串,然后打印。
各位,我的问题没有说清楚,不好意思。
情况是这样的,我现在想要连接到打印机,wifi的,然后给打印机发送一个字符串去打印。连接打印机不知道怎么连接,然后发送消息这个也不知道咋整。各位帮帮忙。
打印机和ipad在同一个局域网下,想用socket通过打印机的ip地址进行传送要打印的内容。
解决方案
.h 里面
#import <UIKit/UIKit.h>
#import "AsyncSocket.h"
@interface ViewController : UIViewController<AsyncSocketDelegate>
{
AsyncSocket *asyncSocket;
}
@end
.m里面
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// NSError *err = nil;
// if(![asyncSocket connectToHost:@"192.168.1.105" onPort:18011 error:&err])
// {
// asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
//
// [asyncSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
//
// if (![self SocketOpen:@"192.168.1.105" port:18011])
// {
// NSMutableString *sendString=[NSMutableString stringWithCapacity:1000];
// [sendString appendString:@"非警务健身房"];
// NSData *cmdData = [sendString dataUsingEncoding:NSUTF8StringEncoding];
//
// [asyncSocket writeData:cmdData withTimeout:-1 tag:0];
// }
// NSLog(@"Error: %@", err);
// }
}
-(IBAction)haha:(id)sender{
asyncSocket=nil;
NSError *err = nil;
if(![asyncSocket connectToHost:@"192.168.1.105" onPort:18011 error:&err])
{
asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
[asyncSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
if (![self SocketOpen:@"192.168.1.105" port:18011])
{
NSMutableString *sendString=[NSMutableString stringWithCapacity:100000];
[sendString appendString:@"Socket测试成功!!!!"];
NSData *cmdData = [sendString dataUsingEncoding:NSUTF8StringEncoding];
[asyncSocket writeData:cmdData withTimeout:-1 tag:0];
}
NSLog(@"Error: %@", err);
}
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"onSocket:%p didConnectToHost:%@ port:%hu", sock, host, port);
[sock readDataWithTimeout:1 tag:0];
}
-(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
}
- (void)onSocket:(AsyncSocket *)sock didSecure:(BOOL)flag
{
NSLog(@"onSocket:%p didSecure:YES", sock);
}
- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
NSLog(@"onSocket:%p willDisconnectWithError:%@", sock, err);
}
- (void)onSocketDidDisconnect:(AsyncSocket *)sock
{
//断开连接了
NSLog(@"onSocketDidDisconnect:%p", sock);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
asyncSocket=nil;
}
//打开
- (NSInteger)SocketOpen:(NSString*)addr port:(NSInteger)port
{
if (![asyncSocket isConnected])
{
[asyncSocket connectToHost:addr onPort:port withTimeout:-1 error:nil];
NSLog(@"connect to Host:%@ Port:%d",addr,port);
}
return 0;
}
解决方案二:
如果是用打印机打印text文件的话,可以用这个
- (IBAction)printContent:(id)sender {
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = self.documentName;
pic.printInfo = printInfo;
UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc]
initWithText:yourNSStringWithContextOfTextFileHere];
textFormatter.startPage = 0;
textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
textFormatter.maximumContentWidth = 6 * 72.0;
pic.printFormatter = textFormatter;
[textFormatter release];
pic.showsPageRange = YES;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(@"Printing could not complete because of error: %@", error);
}
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[pic presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler];
} else {
[pic presentAnimated:YES completionHandler:completionHandler];
}
}
解决方案三:
不知道是不是连接的问题,你是用wifi连接还是bluetooth连接的?在IOS5中不支持蓝牙连接打印。
http://support.apple.com/kb/ht3647
看一下这个网站,有列表说明。
解决方案四:
如果要打印字符串的话是NSString
,不过你需要先将字符串打印到屏幕,然后用AirPrint可以打印到打印机。
这是一个单独的进程。
解决方案五:
如果你的打印机设置中有AirPrinter,那用UIPrintInteractionController就很简单了
解决方案六:
我发现一个比较方便的
https://github.com/newOcean/printer
完整可运行的ios蓝牙打印demo
时间: 2024-10-02 22:09:46