问题描述:我想要再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:%@",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;}总结
以上是内存溢出为你收集整理的iOS如何实现连接打印机,打印一段字符串全部内容,希望文章能够帮你解决iOS如何实现连接打印机,打印一段字符串所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)