how to move view controllers while keeping a multipeer connection

I have dabbled in multipeer connectivity with a project I am making and what I now want to do is be able to pair my two devices in one view and then move to another view(like a spritekit view) and still be able to send data back and forth without having to reconnect the devices. Is there a way to do that?

Replies

It sounds like you’re doing your networking directly in your view controller, which is a classic mistake we all made when we starting programming with Cocoa [Touch]. A better approach is to do your networking in a dedicated model-level object, and then have your view controller display the contents of that object. You can then shared that object between multiple view controllers, one for setting up the game and one for actually playing the game.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Can you link a resource that talks about this. Thanks!

Can you link a resource that talks about this.

This concept has been discussed many times by many folks, but I don’t have a favourite instance to recommend. Sorry. Perhaps someone else can chime in.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Can you at least send me one?

One place I know it was discussed was WWDC 2010 Session 207 Network Apps for iPhone OS, Part 1, starting around 35:30, because I discussed it there (-: The reason I didn’t link to that initially is that, while the fundamental concepts are still sound, I’m no longer entirely happy with the details. Specifically:

  • The presentation implies that your model objects should be actively, reaching out to the network to do stuff. That’s a mistake IMO. These days I prefer to use passive model objects and then add a model-layer controller that does the actual networking.

    There’s a bunch of reasons for this change of heart but the most critical one is that it makes testing easier. With passive model objects you can trivially apply changes to the model to verify that the controller and views respond correctly.

  • The presentation does not discuss how to pass model objects between your view controllers, which is probably the biggest stumbling block for folks who are just getting started.

ps The bulk of that presentation, including Part 2, is still surprisingly valid, even eight years later. It’s definitely worth a watch IMO (although I’ll obviously biased).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"