zoukankan      html  css  js  c++  java
  • iOS设备控制打印机输出文本

    本文转载至 http://tec.5lulu.com/detail/108krn1e6e66m8sbd.html

    让我们来看看是如何实现的吧,首先要知道打印机的ip地址,然后用socket通过打印机的ip地址进行传送要打印的内容,并传送让打印机打印的指令即可。

    代码如下:

    .h 里面

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

    .m里面

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

    其他的方法,还有利用UIPrintInteractionController,不过这需要打印机设置中有AirPrinter。

  • 相关阅读:
    Redis 安装(Windows)
    etcd简介与应用场景
    Nginx+SignalR+Redis(二)windows
    Nginx+SignalR+Redis(一)windows
    Windows 版MongoDB 复制集Replica Set 配置
    走进异步世界async、await
    认识和使用Task
    进程、应用程序域、线程的相互关系
    ASP.NET Core实现类库项目读取配置文件
    用idea做springboot开发,设置thymeleaf时候,新手容易忽略误区
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/4083047.html
Copyright © 2011-2022 走看看