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 = [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设备控制打印机输出文本的更多相关文章
- 在MVC视图的代码块中,直接输出文本,有几种方式?
@{ <div>我爱IT,我是程序员,我骄傲---</div> <!--在C#代码块中,输出文本--> <!--1.0 使用Razor内置标签text--&g ...
- 调试location指令时,直接让location输出文本
有时候我们调试location指令时希望location指令能够直接输出文本,这样能够方便我们进行调试.这时我们可以使用echo模块实现,但是大多数情况我们没有安装这个模块,那么我们还可以使用另一个方 ...
- IOS开发UI基础文本属性Attributes
文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFo ...
- ios 调用打印机
源码 无意中玩一个demo发现调用了打印机 才发现ios有快速调用打印机的功能. if ([UIPrintInteractionController isPrintingAvailable] == ...
- 转:MVC2表单验证失败后,直接返回View,已填写的内容就会清空,可以这样做;MVC2输出文本;MVC2输出PDF文件
ViewData.ModelState.AddModelError("FormValidator", message); foreach (string field in Requ ...
- iOS 自定义日志输出
在做iOS开发过程中,我们经常需要输出日志来查看某些数据是否打印出来,或者查看查个类是否被调用了. 系统默认的是NSLog(@"xxxx %d",1) ,但如果该APP要发布到商店 ...
- 如何使用 awk 输出文本中的字段和列
首先我们要知道,awk 能够自动将输入的行,分隔为若干字段.每一个字段就是一组字符,它们和其他的字段由一个内部字段分隔符分隔开来. 如果你熟悉 Unix/Linux 或者懂得 bash shell 编 ...
- 使用awk格式化输出文本
注意:本文并不是一篇awk入门文章,而是偏重实例讲解 awk借鉴了c语法,因此awk在许多地方还保留有c语言的痕迹,比如printf语句:for,if的语法结构等 介绍 最简单地说,AWK 是一种用于 ...
- iOS学习——NSLog输出各种类型
在开发过程中,在调试过程中经常打印不出自己想要的数据格式,还时常报警告,所以整理了一下iOS中用NSLog打印各种数据类型的样式.整型占位符说明 : %d : 十进制整数, 正数无符号, 负数有 “- ...
随机推荐
- 【CF645D】 Robot Rapping Results Report(拓扑排序,二分)
题意:有一张N点M边的有向图,求最小的K使根据前K条边就能够确定图是否有唯一的拓扑序, 若没有唯一拓扑序输出-1 思路:二分答案再拓扑排序,以入度为0的节点作为新的一层,若某一层的节点个数<&g ...
- 学习javascript设计模式之装饰者模式
1.装饰者模式定义:给对象动态添加职责的方式称为装饰者(decorator)模式. js如何实现装饰者模式 通过保存原函数引用方式改写某函数 window.onload = function(){al ...
- udp 多播
先来了解下UDP UDP 是UserDatagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参考模型中一 ...
- Codechef FNCS Chef and Churu
Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...
- ios 使用keychain具体方法
Dictionary 写入: if ([self.currentUserAccount length] > 0) { Keycha ...
- Code Sign error: a valid provisioning profile matching the application's Identifier 'com. sensoSource.VoiceRecorder' could not be found
如果不是com. sensoSource.VoiceRecorder,在xxx-info.plist里Bundle identifier里替换成你的证书名 xxx是你的工程名 在Bundle iden ...
- VMWare 无损扩展磁盘大小
1. 所需文件(gparted) 可以去gparted主页下载LiveCD 下载地址:http://sourceforge.net/projects/gparted/files/gparted/ 或百 ...
- Unity -- 入门教程二
为了接下来要做的小游戏,在这里我要小小的修改一下移动的代码. public class PlayerMove : MonoBehaviour { //定义移动的速度 public float Move ...
- SQLAlchemy的查询操作Query
查询操作 查询子句使用session的.query()方法来获取Query查询对象.查询对象能够使用一些方法来对应一些查询子句,比如.order_by(),.limit(),.filter()等. 查 ...
- Android基础新手教程——3.7 AnsyncTask异步任务
Android基础新手教程--3.7 AnsyncTask异步任务 标签(空格分隔): Android基础新手教程 本节引言: 本节给大家带来的是Android给我们提供的一个轻量级的用于处理异步任务 ...