iOS实现连接打印机,打印一段字符串

问答小助手 2013-01-04 01:55:57
加精
问题描述:我想要再ipad程序里面,用代码给打印机发送一段字符串,然后打印。

具体情况是这样的,现在想要用wifi连接到打印机,然后给打印机发送一个字符串去打印。但是问题是不知道如何连接打印机,也不知道如何给打印机发送消息。其中打印机和ipad在同一个局域网下,想用socket通过打印机的ip地址进行传送要打印的内容。

更多解决方法见:http://ask.csdn.net/questions/910

.h 里面
#import <UIKit/UIKit.h>
#import "AsyncSocket.h"

@interface ViewController : UIViewController<AsyncSocketDelegate>
{
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;
}

...全文
5041 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lois_soul 2014-08-23
  • 打赏
  • 举报
回复
出现“unable to encrypt connection from 192.168.1.101 - ssl protocol error (-9800)”的错误,楼主没有出现吗!
lvjsky 2014-08-20
  • 打赏
  • 举报
回复
为啥我这么写的无法打印呢 在 didConnectToHost里执行[sock readDataWithTimeout:-1 tag:0];的时候会连接失败执行willDisconnectWithError 这个函数 。求解啊
wen6691853 2014-06-19
  • 打赏
  • 举报
回复
爱技术的你 2014-04-10
  • 打赏
  • 举报
回复
出现“unable to encrypt connection from 192.168.1.101 - ssl protocol error (-9800)”的错误,楼主没有出现吗?
riseliang 2013-11-05
  • 打赏
  • 举报
回复
赞一个,哈哈哈哈
kikitl 2013-10-09
  • 打赏
  • 举报
回复
我也遇到楼主的问题。。我下了socket这个代码,看不懂。。。对 AirPrint不懂,对打印也不懂。。。怎么办嘛。急着做
znzxyz 2013-01-09
  • 打赏
  • 举报
回复
有用的,支持下
sqk2050 2013-01-09
  • 打赏
  • 举报
回复
哇色 民间高手真是不少呀!
wcsoswcsos 2013-01-08
  • 打赏
  • 举报
回复
这么厉害?学习到了
yangqiangwillbe 2013-01-07
  • 打赏
  • 举报
回复
哎,积分啊啊啊啊
DAWNMJ 2013-01-06
  • 打赏
  • 举报
回复
哎,积分啊啊啊啊
asd5858 2013-01-06
  • 打赏
  • 举报
回复
id6000 2013-01-05
  • 打赏
  • 举报
回复
有用的,支持下
问答小助手 2013-01-05
  • 打赏
  • 举报
回复
引用 12 楼 zushangban123 的回复:
这个不懂怎么办
学嘛
zushangban123 2013-01-04
  • 打赏
  • 举报
回复
这个不懂怎么办
taufik13888 2013-01-04
  • 打赏
  • 举报
回复
感觉不错,先看看
lingling9978 2013-01-04
  • 打赏
  • 举报
回复
nianhua321 2013-01-04
  • 打赏
  • 举报
回复
abc_xx 2013-01-04
  • 打赏
  • 举报
回复
有空了去看看
ahaujdd 2013-01-04
  • 打赏
  • 举报
回复
太有才了吧!!!!
加载更多回复(3)

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧