GLBasic forum

Feature request => Network => Topic started by: Neurox on 2011-Jul-21

Title: Airprint support - Possible ?
Post by: Neurox on 2011-Jul-21
AirPrint (or/and ePrint) support would be really useful with iPhone/iPad in GLBasic developping.

Gernot ?  :whistle:
Title: Re: Airprint support - Possible ?
Post by: Moebius on 2011-Jul-21
With a *!*GOOGLE SEARCH*!* code comes up for printing an image.  I can't test it, but the attached file compiles fine.  Because of attachment restrictions, you should rename it to a ".m"...
Not sure if you wanted something more than images, but considering the GLBasic is a graphics program, it's probably more suited to printing images that you render.
Possible GLB code, again not tested because I'd need to upgrade the device and sort out Airprint...
Code (glbasic) Select
IMPORT "C" int __stdcall GLBAirPrint(const char* imagePath)

FUNCTION PrintImage: Image%, Filename$ = "Print.png"

LOCAL Documents$ = PLATFORMINFO$("Documents")
SAVESPRITE Documents$ + "\\" + Filename$, Image
GLBAirPrint(Documents$ + "\\" + Filename$)

ENDFUNCTION


[attachment deleted by admin]
Title: Re: Airprint support - Possible ?
Post by: Neurox on 2011-Jul-25
Thanks for source code but...
I realized that the AirPrint doesn't work with ipod touch second generation: (
I'll wait to try the new iPod coming next week.

Title: Re: Airprint support - Possible ?
Post by: Neurox on 2011-Jul-30
Hi,
after 3 days full of tests the result is negative  :giveup:
I've tested the function with iPhone 4 with iOS 4.3.5 and iPod fourth gen
but the printer not receive anything and not print anything :'(
Title: Re: Airprint support - Possible ?
Post by: Moebius on 2011-Jul-31
The original code was an objective C class, and I have no doubt I messed something up when deleting stuff from it  :whistle:
Unfortunately I can't help here...
Title: Re: Airprint support - Possible ?
Post by: trucidare on 2011-Jul-31
Its working from other applications? if not you need a bonjour compatible router
Title: Re: Airprint support - Possible ?
Post by: Neurox on 2011-Jul-31
I'm using a HP B110 printer, AirPrint compatible.
Title: Re: Airprint support - Possible ?
Post by: trucidare on 2011-Jul-31
try it with another application first then with glbasic and tell me the difference
Title: Re: Airprint support - Possible ?
Post by: Neurox on 2011-Jul-31
I've made the test.
First of all I printed a html page from safari and then I printed from my program.
Unfortunately, the printer prints only the html page.  :'(
After printing my app didn't blocked but accept no more commands, as if MOUSESTATE function doesn't return any value.   :'(
Title: Re: Airprint support - Possible ?
Post by: trucidare on 2011-Jul-31
Add a single NSLog(@"Error!"); line if the image is not present and the canPrint else so we could see if the app isnt ready to print
Title: Re: Airprint support - Possible ?
Post by: Neurox on 2011-Jul-31
I put in all the points the NSLog(), I discovered that the print is sent to the printer without problems (even if it doesn't print anything) and exit the function but it seems that GLBasic isn't able to control of the screen.
Title: Re: Airprint support - Possible ?
Post by: Moebius on 2011-Aug-01
I deleted anything pertaining to 'self' in the class, including the delegate, and if I understand correctly the action should never be recognised as 'complete'.  The code is screwed up - find a better example on the internet.
Title: Re: Airprint support - Possible ?
Post by: trucidare on 2011-Aug-01
the print controller is a shared thing and could be used outside from classes. but apple uses delegates in all shared things so i need the full code to make it workable
Title: Re: Airprint support - Possible ?
Post by: Moebius on 2011-Aug-01
Apple page:  http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/Printing/Printing.html (http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/Printing/Printing.html)
print image code taken from:  http://www.iphonedevsdk.com/forum/iphone-sdk-development/67176-airprint-tutorial-simple-print-file-30-lines-code.html (http://www.iphonedevsdk.com/forum/iphone-sdk-development/67176-airprint-tutorial-simple-print-file-30-lines-code.html)
Good Luck!  (I don't even understand delegates  :giveup:)
Title: Re: Airprint support - Possible ?
Post by: trucidare on 2011-Aug-01
very untested and not checked for syntax - please try someone - put them into .mm file

Code (glbasic) Select
//
//  AirPrintGLB.mm
//  AirPrintGLB
//
//  Created by Hiro Kanakawa on 01.08.11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface AirPrintGLB : NSObject <UIPrintInteractionControllerDelegate>

@end


@implementation AirPrintGLB

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
   
    return self;
}

-(int) GLBAirPrint:(const char *) imagePath
{
NSString *path = [[NSString alloc ]initWithCString: imagePath];
NSData *myData = [NSData dataWithContentsOfFile: path];


UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
    if (![UIPrintInteractionController isPrintingAvailable])
    {
        NSLog(@"Sorry printing not supported!");
        return -1;
    }

if(pic && [UIPrintInteractionController canPrintData: myData] ) {
        pic.delegate = self;

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pic.printInfo = printInfo;
pic.showsPageRange = YES;
pic.printingItem = myData;

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
if (!completed && error) {
                NSLog(@"Error accoured!");
} else {
                NSLog(@"Tadaaaa");
            }
};

[pic presentAnimated:YES completionHandler:completionHandler];

} else {
return 0;
}
return 1;

}

@end

extern "C" int GLBAirPrint(const char* file);

extern "C" int GLBAirPrint(const char* file) {
    AirPrintGLB *printInfo = [[AirPrintGLB alloc]init];
    return [printInfo GLBAirPrint:file];
}


Tested in simulator:

Code (glbasic) Select
[01/Aug/2011:21:51:33 +0200] Save Original to Simulator: Accepted "test.gif" for printing (job #143896126, application/pdf, 1 pages){
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:33 +0200] save-143896126: job-state-changed - Job pending.{
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:33 +0200] Save Original to Simulator: Printing "test.gif" (job #143896126){
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:33 +0200] Viewing "/var/folders/pg/t13mbqm1063d_8wggb6xjpch0000gq/T/PrinterSimulator.RSqXpC/143896126.pdf".{
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:33 +0200] "POST /printers/save HTTP/1.1" 200 4093918 - successful-ok{
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:33 +0200] save-143896126: job-state-changed - Job printing.{
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:33 +0200] Save Original to Simulator: Connecting to printer.{
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:33 +0200] "POST /printers/save HTTP/1.1" 200 382 Get-Job-Attributes successful-ok{
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:33 +0200] Save Original to Simulator: Undefined error: 0{
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:33 +0200] save-143896126: job-completed - Job completed.{
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:34 +0200] Accepted connection from 192.168.125.1:49418 (IPv4){
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:34 +0200] "POST /printers/save HTTP/1.1" 200 295 Get-Printer-Attributes successful-ok{
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:34 +0200] Closing connection from 192.168.125.1:49418 (IPv4){
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:34 +0200] "POST /printers/save HTTP/1.1" 200 382 Get-Job-Attributes successful-ok{
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
[01/Aug/2011:21:51:34 +0200] Closing connection from 192.168.125.1:49416 (IPv4){
    NSFont = "\"Monaco 10.00 pt. P [] (0x10016af40) fobj=0x10030cae0, spc=6.00\"";
}
Title: Re: Airprint support - Possible ?
Post by: Neurox on 2011-Aug-02
Hi Trucidare,
I've tested your code but I received this error "expected identifier or '(' before string constant" for Extern "C" declarations.
My XCode version is 3.2.5
Title: Re: Airprint support - Possible ?
Post by: trucidare on 2011-Aug-02
you need to put the code in a .mm file because its objc++ sorry i forget this
Title: Re: Airprint support - Possible ?
Post by: Neurox on 2011-Aug-02
Now compile, but nothing happens.
I added the NSLog into various points of code to see what happens :
Code (glbasic) Select

//
//  Airprint.mm
// 
//
//  Created by James on 21/07/11.
#import <UIKit/UIKit.h>

@interface AirPrintGLB : NSObject <UIPrintInteractionControllerDelegate>
@end

@implementation AirPrintGLB

- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
NSLog(@"Init code");
}
return self;
}


-(int) GLBAirPrint:(const char *) imagePath
{
   NSLog(@"Inside function");
NSString *path = [[NSString alloc ]initWithCString: imagePath];
NSData *myData = [NSData dataWithContentsOfFile: path];
   NSLog(@"Img: %@ ",path);

UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
if (![UIPrintInteractionController isPrintingAvailable])
{
NSLog(@"Sorry printing not supported!");
return -1;
}

if(pic && [UIPrintInteractionController canPrintData: myData] ) {
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pic.printInfo = printInfo;
pic.showsPageRange = YES;
pic.printingItem = myData;

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(@"Error accoured!");
} else {
NSLog(@"Tadaaaa");
}
};
[pic presentAnimated:YES completionHandler:completionHandler];

} else {
NSLog(@"Error with myData");
   return 0;
}
   NSLog(@"Out from function");
return 1;
}
@end

extern "C" int GLBAirPrint(const char* file);
extern "C" int GLBAirPrint(const char* file) {
AirPrintGLB *printInfo = [[AirPrintGLB alloc]init];
    return [printInfo GLBAirPrint:file];
}


This is result from debugger console :
Code (glbasic) Select

set cdir to: /var/mobile/Applications/3849DEC0-1EFA-4732-9BFE-710E0786C2B2/AirTest.app
exepath=curdir= /var/mobile/Applications/3849DEC0-1EFA-4732-9BFE-710E0786C2B2/AirTest.app
AudioStreamBasicDescription:  2 ch,  44100 Hz, 'lpcm' (0x00000C2C) 8.24-bit little-endian signed integer, deinterleaved
Init Finalized
set cdir to: Media
2011-08-02 08:41:58.479 AirTest[773:6503] Init code
2011-08-02 08:41:58.483 AirTest[773:6503] Inside function
2011-08-02 08:41:58.490 AirTest[773:6503] Img: /var/mobile/Applications/3849DEC0-1EFA-4732-9BFE-710E0786C2B2/Documents/pollo.png
[Switching to thread 13315]
2011-08-02 08:41:58.653 AirTest[773:6503] Out from function

Title: Re: Airprint support - Possible ?
Post by: trucidare on 2011-Aug-02
cant say - on simulator it works fine, i tested with a gif file. i think i enable airprint via hack and test on real device
Title: Re: Airprint support - Possible ?
Post by: Neurox on 2011-Sep-25
I'm crazy out.
I've made test and test and test
but the app don't print to my HP printer and not appear the window of configuration airprint  :rant: