Coder Social home page Coder Social logo

p2psharekit's Introduction

P2PShareKit

Swift Version Platforms

Peer-to-peer sharing using the modern Network.framework, all wrapped up in a handy Swift package.

Securely share data between iOS devices using peer wifi networking (no wifi routers or any established network required)

  • Automatically connect to trusted peers
  • Comms via TLS 1.2
  • Simple Data sharing
  • Automatically re-establish connections to lost peers

Builds upon sample code provided during WWDC2019 - Advances in Networking Part 2

This implementation is currently limited to:

  • iOS 13 or later
  • exclusively uses peer-to-peer wifi

Demo

Refer to the Example project

Sample

Usage

Make sure you have added NSLocalNetworkUsageDescription and NSBonjourServices to your Info.plist

Each device needs to identify itself to others. Build your own identify dictionary based on what makes sense for your app. The PeerInfo record is exchanged before the framework reports a new connection.

let peerInfo = PeerInfo(info: ["name": "Fred"])

Configure a session with your security credentials. Connections are only made with other devices with the same security credentials.

let config = MultipeerSessionConfig(myPeerInfo: peerInfo, 
                                    bonjourService: "_demo._tcp", 
                                    presharedKey: "1234", 
                                    identity: "Demo")

let session = MultipeerSession(config: config)

Finally, set the session callbacks and start the session.

session.peersChangeHandler = { peerInfos in 
     // update any UI that shows connected peers
 }

session.newPeerHandler = { peerInfo in 
    // a good time to sync anything necessary 
}

session.messageReceivedHandler = { peerInfo, data in 
    // Decode data and process
}

session.startSharing()

To send a message, use the peerID from the list of peers returned in peersChangeHandler.

let peerID = peerInfo.peerID
session.send(to: peerID, data: data)

The framework isn't opinionated about how you handle the generic data payload. You can progressively try to decode against expected Codable structs, or use a Codable enum.

Refer to the following blog post for more information and the example PeerShareData that uses a Codable enum: Adopting Network.framework for iOS peer-to-peer connectivity

Integrating

Add the depedency in your Swift package file:

    dependencies: [
        .package(url: "https://github.com/dobster/P2PShareKit", from: "0.2.0")
    ],

Contributing

Feel free to open issues on GitHub or to open pull requests.

License

This project is licensed unter the terms of the MIT license. See LICENSE for more information.

p2psharekit's People

Contributors

dobster avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

p2psharekit's Issues

Using P2PShareKit with SwiftUI

Hi there!
I am currently teaching myself SwiftUI through making a simple project. I found your website through the article you wrote about peer-to-peer networking on iOS and this library P2PShareKit. As a beginner learning SwiftUI I am having trouble trying to implement P2PShareKit in a simple SwiftUI project. I wonder if you have a simple example of how your example project would work in a swiftUI scenario? Or if you could help me to make one?

Background mode issue

Device1 put in the background mode and then device 2 in foreground mode
Sending data from device 2 and when device 2 come online ( foreground ) not showing data - is it lost or Network framework doesnt have this capability ?

Error running example project

as follows:

2020-12-10 10:37:17.307649+0800 p2pShareKitDemo[18518:5790389] [listener] Ready
2020-12-10 10:37:17.309910+0800 p2pShareKitDemo[18518:5790389] [listener] Error: -65555: NoAuth
2020-12-10 10:37:17.310002+0800 p2pShareKitDemo[18518:5790389] [listener] restarting
2020-12-10 10:37:17.310382+0800 p2pShareKitDemo[18518:5790389] [listener] Cancelled
2020-12-10 10:37:17.310543+0800 p2pShareKitDemo[18518:5790389] [listener] Waiting: POSIXErrorCode: Network is down

App may crash on return to foreground if browser not cancelled

Apps using the Network framework to do peer-to-peer over wifi will need to call stopSharing() on the session object when going into background and call startSharing() on return to foreground. This is due to a bug in the Network framework NWBrowser object. You will only see this crash when you are running your app installed on a device without the Xcode debugger connected. This bug has been reported to Apple, but we're up to iOS 13.5 and there seems no fix out for it yet. Their sample TicTacToe project associated with WWDC 2019 Advanced Networking Part 2 video will also crash due to this bug.

Task: Update the example project to stop browsing while the app is backgrounded.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.