生活随笔
收集整理的這篇文章主要介紹了
iOS设备控制打印机输出文本
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文轉(zhuǎn)載至?http://tec.5lulu.com/detail/108krn1e6e66m8sbd.html
讓我們來看看是如何實(shí)現(xiàn)的吧,首先要知道打印機(jī)的ip地址,然后用socket通過打印機(jī)的ip地址進(jìn)行傳送要打印的內(nèi)容,并傳送讓打印機(jī)打印的指令即可。
代碼如下:
.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:@"非警務(wù)健身房"]; // 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;}其他的方法,還有利用UIPrintInteractionController,不過這需要打印機(jī)設(shè)置中有AirPrinter。
轉(zhuǎn)載于:https://www.cnblogs.com/Camier-myNiuer/p/4083047.html
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎(jiǎng)!定制產(chǎn)品紅包拿不停!
總結(jié)
以上是生活随笔為你收集整理的iOS设备控制打印机输出文本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。