1

I want the multiplayer connectivity peer picker view to launch & stay in landscape like everything else in the app.

The plist has "Supported interface orientations" containing just "landscape (right home button)", but the MCBrowserViewController ignores it.

I tried to subclass to override supportedInterfaceOrientations, but according to this link, it doesn't support this. presentViewController, which works on MCBrowserViewController, gives:

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

Thanks for your help.

Thom A
  • 88,727
  • 11
  • 45
  • 75
DenverCoder9
  • 3,635
  • 3
  • 31
  • 57
  • 1
    I am unable to reproduce this behavior in the iOS 11.2 simulator. I have the `MCBrowserViewController` locked to landscape right. – allenh Feb 07 '18 at 23:50
  • 1
    [My test application](https://github.com/allenhumphreys/MCBrowserViewControllerSOTest) – allenh Feb 08 '18 at 00:00
  • @AllenHumphreys thanks, this will hopefully be helpful. If you write it into an answer in the next few hours I'll award you the bounty (assuming no-one gives the actual solution). – DenverCoder9 Feb 08 '18 at 21:49

1 Answers1

1

I've written a very basic sample application and tested it using iOS 11.2. It seems that, for me, MCBrowserViewController is obeying the app level orientation restrictions.

You should check that your app delegate is not overriding the plist orientation values by providing an implementation of application(_:supportedInterfaceOrientationsFor:). Or, if you have a custom subclass of UIApplication, make sure it is not overriding supportedInterfaceOrientations(for:).

I'm including the full source here:

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
    }
}

Info.plist

<?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>
allenh
  • 6,582
  • 2
  • 24
  • 40
  • Your extra check was spot on. I had a supportedInterfaceOrientationsForWindow method in the app delegate returning UIInterfaceOrientationMaskAllButUpsideDown. It was needed to fix a Game Center crash bug back in iOS6 (http://stackoverflow.com/questions/12488838), but I had every other view controller returning UIInterfaceOrientationMaskLandscapeRight so I didn't notice it. – DenverCoder9 Feb 12 '18 at 15:54