MCBrowserViewController

MCBrowserViewController

我希望多人连接对等选择器视图能够像应用程序中的其他所有内容一样启动并保持在横向。

该plist具有“支持的界面方向”,仅包含“横向(右侧主页按钮)”,但是MCBrowserViewController忽略它。

我试图将其子类化为override supportedInterfaceOrientations,但是根据这个link,它不支持该类。 presentViewController(可用于MCBrowserViewController)提供:

uncaughtExceptionHandler; NSInternalInconsistencyException: Could not load NIB in bundle:
            'NSBundle </var/containers/Bundle/Application/B2B62F00-15A1-4063-89B0-52EB7F3E464C/
            Parsec.app> (loaded)' with name 'MCBrowserViewController'

谢谢你的帮助。

最佳答案

我编写了一个非常基本的示例应用程序,并使用iOS 11.2对其进行了测试。对于我来说,MCBrowserViewController似乎正在遵守应用程序级别的定向限制。

您应该通过提供 application(_:supportedInterfaceOrientationsFor:) 实现来检查您的应用程序委托是否不覆盖plist方向值。或者,如果您具有UIApplication的自定义子类,请确保它未覆盖 supportedInterfaceOrientations(for:)

我在这里包括完整的源代码:

ViewController.swift:

import UIKit
import MultipeerConnectivity

class ViewController: UIViewController {

    var textField: UITextField?
    let id = MCPeerID(displayName: "testpeer")
    lazy var session = MCSession(peer: id)

    override func viewDidLoad() {
        super.viewDidLoad()

        let button = UIButton(frame: CGRect(x: 100, y: 100, width: 0, height: 0))
        button.setTitle("Browse", for: .normal)
        button.backgroundColor = .red
        button.addTarget(self, action: #selector(showBrowser), for: .touchUpInside)
        button.sizeToFit()
        view.addSubview(button)
    }

    @objc func showBrowser() {
        let controller = MCBrowserViewController(serviceType: "test", session: session)
        present(controller, animated: true)
        controller.delegate = self
    }
}

extension ViewController: MCBrowserViewControllerDelegate {

    func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) {
        self.presentedViewController?.dismiss(animated: true)
    }

    func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
        self.presentedViewController?.dismiss(animated: true)
    }
}

AppDelegate.swift:
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        return true
    }
}

信息清单
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
</dict>
</plist>

09-25 21:01