gpt4 book ai didi

IOS - 从非 UI 类启动视频弹出窗口

转载 作者:行者123 更新时间:2023-11-29 11:57:32 25 4
gpt4 key购买 nike

我正在开发一个将由某些客户端 UI 调用的 SDK。 SDK 包含执行不同任务的各种模块。其中一个模块在收到 url 后,必须打开视频弹出窗口或等同于播放视频。模块本身是一个没有 UI 的普通类。

虽然我能够从类中启动 UIAlertController 弹出窗口,但当我尝试启动 UIViewController 来播放视频时,我听到了音频但没有看到视频。什么是建议的方式

  1. 从非 UI 类启动 View Controller
  2. 将视频显示为带有控件的弹出窗口

谢谢

最佳答案

如果您的 SDK 需要兼容应用扩展,那么 UIApplication 将无法供您使用。

要解决此问题,同时也为主机应用程序提供更大的控制权,您可以实现一个代表 SDK 的 View Controller 。

宿主应用程序可以实现委托(delegate)并选择最合适的 Controller ,包括使用 Mark 推荐的 Root View Controller 。

首先声明一个协议(protocol):

public protocol MySDKPopupDelegate: NSObjectProtocol {
func presentPopupViewController(viewController: UIViewController)
}

定义一种在 SDK 中设置委托(delegate)的方法:

public class MySDKConfig {

public let sharedConfig = MySDKConfig

weak var delegate: MySDKPopupDelegate?
}

当您的 SDK 需要呈现视频时,向委托(delegate)请求 View Controller :

func showPopupForVideo(url: NSURL) {
let viewController = viewControllerForVideo(url)
delegate?.presentPopupViewController(videoViewController)
}

最后,宿主应用程序应该实现委托(delegate)方法来实际呈现 View Controller ,例如,在 AppDelegate 中:

class AppDelegate: NSObject, MySDKPopupDelegate {

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

MySDKConfig.sharedConfig.delegate = self
}


func presentPopupViewController(viewController: UIViewController) {

guard let presentingViewController = window?.rootViewController else {
return
}

presentingViewController.presentViewController(viewController, animated: true, completion: nil)
}
}

委托(delegate)也可以在特定的 View Controller 中实现,包括在应用程序扩展中:

class ShareViewController: UIViewController, MySDKPopupDelegate {

override public func viewDidLoad() {
MySDKConfig.sharedConfig.delegate = self
}

func presentPopupViewController(viewController: UIViewController) {
presentViewController(viewController, animated: true, completion: nil)
}
}

关于IOS - 从非 UI 类启动视频弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38378894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com