2016-05-11 48 views
1

我需要在後臺無需用戶交互的Xcode中的AirPrint。我不在乎是否必須使用第三方框架來執行此操作。這是我有的代碼,但需要用戶的交互。自動AirPrint IOS

NSMutableString *printBody = [NSMutableString stringWithFormat:@"%@",TextField.text]; 

    UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController]; 
    pic.delegate = self; 


    UIPrintInfo *printInfo = [UIPrintInfo printInfo]; 
    printInfo.outputType = UIPrintInfoOutputGeneral; 
    printInfo.jobName = @"PrintJob"; 
    pic.printInfo = printInfo; 


    UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody]; 
    textFormatter.startPage = 0; 
    textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); 
    textFormatter.maximumContentWidth = 6 * 72.0; 
    pic.printFormatter = textFormatter; 
    pic.showsPageRange = YES; 


void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) { 
    if (!completed && error) { 
     NSLog(@"Printing could not complete because of error: %@", error); 
    } 
}; 


    [pic presentAnimated:YES completionHandler:completionHandler]; 

如果有人能幫上忙,那就太好了!

+1

想要自動打印而不進行任何操作? – iOS

+1

你看了嗎?:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPrintInteractionController_Class/index.html#//apple_ref/occ/instm/UIPrintInteractionController/printToPrinter:completionHandler: – tptcat

+0

是@ DarjiJigar這就是我想要做的 – Connor

回答

0

I found a video that helped a lot!這是iOS 8的wddc視頻,他們只是在不使用UIPrintInteractionController的情況下引入了新的打印方式。這裏是。

-(IBAction)print:(id)sender{ 

     NSMutableString *printBody = [NSMutableString stringWithFormat:@"Hey this is a test lets hope it works!"]; 

     UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController]; 
     pic.delegate = self; 


     UIPrintInfo *printInfo = [UIPrintInfo printInfo]; 
     printInfo.outputType = UIPrintInfoOutputGeneral; 
     printInfo.jobName = @"PrintJob"; 
     pic.printInfo = printInfo; 


     UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody]; 
     textFormatter.startPage = 0; 
     textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); 
     textFormatter.maximumContentWidth = 6 * 72.0; 
     pic.printFormatter = textFormatter; 
     pic.showsPageRange = YES; 
//save your printer using code also in video. 

     UIPrinter *SavedPrinterUserDefaults = [[NSUserDefaults standardUserDefaults] 
              objectForKey:@"SavedPrinter"]; 
     [pic printToPrinter:SavedPrinterUserDefaults 
     completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) { 
      if (completed && !error) 
      NSLog(@"YAYA! it printed"); 
     }]; 

    } 

我希望這可以幫助任何人有同樣的問題。 Here is more to read about what you can do with this new API