当前位置: 代码迷 >> Iphone >> iOS兑现连接打印机,打印一段字符串
  详细解决方案

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

热度:30   发布时间:2016-04-25 06:04:39.0
iOS实现连接打印机,打印一段字符串
本帖最后由 CSDN_Ask 于 2013-01-04 13:56:34 编辑
问题描述:我想要再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];
  相关解决方案