自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS設(shè)備控制打印機輸出文本教程

移動開發(fā) iOS
現(xiàn)在用移動智能設(shè)備控制家庭電器逐漸成為一種趨勢,這里介紹如何用iPad控制局域網(wǎng)內(nèi)的打印機,輸出文本,希望對你有所幫助。

在同一局域網(wǎng)下,用iPad寫一段話,然后向打印機發(fā)送一段代碼,控制打印機將這段話打印下來,這是不是很酷?

讓我們來看看是如何實現(xiàn)的吧,首先要知道打印機的ip地址,然后用socket通過打印機的ip地址進(jìn)行傳送要打印的內(nèi)容,并傳送讓打印機打印的指令即可。

代碼如下:

.h 里面

  1. #import   
  2. #import "AsyncSocket.h"  
  3.   
  4. @interface ViewController : UIViewController  
  5. {  
  6.     AsyncSocket *asyncSocket;  
  7. }  
  8.   
  9. @end  


.m里面

  1. #import "ViewController.h" 
  2.  
  3. @interface ViewController () 
  4.  
  5. @end 
  6.  
  7. @implementation ViewController 
  8.  
  9. - (void)viewDidLoad { 
  10.     [super viewDidLoad]; 
  11.     //    NSError *err = nil; 
  12.     //    if(![asyncSocket connectToHost:@"192.168.1.105" onPort:18011 error:&err]) 
  13.     //    { 
  14.     //        asyncSocket = [[AsyncSocket alloc] initWithDelegate:self]; 
  15.     // 
  16.     //        [asyncSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]]; 
  17.     // 
  18.     //        if (![self SocketOpen:@"192.168.1.105" port:18011]) 
  19.     //        { 
  20.     //            NSMutableString *sendString=[NSMutableString stringWithCapacity:1000]; 
  21.     //            [sendString appendString:@"非警務(wù)健身房"]; 
  22.     //            NSData *cmdData = [sendString dataUsingEncoding:NSUTF8StringEncoding]; 
  23.     // 
  24.     //            [asyncSocket writeData:cmdData withTimeout:-1 tag:0]; 
  25.     //        } 
  26.     //        NSLog(@"Error: %@", err); 
  27.     //    } 
  28.  
  29. -(IBAction)haha:(id)sender{ 
  30.     asyncSocket=nil; 
  31.     NSError *err = nil; 
  32.     if(![asyncSocket connectToHost:@"192.168.1.105" onPort:18011 error:&err]) 
  33.     { 
  34.         asyncSocket = [[AsyncSocket alloc] initWithDelegate:self]; 
  35.  
  36.         [asyncSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]]; 
  37.  
  38.         if (![self SocketOpen:@"192.168.1.105" port:18011]) 
  39.         { 
  40.             NSMutableString *sendString=[NSMutableString stringWithCapacity:100000]; 
  41.             [sendString appendString:@"Socket測試成功?。。。?quot;]; 
  42.             NSData *cmdData = [sendString dataUsingEncoding:NSUTF8StringEncoding]; 
  43.  
  44.             [asyncSocket writeData:cmdData withTimeout:-1 tag:0]; 
  45.         } 
  46.         NSLog(@"Error: %@", err); 
  47.     } 
  48. - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port 
  49.     NSLog(@"onSocket:%p didConnectToHost:%@ port:%hu", sock, host, port); 
  50.     [sock readDataWithTimeout:1 tag:0]; 
  51. -(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag 
  52.  
  53. - (void)onSocket:(AsyncSocket *)sock didSecure:(BOOL)flag 
  54.     NSLog(@"onSocket:%p didSecure:YES", sock); 
  55. - (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err 
  56.     NSLog(@"onSocket:%p willDisconnectWithError:%@", sock, err); 
  57. - (void)onSocketDidDisconnect:(AsyncSocket *)sock 
  58.     //斷開連接了 
  59.     NSLog(@"onSocketDidDisconnect:%p", sock); 
  60. - (void)didReceiveMemoryWarning { 
  61.     [super didReceiveMemoryWarning]; 
  62. - (void)viewDidUnload { 
  63.     asyncSocket=nil; 
  64. //打開 
  65. - (NSInteger)SocketOpen:(NSString*)addr port:(NSInteger)port 
  66.     if (![asyncSocket isConnected]) 
  67.     { 
  68.         [asyncSocket connectToHost:addr onPort:port withTimeout:-1 error:nil]; 
  69.  
  70.         NSLog(@"connect to Host:%@ Port:%d",addr,port); 
  71.     } 
  72.     return 0; 

其他的方法,還有利用UIPrintInteractionController,不過這需要打印機設(shè)置中有AirPrinter。

責(zé)任編輯:徐川 來源: csdn
相關(guān)推薦

2009-04-11 01:22:19

2011-05-06 15:00:16

老式打印機無線打印機

2011-05-07 09:59:31

施樂LED打印機普通激光打印機

2021-12-03 11:56:10

打印機攻擊漏洞

2011-08-30 17:37:32

打印機共享怎么設(shè)置共享打印機局域網(wǎng)

2011-05-04 17:23:01

打印機

2011-07-14 23:02:15

打印機評測

2011-04-26 15:31:45

聯(lián)想打印機LJ2200清零

2012-03-07 16:19:58

噴墨打印機推薦

2011-08-24 23:43:04

打印機對比評測

2011-06-21 10:00:39

惠普打印機行情

2011-03-21 10:10:38

nagios打印機

2011-05-29 11:45:39

打印機技巧

2011-05-03 16:22:33

打印機

2011-04-26 13:31:05

噴墨打印機

2009-01-03 08:57:00

網(wǎng)絡(luò)打印機共享

2011-04-21 09:11:36

惠普打印機維修代碼

2011-05-06 14:35:28

打印機維修方法

2021-12-05 12:10:05

打印機漏洞HP公司

2012-03-02 12:36:58

HP激光打印機
點贊
收藏

51CTO技術(shù)棧公眾號