从同一网络但不同子网中的 iOS 应用程序访问 wifi 打印机

Access wifi printer from iOS app in same network but different subnet

在我的项目中有一个打印选项,我们使用以下代码打印一个简单的 pdf 文件:

UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];

if  (pic && [UIPrintInteractionController canPrintData: self.myPDFData] ) {
    pic.delegate = self;
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = @"PrintPdf";
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    pic.printInfo = printInfo;
    pic.showsPageRange = YES;
    pic.printingItem = self.myPDFData;     
    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =

    ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
        if (!completed && error)
            NSLog(@"FAILED! due to error in domain %@ with error code %ld",
                  error.domain, (long)error.code);
    };
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [pic presentFromRect:self.printButton.frame inView:self.view animated:YES completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {
        }];
    } else {
        [pic presentAnimated:YES completionHandler:completionHandler];
    }
}

当我在打印机模拟器中测试时,它工作正常。我的要求是打印机可能位于同一 wifi 的另一个子网上。我该怎么做?

无需更改代码 UIPrintInteractionController 中的任何内容,如果打印机将在同一网络中,则 UIPrintInteractionController 可以发现它,并且您可以从弹出窗口中 select打印机。