iOS 13 UIActivityViewController在保存图像后自动显示以前的VC [英] iOS 13 UIActivityViewController automatically present previous VC after image saving

查看:311
本文介绍了iOS 13 UIActivityViewController在保存图像后自动显示以前的VC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现将图像保存到库"功能,然后返回到当前的视图控制器,但是在新的iOS 13上,它又退回到显示当前视图的视图控制器:

I'm trying to implement "Save image to Library" function and then return back to the current view controller, but on a new iOS 13 it dismisses back to the view controller that presented the current one:

PHPhotoLibrary.requestAuthorization({(_ status: PHAuthorizationStatus) -> Void in })

let shareItems: Array = [newImg,"Hello"] as [Any]

let activityController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)

if UIDevice.current.userInterfaceIdiom == .pad {
    activityController.popoverPresentationController?.sourceView = saveButton
}

present(activityController, animated: true)

推荐答案

我可以确认此错误仍在iOS 13.3.1中存在.以下解决方法是 franze解决方案的Swift版本.我更喜欢这种方法,因为它不会在视图控制器层次结构上做任何进一步的假设,并且不会使用方法混乱.

I can confirm that this bug is still present in iOS 13.3.1. The following workaround is a Swift version of franze's solution. I prefer this approach, as it doesn't make any further assumptions on the view controller hierarchy and doesn't use method swizzling.

使用此附加的UIWindow会中断iOS 12及更早版本上UIActivityViewController Cancel 按钮,因此我添加了对OS版本的检查.

Using this additional UIWindow breaks the Cancel button of the UIActivityViewController on iOS 12 and earlier, so I added a check for the OS version.

private let activityWindow: UIWindow = {
  let window = UIWindow(frame: UIScreen.main.bounds)
  window.rootViewController = UIViewController()
  return window
}()

func showActivityController() {
  let activityViewController = UIActivityViewController(/* ... */)
  activityViewController.completionWithItemsHandler = {
     // ...
     UIApplication.shared.delegate?.window??.makeKeyAndVisible()       
  }

  // Use this workaround only on iOS 13
  if ProcessInfo.processInfo.operatingSystemVersion.majorVersion == 13 {
    activityWindow.makeKeyAndVisible()
    activityWindow.rootViewController?.present(activityViewController, animated: true)
  } else {
    present(activityViewController, animated: true)
  }
}

更新:显然,此解决方案无法在iPad上可靠地运行.在iPad上,UIActivityViewController的显示方式似乎有所不同,并且只要它在屏幕上可见,就不会记录任何触摸事件,从而有效地冻结了该应用程序.

Update: Apparently, this solution doesn't work reliably on iPads. It looks like on iPad the UIActivityViewController is presented differently and as soon as it's visible on screen, no touch events are registered, effectively freezing the app.

这篇关于iOS 13 UIActivityViewController在保存图像后自动显示以前的VC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆