Swift 3 打印功能

1. 加载到webview再打印

2. 直接打印nsdata

func printViaWebView(fileName: String)
{
 let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let filePath = "\(documentsPath)/\(filename)"
        let url = NSURL(fileURLWithPath: filePath)
        let urlRequest = NSURLRequest(url: url as URL)
        
        webView.loadRequest(urlRequest as URLRequest)
        
        
        let pic = UIPrintInteractionController.shared
        let printInfo : UIPrintInfo = UIPrintInfo(dictionary: nil)
        
        printInfo.outputType = UIPrintInfoOutputType.general
        printInfo.jobName = url.absoluteString!
        
        pic.printInfo = printInfo
        pic.printFormatter = webView.viewPrintFormatter()
        pic.showsPageRange = false
        
        pic.present(animated: true, completionHandler: nil)
}

func printViaNSData(fileName: String){
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let filePath = "\(documentsPath)/\(filename)"
        let url = NSURL(fileURLWithPath: filePath)
        let urlRequest = NSURLRequest(url: url as URL)
        
        webView.loadRequest(urlRequest as URLRequest)
        
        
        let pic = UIPrintInteractionController.shared
        let printInfo : UIPrintInfo = UIPrintInfo(dictionary: nil)
        
        printInfo.outputType = UIPrintInfoOutputType.general
        //printInfo.jobName = url.absoluteString!
        //printInfo.orientation = .portrait
        
        pic.printingItem = NSData(contentsOfFile: filePath)
        pic.showsPaperSelectionForLoadedPapers = true;
        pic.showsPageRange = true;
        pic.showsNumberOfCopies = true;
        pic.printInfo = printInfo
        //pic.printFormatter = webView.viewPrintFormatter()
        
        pic.present(animated: true, completionHandler: nil)
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在SwiftUI中实现Game Center登录,你需要进行以下步骤: 1. 导入GameKit框架。在你的SwiftUI视图文件中,添加以下导入语句: ``` import GameKit ``` 2. 在你的SwiftUI视图中创建一个Game Center登录按钮。你可以使用Button视图来实现这个按钮,并在按钮的action中调用authenticateLocalPlayer()方法来启动Game Center登录流程: ``` Button(action: { // Authenticate local player GKLocalPlayer.local.authenticateHandler = { (viewController, error) in if let viewController = viewController { // Show Game Center login view controller UIApplication.shared.windows.first?.rootViewController?.present(viewController, animated: true, completion: nil) } else if let error = error { // Handle error } else if GKLocalPlayer.local.isAuthenticated { // Player is authenticated, handle accordingly } } }) { Text("Login with Game Center") } ``` 在上面的代码中,我们使用GKLocalPlayer.local.authenticateHandler来处理Game Center登录请求。如果玩家尚未登录,该方法将返回一个视图控制器,该视图控制器用于显示Game Center登录界面。如果发生错误,该方法将返回一个错误。如果玩家已经登录,该方法将返回一个已认证的本地玩家对象。 3. 处理Game Center登录结果。在authenticateHandler中,我们可以处理Game Center登录结果,例如更新UI或检查玩家分数等等。 ``` } else if GKLocalPlayer.local.isAuthenticated { // Player is authenticated, handle accordingly print("Player authenticated: \(GKLocalPlayer.local.alias)") } ``` 在上面的代码中,我们仅仅是打印了已认证的玩家名称。你可以根据你的应用程序的需要来处理这个结果。 注意:在使用Game Center之前,你需要在苹果开发者网站上注册一个Game Center应用程序,并将其添加到你的Xcode项目中。你还需要在Xcode项目中设置Game Center功能,例如Leaderboards和Achievements等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值