📱📲 A wrapper for the MultipeerConnectivity framework for automatic offline data transmission between devices

Last update: Jun 10, 2022

Build Status CocoaPods Version Status Carthage compatible doccov iOS MacOS tvOS Swift MIT License

A wrapper for Apple's MultipeerConnectivity framework for offline data transmission between Apple devices. This framework makes it easy to automatically connect to multiple nearby devices and share information using either bluetooth or wifi radios.

  1. Features
  2. Integration
  3. Usage
  4. Example
  5. License
  6. Authors

Features

  • Supports iOS/macOS/tvOS
  • Auto Connection
  • Auto Invitations/Advertising
  • Send/Receive data via MultipeerConnectivity Framework
  • Specify data types for easy handling

Integration

CocoaPods

You can use CocoaPods to install MultiPeer by adding it to your Podfile:

pod 'MultiPeer'

Carthage

You can use Carthage to install MultiPeer by adding it to your Cartfile:

github "dingwilson/MultiPeer"

Swift Package Manager

For SPM, add the following to your package dependencies:

.package(url: "https://github.com/dingwilson/MultiPeer.git", .upToNextMinor(from: "0.0.0"))

Usage

To get started, import MultiPeer.

import MultiPeer

Then, simply initialize MultiPeer with the name of your session (serviceType). There are two modes of connections (advertiser and browser). To utilize both, simply use .autoConnect().

MultiPeer.instance.initialize(serviceType: "demo-app")
MultiPeer.instance.autoConnect()

Any data transmitted by MultiPeer will always be accompanied by a numerical "type", to ensure other peers know what kind of data is being received, and how to properly process it. You can manage this by creating a UInt32 enum, as shown below:

enum DataType: UInt32 {
  case string = 1
  case image = 2
  // ...
}

To send data, simply use the .send(object: type:) function:

MultiPeer.instance.send(object: "Hello World!", type: DataType.string.rawValue)

To receive data, we must conform to the MultiPeerDelegate protocol:

func multiPeer(didReceiveData data: Data, ofType type: UInt32, from peerID: MCPeerID) {
  switch type {
    case DataType.string.rawValue:
      let string = data.convert() as! String
      // do something with the received string
      break
      		
    case DataType.image.rawValue:
      let image = UIImage(data: data)
      // do something with the received UIImage
      break
      		
    default:
      break
  }
}

func multiPeer(connectedDevicesChanged devices: [String]) {
}

Ensure that you set the MultiPeer delegate.

MultiPeer.instance.delegate = self

Finally you'll need to enable incoming / outgoing connections in your entitlements.

Congratulations! You have successfully sent data using MultiPeer! For more detailed information (including details of other functions), please see the docs.

Example

For an example app using MultiPeer, checkout MultiPeer_Sample.

License

MultiPeer is released under an MIT License. See LICENSE for details.

Authors

Project heavily inspired by Apple-Signal.

GitHub

https://github.com/dingwilson/MultiPeer
Comments
  • 1. Issues with sending images

    Hi,

    Not sure if this is an issue or just a problem with my syntax, but when sending images, nothing is received. I can send strings no problem.

    Starting connection:

    MultiPeer.instance.delegate = self
            
            MultiPeer.instance.initialize(serviceType: "livestream")
            MultiPeer.instance.autoConnect()
    

    Sending:

    MultiPeer.instance.send(object: imageView, type: DataType.image.rawValue)

    Receiving:

    extension loadViewController: MultiPeerDelegate {
    
        func multiPeer(didReceiveData data: Data, ofType type: UInt32) {
            switch type {
            case DataType.image.rawValue:
                guard let image = data.convert() as? UIImage else { return }
                print("___________________________image recieved!___________________________")
                break;
                
            default:
                break;
            }
        }
        func multiPeer(connectedDevicesChanged devices: [String]) {
        }
    }
    

    Any help would be appreciated!

    Many Thanks, Ben

    Reviewed by benmckillop at 2018-09-07 06:04
  • 2. Added source peer on MultipeerDelegate didReceveData method

    This PR exposes source peer when data is received. It's useful when you want to forward a message to other peers, excluding source peer to avoid loops/duplication

    Reviewed by thibauddavid at 2020-09-01 14:36
  • 3. Added danger support

    Features:

    • Notifies user if xcodebuild contains warnings/errors
    • Notifies user if PR contains commit with merge message (asks them to rebase)
    • Notifies user if [WIP] found in PR head
    • Notifies user for SwiftLint issues
    • [More to Come]
    Reviewed by dingwilson at 2018-02-19 05:46
  • 4. Warnings of depreciated methods in MultiPeer.swift file

    Getting these alert warnings when I compile, thought they might want to be addressed/updated:

    Line 159: 'archivedData(withRootObject:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: instead

    line 174: 'archivedData(withRootObject:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: instead

    line 297: 'unarchiveObject(with:)' was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: instead

    line 302: 'archivedData(withRootObject:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: instead

    Reviewed by thoughtcastapp at 2020-08-05 05:13
  • 5. Sending data to a peer that is not nearby

    Hi @dingwilson,

    I like your wrapper for MultiPeer and I was thinking what if we store a particular peerID when it is nearby (for instance in userDefaults) and when they are not nearby, try sending a message to that peerID.

    For example peer A wants to send a msg to Peer C but they are not nearby but B is nearby to A and also nearby to C.

    A-----B-------C

    Reviewed by iHamzaaK at 2020-01-15 01:45
  • 6. Invite peer manually

    Add shouldInvitePeerDidFound delegate and expose invite peer method to make developer invite peer manually. If there is no delegate, then auto invite peer and reconnect issue is possible to be raised.

    Reviewed by kf99916 at 2019-11-06 06:14
A peer to peer framework for OS X, iOS and watchOS 2 that presents a similar interface to the MultipeerConnectivity framework

This repository is a peer to peer framework for OS X, iOS and watchOS 2 that presents a similar interface to the MultipeerConnectivity framework (which is iOS only) that lets you connect any 2 devices from any platform. This framework works with peer to peer networks like bluetooth and ad hoc wifi networks when available it also falls back onto using a wifi router when necessary. It is built on top of CFNetwork and NSNetService. It uses the newest Swift 2's features like error handling and protocol extensions.

Jun 18, 2022
Apple watch app to interface with Transmission Client
Apple watch app to interface with Transmission Client

TransmissionWatch Apple watch app to interface with Transmission Client Currrent

Dec 23, 2021
Mar 3, 2022
Bonjour networking for discovery and connection between iOS, macOS and tvOS devices.
Bonjour networking for discovery and connection between iOS, macOS and tvOS devices.

Merhaba Bonjour networking for discovery and connection between iOS, macOS and tvOS devices. Features Creating Service Start & Stop Service Stop Brows

Jun 20, 2022
Kiwix for offline access on iOS and macOS

Kiwix for iOS & macOS This is the home for Kiwix apps on iOS and macOS. Mobile app for iPads & iPhones Download the iOS mobile app on iTunes App Store

Jun 16, 2022
A barebones Swift HTTP client with automatic JSON response parsing.

HTTP Client A barebones Swift HTTP client with automatic JSON response parsing. Installation Add HTTP Client as a dependency through Xcode or directly

Feb 3, 2022
Fully automatic managed circular image array above UIKit.
Fully automatic managed circular image array above UIKit.

Automatic Profile cluster view Fully automatic managed circular image array. Usage From Interface Builder • Drag ProfileClusterView.swift file into yo

Sep 2, 2021
NWReachability - a pure Swift library for monitoring the network connection of iOS devices using Apple's Network framework.

NWReachability is a pure Swift library for monitoring the network connection of iOS devices using Apple's Network framework.

Mar 4, 2022
A remote for your IR devices for iOS and Mac!
A remote for your IR devices for iOS and Mac!

Command your TV, Apple TV or Receiver with your Mac/iOS device through iTach. Screenshots On iOS: On Mac (notification center): How to use Buy a iTach

Feb 10, 2022
Swift Paging is a framework that helps you load and display pages of data from a larger dataset from local storage or over network.

Swift Paging is a framework that helps you load and display pages of data from a larger dataset from local storage or over network. This approach allows your app to use both network bandwidth and system resources more efficiently. It's built on top of Combine, allowing you to harness its full power, handle errors easily, etc.

Feb 7, 2022
Open source SDK to quickly integrate subscriptions, stop worring about code maintenance, and getting advanced real-time data. Javascript / iOS glue framework
Open source SDK to quickly integrate subscriptions, stop worring about code maintenance, and getting advanced real-time data.  Javascript / iOS glue framework

Open source SDK to quickly integrate subscriptions, stop worring about code maintenance, and getting advanced real-time data. Javascript / iOS glue framework

Apr 29, 2022
Thin wrapper around NSURLSession in swift. Simplifies HTTP requests.

SwiftHTTP SwiftHTTP is a thin wrapper around NSURLSession in Swift to simplify HTTP requests. Features Convenient Closure APIs Simple Queue Support Pa

Jun 8, 2022
Http Request wrapper written in Swift

Net Net is a HttpRequest wrapper written in Swift Features GET, POST, PUT, DELETE method Powerful request params: nested params, number, string, dic,

Apr 14, 2022
Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application.
Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application.

Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application. Install Carthage

Jan 21, 2022
A WIP Swift wrapper for Discord's Bot API.

Quickcord Quickcord is a WIP Swift wrapper for Discord's Bot API. Features (As of right now) Connecting the the gateway and identifying. Literally eve

Aug 7, 2021
Easy to use CFNetwork wrapper for HTTP requests, Objective-C, Mac OS X and iPhone

ASIHTTPRequest is an easy to use wrapper around the CFNetwork API that makes some of the more tedious aspects of communicating with web servers easier

Jun 17, 2022
A custom wrapper over AFNetworking library that we use inside RC extensively

AFNetworkingHelper A very simple wrapper over the most amazing networking library for objective C, AFNetworking. We extensively use it inside RC and i

Aug 3, 2021
Easy HTTP Networking in Swift a NSURLSession wrapper with image caching support
Easy HTTP Networking in Swift a NSURLSession wrapper with image caching support

Networking was born out of the necessity of having a simple networking library that doesn't have crazy programming abstractions or uses the latest rea

Jun 11, 2022
A modern Swift wrapper for Instagram Private API.
A modern Swift wrapper for Instagram Private API.

Swiftagram is a wrapper for Instagram Private API, written entirely in (modern) Swift. Instagram's official APIs, both the Instagram Basic Display API

Jun 13, 2022