本文转载至 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。

iOS设备控制打印机输出文本的更多相关文章

  1. 在MVC视图的代码块中,直接输出文本,有几种方式?

    @{ <div>我爱IT,我是程序员,我骄傲---</div> <!--在C#代码块中,输出文本--> <!--1.0 使用Razor内置标签text--&g ...

  2. 调试location指令时,直接让location输出文本

    有时候我们调试location指令时希望location指令能够直接输出文本,这样能够方便我们进行调试.这时我们可以使用echo模块实现,但是大多数情况我们没有安装这个模块,那么我们还可以使用另一个方 ...

  3. IOS开发UI基础文本属性Attributes

    文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFo ...

  4. ios 调用打印机

    源码 无意中玩一个demo发现调用了打印机  才发现ios有快速调用打印机的功能. if ([UIPrintInteractionController isPrintingAvailable] == ...

  5. 转:MVC2表单验证失败后,直接返回View,已填写的内容就会清空,可以这样做;MVC2输出文本;MVC2输出PDF文件

    ViewData.ModelState.AddModelError("FormValidator", message); foreach (string field in Requ ...

  6. iOS 自定义日志输出

    在做iOS开发过程中,我们经常需要输出日志来查看某些数据是否打印出来,或者查看查个类是否被调用了. 系统默认的是NSLog(@"xxxx %d",1) ,但如果该APP要发布到商店 ...

  7. 如何使用 awk 输出文本中的字段和列

    首先我们要知道,awk 能够自动将输入的行,分隔为若干字段.每一个字段就是一组字符,它们和其他的字段由一个内部字段分隔符分隔开来. 如果你熟悉 Unix/Linux 或者懂得 bash shell 编 ...

  8. 使用awk格式化输出文本

    注意:本文并不是一篇awk入门文章,而是偏重实例讲解 awk借鉴了c语法,因此awk在许多地方还保留有c语言的痕迹,比如printf语句:for,if的语法结构等 介绍 最简单地说,AWK 是一种用于 ...

  9. iOS学习——NSLog输出各种类型

    在开发过程中,在调试过程中经常打印不出自己想要的数据格式,还时常报警告,所以整理了一下iOS中用NSLog打印各种数据类型的样式.整型占位符说明 : %d : 十进制整数, 正数无符号, 负数有 “- ...

随机推荐

  1. stein法求gcd 学习笔记

    原理显然 由于当x,y都为奇数时进行辗转相见 每次减完必有偶数 而偶数最多除log次 那么也最多减log次 复杂度有保证 注:代码未验证 int gcd(int x,int y){ int res=1 ...

  2. UVa1362 Exploring Pyramids

    区间dp,枚举走完第一个子树之后回到根节点的位置. /*by SilverN*/ #include<algorithm> #include<iostream> #include ...

  3. python3基础语法(标识符,注释,数据类型,获取用户输出,代码块,python保留字)

    编码 默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -*- coding: cp-1252 -*- ...

  4. 計算 battery impedence

    Origin 一顆電池被拉載後,會產生電流及電壓如下圖, 如何計算其電池內阻呢 其公式為 R = |delta(V) / delta(I)| 公式推導如下: V1 = 10 - I1R --- 左圖 ...

  5. Ubuntu 16.04 LTS 成功编译 Android 6.0 源码教程

    sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386 \  libx11-dev:i386 ...

  6. upper_bound()和lower_bound()

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

  7. SQL Server 触发器 详细讲解

    最近在做微信活动,需要用到存储过程,在网上找了下感觉使用触发器更好些,和大家分享下 希望对你有用. 触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动 ...

  8. NYOJ90 整数划分(经典递归和dp)

    整数划分 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 将正整数n表示成一系列正整数之和:n=n1+n2+…+nk,  其中n1≥n2≥…≥nk≥1,k≥1.  正 ...

  9. PyTorch学习问题记录

    Q1:def train() 中的model.train()的作用是什么?为什么要写? A1:class torch.nn.Module中 train(mode=True) Sets the modu ...

  10. python 工具 图片批量合并

    注:代码两处设置 region = (4,3,x-3,y-5) 目的是crop剪去图片的白边,这个可以视情况改变 图片需要命名为   x_1.png   .....这样的格式 #encoding=ut ...