首页 > 代码库 > iOS设备控制打印机输出文本
iOS设备控制打印机输出文本
本文转载至 http://tec.5lulu.com/detail/108krn1e6e66m8sbd.html
让我们来看看是如何实现的吧,首先要知道打印机的ip地址,然后用socket通过打印机的ip地址进行传送要打印的内容,并传送让打印机打印的指令即可。
代码如下:
.h 里面
- #import
- #import "AsyncSocket.h"
- @interface ViewController : UIViewController
- {
- 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 = http://www.mamicode.com/[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;
- }
其他的方法,还有利用UIPrintInteractionController,不过这需要打印机设置中有AirPrinter。
iOS设备控制打印机输出文本
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。