{"id":2252,"date":"2020-09-28T11:00:00","date_gmt":"2020-09-28T09:00:00","guid":{"rendered":"https:\/\/www.albertopasca.it\/whiletrue\/?p=2252"},"modified":"2023-06-26T15:55:52","modified_gmt":"2023-06-26T13:55:52","slug":"send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity","status":"publish","type":"post","link":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/","title":{"rendered":"Send data between Apple devices (iPhone\/iPad\/macOS) using Multipeer Connectivity"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Today I want to introduce an Apple framework, called <strong>Multipeer Connectivity<\/strong>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">&#8220;The Multipeer Connectivity framework supports the discovery of services provided by <strong>nearby devices<\/strong> and supports communicating with those services through <strong>message-based data<\/strong>, <strong>streaming<\/strong> data, and <strong>resources<\/strong> (such as files). In iOS, the framework uses infrastructure <em>Wi-Fi networks<\/em>, <em>peer-to-peer Wi-Fi<\/em>, and <em>Bluetooth<\/em> personal area networks for the underlying transport. In <strong>macOS<\/strong> and tvOS, it uses infrastructure <em>Wi-Fi<\/em>, <em>peer-to-peer Wi-Fi<\/em>, and <em>Ethernet<\/em>.&#8221;<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Reference here<\/strong>: <a href=\"https:\/\/developer.apple.com\/documentation\/multipeerconnectivity\">https:\/\/developer.apple.com\/documentation\/multipeerconnectivity<\/a> <\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">With these few phrases, you can imagine the potential behind this framework and the applications that can be done&#8230;<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-luminous-vivid-orange-color has-css-opacity has-luminous-vivid-orange-background-color has-background\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>H<\/strong>ow it works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To make it simpler, we can imagine <strong>Airdrop<\/strong>: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>you choose something to send (image, text, file, streaming,&#8230;)<\/li>\n\n\n\n<li>open AirDrop<\/li>\n\n\n\n<li>Search for <strong>users<\/strong><\/li>\n\n\n\n<li>select a user<\/li>\n\n\n\n<li>send data to the selected user<\/li>\n\n\n\n<li>the selected peer <strong>accept<\/strong> the transfer (<strong>the invitation<\/strong>)<\/li>\n\n\n\n<li>data is transferred.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Using <em>Multipeer Connectivity<\/em>, we can add some frameworks objects to the same bullet points:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>you choose something to send (image, text, file, streaming,&#8230;)<\/li>\n\n\n\n<li><s>open AirDrop<\/s> -&gt; initialize the <strong>MCSession<\/strong><\/li>\n\n\n\n<li>Search for <strong>users<\/strong> -&gt; initialize the <strong>MCNearbyServiceBrowser<\/strong> and the <strong>MCNearbyServiceAdvertiser<\/strong> <\/li>\n\n\n\n<li>select a <strong>user<\/strong> -&gt; <strong>MCPeerID<\/strong><\/li>\n\n\n\n<li>send data to the selected peer -&gt; <strong>MCSession.send()<\/strong><\/li>\n\n\n\n<li>the selected peer <strong>accept<\/strong> the transfer (<strong>the invitation<\/strong>) -&gt; <strong>MCNearbyServiceBrowser.invitePeer()<\/strong><\/li>\n\n\n\n<li>data is transferred -&gt; <strong>MCSessionDelegate.didReceive()<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The implementation is relatively simple.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-luminous-vivid-orange-color has-css-opacity has-luminous-vivid-orange-background-color has-background\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>S<\/strong>how me the code!<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Initialize the session, the browser and the advertiser:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">\/\/ helper initializer\npublic init( delegate: MPCManagerDelegate, peerId: String = UIDevice.current.name ) {\n    super.init()\n    \n    self.delegate = delegate\n    self.peer = MCPeerID(displayName: peerId)\n\n    initSession(peer: peer!, delegate: self)\n    initBrowser(peer: peer!, delegate: self, serviceType: serviceType)\n    initAdvertiser(peer: peer!, delegate: self, serviceType: serviceType)\n}\n\n\/\/ session initializer\nfileprivate func initSession( peer: MCPeerID, delegate: MCSessionDelegate ) {\n    session = MCSession(peer: peer, securityIdentity: nil, encryptionPreference: .required)\n    session.delegate = delegate\n}\n\n\/\/ browser initializer\nfileprivate func initBrowser( peer: MCPeerID, delegate: MCNearbyServiceBrowserDelegate, serviceType: String ) {\n    browser = MCNearbyServiceBrowser(peer: peer, serviceType: serviceType)\n    browser.delegate = delegate\n}\n\n\/\/ advertiser initializer\nfileprivate func initAdvertiser( peer: MCPeerID, delegate: MCNearbyServiceAdvertiserDelegate, serviceType: String ) {\n    advertiser = MCNearbyServiceAdvertiser(peer: peer, discoveryInfo: nil, serviceType: serviceType)\n    advertiser.delegate = delegate\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Well, the init is complete. Now we need to implements all the delegates, like: <strong>session.delegate<\/strong>, <strong>browser.delegate<\/strong> and <strong>advertiser.delegate<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>M<\/strong>CSessionDelegate<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Implements the <strong>MCSessionDelegate<\/strong> mandatory delegates:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {}\nfunc session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {}\n\nfunc session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {}\nfunc session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) {}\nfunc session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) {}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The second one is very important because is the callback of the data reception.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>M<\/strong>CNearbyServiceBrowserDelegate<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {}\n\nfunc browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can use the foundPeer delegate to add a peer in the list of founded peers (for any use).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>M<\/strong>CNearbyServiceAdvertiserDelegate<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In the Advertiser we manage <strong>the invitation<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -&gt; Void) {}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-cyan-bluish-gray-color has-css-opacity has-cyan-bluish-gray-background-color has-background\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Well, initialization is complete, delegates are created, we can start the scan.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">browser.startBrowsingForPeers()\nadvertiser.startAdvertisingPeer()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To test your code I suggest using a <strong>simulator and device<\/strong> (both running in debug) or two simulators, but you can debug only one each time.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-luminous-vivid-orange-color has-css-opacity has-luminous-vivid-orange-background-color has-background\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>R<\/strong>eal life application: a simple chat<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Using this framework you can chat with phones \/ ipads \/ mac <strong>around you<\/strong>&#8230; so why do not creating an &#8220;<strong>offline<\/strong>&#8221; chat?<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Let&#8217;s do it!<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The first step is to create a <strong>new XCode project<\/strong> and <strong>create an interface<\/strong> like this. Similar of course&#8230;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i2.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.02.16-1.png?fit=525%2C977&amp;ssl=1\" alt=\"\" class=\"wp-image-2441\" width=\"280\" height=\"521\" srcset=\"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.02.16-1.png?w=824&amp;ssl=1 824w, https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.02.16-1.png?resize=161%2C300&amp;ssl=1 161w, https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.02.16-1.png?resize=550%2C1024&amp;ssl=1 550w, https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.02.16-1.png?resize=768%2C1430&amp;ssl=1 768w\" sizes=\"auto, (max-width: 280px) 100vw, 280px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">It should be a <em>simple chat interface<\/em>, with a <strong>UITextfield<\/strong>, a <strong>UIButton<\/strong> and a <strong>UITableView<\/strong> with two types of <strong>UITableViewCells<\/strong> (one for <em>the sender<\/em> and one for <em>the receiver<\/em>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Connect all the <strong>IBOutlets<\/strong> and continue reading when you&#8217;re ready.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Next, create a new <strong>Message<\/strong> object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">struct Message {\n    var sender: String\n    var message: String\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and a local array of type <strong>Message<\/strong> in your <strong>ViewController<\/strong> to use later in the table view:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">lazy var messages: [Message] = [] \/\/ the table view rows<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Copy \/ paste the two helper function to send data to peers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">open func sendData(dictionaryWithData message: Message, toPeer targetPeer: [MCPeerID]) -&gt; Bool {\n    do {\n        let dictionary: [String: String] = [\"message\": message.message]\n        if let dataToSend: Data = try archivedData(dictionaryWithData: dictionary) {\n            try session.send(dataToSend, toPeers: targetPeer, with: .reliable)\n        }\n        return true\n    } catch {}\n    return false\n}\n\nprivate func archivedData( dictionaryWithData dictionary: Dictionary&lt;String, String&gt; ) throws -&gt; Data? {\n    return try NSKeyedArchiver.archivedData(withRootObject: dictionary, requiringSecureCoding: true)\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>session.send()<\/strong> accept a <strong>Data<\/strong> object. We can handle it now using the Message struct created before and convert it to a <strong>NSKeyedArchiver<\/strong> object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this way you can send data <em>to one or more<\/em> <strong>MCPeerID<\/strong>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">But how to get the peers?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To do this you can add peers in your list of discovered peers using the delegates of <strong>MCNearbyServiceBrowserDelegate<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can manage this for instance in the <strong>browser:foundPeer:withDiscoveryInfo<\/strong> callback.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to send data to the peer(s) you must invite them:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">browser.invitePeer(peerID, to: session, withContext: nil, timeout: timeout)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Last thing is to manage the data received.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To do this, go in the <strong>MCSessionDelegate<\/strong> methods and send a notification using the NotificationCenter to inform the app that you receive something or implement the design pattern you prefer for this.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">open func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {\n    let dictionary: [String: Any] = [\"data\": data, \"fromPeer\": peerID]\n    NotificationCenter.default.post(name: Notifications.kReceiveData.name, object: dictionary)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the ViewController, implement the notification observer and refresh the table when data change:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">NotificationCenter.default.addObserver(\n    self,\n    selector: #selector(handleReceivedData(notification:)),\n    name: Notifications.kReceiveData.name,\n    object: nil)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">@objc func handleReceivedData(notification: NSNotification) {\n    \/\/ refresh table with new message(s) received\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-luminous-vivid-orange-color has-css-opacity has-luminous-vivid-orange-background-color has-background\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">You should see something like this:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1738\" height=\"1838\" src=\"https:\/\/i2.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.24.21-1.png?fit=525%2C555&amp;ssl=1\" alt=\"\" class=\"wp-image-2466\" srcset=\"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.24.21-1.png?w=1738&amp;ssl=1 1738w, https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.24.21-1.png?resize=284%2C300&amp;ssl=1 284w, https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.24.21-1.png?resize=968%2C1024&amp;ssl=1 968w, https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.24.21-1.png?resize=768%2C812&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.24.21-1.png?resize=1452%2C1536&amp;ssl=1 1452w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Your &#8220;<strong>offline<\/strong>&#8221; chat is ready. Offline means that you don&#8217;t need to register to any web services.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can exchange <strong>Data<\/strong> using Wifi \/ Bluetooth and peer-to-peer network.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Repeat, you can send a <strong>Data<\/strong> object, so <em>you can send everything<\/em>!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">For the lazy var, the code is here: <a href=\"https:\/\/github.com\/elpsk\/MultipeerConnectivity-Example\">https:\/\/github.com\/elpsk\/MultipeerConnectivity-Example<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You should implement by yourself the invitation, the disconnection, and all the features available for this beautiful framework.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The source code not contain this, is just a simple example.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Enjoy peer!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I want to introduce an Apple framework, called Multipeer Connectivity. &#8220;The Multipeer Connectivity framework supports the discovery of services provided by nearby devices and supports communicating with those services through message-based data, streaming data, and resources (such as files). In iOS, the framework uses infrastructure Wi-Fi networks, peer-to-peer Wi-Fi, and Bluetooth personal area networks <a class=\"read-more\" href=\"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":2466,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[65,925,923,918,916,917,919,927,928,922,926],"class_list":["post-2252","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-generic","tag-chat","tag-devices","tag-discovery","tag-mcnearbyserviceadvertiser","tag-mcpeerid","tag-mcsession","tag-mcsessiondelegate","tag-message","tag-nskeyedarchiver","tag-peer-to-peer","tag-peripheral"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Send data between Apple devices (iPhone\/iPad\/macOS) using Multipeer Connectivity - while (true) {\u00a0}<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Send data between Apple devices (iPhone\/iPad\/macOS) using Multipeer Connectivity - while (true) {\u00a0}\" \/>\n<meta property=\"og:description\" content=\"Today I want to introduce an Apple framework, called Multipeer Connectivity. &#8220;The Multipeer Connectivity framework supports the discovery of services provided by nearby devices and supports communicating with those services through message-based data, streaming data, and resources (such as files). In iOS, the framework uses infrastructure Wi-Fi networks, peer-to-peer Wi-Fi, and Bluetooth personal area networks Read More\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/\" \/>\n<meta property=\"og:site_name\" content=\"while (true) {\u00a0}\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-28T09:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-26T13:55:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.24.21-1.png?fit=1738%2C1838&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"1738\" \/>\n\t<meta property=\"og:image:height\" content=\"1838\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Alberto Pasca\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@lupasca\" \/>\n<meta name=\"twitter:site\" content=\"@lupasca\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alberto Pasca\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\\\/\"},\"author\":{\"name\":\"Alberto Pasca\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"headline\":\"Send data between Apple devices (iPhone\\\/iPad\\\/macOS) using Multipeer Connectivity\",\"datePublished\":\"2020-09-28T09:00:00+00:00\",\"dateModified\":\"2023-06-26T13:55:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\\\/\"},\"wordCount\":710,\"publisher\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"image\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.albertopasca.it\\\/whiletrue\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/Schermata-2020-04-20-alle-12.24.21-1.png?fit=1738%2C1838&ssl=1\",\"keywords\":[\"chat\",\"devices\",\"discovery\",\"MCNearbyServiceAdvertiser\",\"MCPeerID\",\"MCSession\",\"MCSessionDelegate\",\"message\",\"NSKeyedArchiver\",\"peer-to-peer\",\"peripheral\"],\"articleSection\":[\"generic\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\\\/\",\"url\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\\\/\",\"name\":\"Send data between Apple devices (iPhone\\\/iPad\\\/macOS) using Multipeer Connectivity - while (true) {\u00a0}\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.albertopasca.it\\\/whiletrue\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/Schermata-2020-04-20-alle-12.24.21-1.png?fit=1738%2C1838&ssl=1\",\"datePublished\":\"2020-09-28T09:00:00+00:00\",\"dateModified\":\"2023-06-26T13:55:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/www.albertopasca.it\\\/whiletrue\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/Schermata-2020-04-20-alle-12.24.21-1.png?fit=1738%2C1838&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.albertopasca.it\\\/whiletrue\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/Schermata-2020-04-20-alle-12.24.21-1.png?fit=1738%2C1838&ssl=1\",\"width\":1738,\"height\":1838},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Send data between Apple devices (iPhone\\\/iPad\\\/macOS) using Multipeer Connectivity\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#website\",\"url\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/\",\"name\":\"while (true) {\u00a0}\",\"description\":\"notes from a developer, tutorials, how-to, ideas\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\",\"name\":\"Alberto Pasca\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"caption\":\"Alberto Pasca\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\"},\"description\":\"Software engineer @ Pirelli &amp; C. S.p.A. with a strong passion for mobile \uf8ff development, security, and connected things.\",\"sameAs\":[\"https:\\\/\\\/www.albertopasca.it\",\"albertopasca\",\"https:\\\/\\\/x.com\\\/lupasca\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Send data between Apple devices (iPhone\/iPad\/macOS) using Multipeer Connectivity - while (true) {\u00a0}","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/","og_locale":"en_US","og_type":"article","og_title":"Send data between Apple devices (iPhone\/iPad\/macOS) using Multipeer Connectivity - while (true) {\u00a0}","og_description":"Today I want to introduce an Apple framework, called Multipeer Connectivity. &#8220;The Multipeer Connectivity framework supports the discovery of services provided by nearby devices and supports communicating with those services through message-based data, streaming data, and resources (such as files). In iOS, the framework uses infrastructure Wi-Fi networks, peer-to-peer Wi-Fi, and Bluetooth personal area networks Read More","og_url":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/","og_site_name":"while (true) {\u00a0}","article_published_time":"2020-09-28T09:00:00+00:00","article_modified_time":"2023-06-26T13:55:52+00:00","og_image":[{"width":1738,"height":1838,"url":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.24.21-1.png?fit=1738%2C1838&ssl=1","type":"image\/png"}],"author":"Alberto Pasca","twitter_card":"summary_large_image","twitter_creator":"@lupasca","twitter_site":"@lupasca","twitter_misc":{"Written by":"Alberto Pasca","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/#article","isPartOf":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/"},"author":{"name":"Alberto Pasca","@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"headline":"Send data between Apple devices (iPhone\/iPad\/macOS) using Multipeer Connectivity","datePublished":"2020-09-28T09:00:00+00:00","dateModified":"2023-06-26T13:55:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/"},"wordCount":710,"publisher":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"image":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.24.21-1.png?fit=1738%2C1838&ssl=1","keywords":["chat","devices","discovery","MCNearbyServiceAdvertiser","MCPeerID","MCSession","MCSessionDelegate","message","NSKeyedArchiver","peer-to-peer","peripheral"],"articleSection":["generic"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/","url":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/","name":"Send data between Apple devices (iPhone\/iPad\/macOS) using Multipeer Connectivity - while (true) {\u00a0}","isPartOf":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/#primaryimage"},"image":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.24.21-1.png?fit=1738%2C1838&ssl=1","datePublished":"2020-09-28T09:00:00+00:00","dateModified":"2023-06-26T13:55:52+00:00","breadcrumb":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/#primaryimage","url":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.24.21-1.png?fit=1738%2C1838&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.24.21-1.png?fit=1738%2C1838&ssl=1","width":1738,"height":1838},{"@type":"BreadcrumbList","@id":"https:\/\/www.albertopasca.it\/whiletrue\/send-data-between-apple-devices-iphone-ipad-macos-using-multipeer-connectivity\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.albertopasca.it\/whiletrue\/"},{"@type":"ListItem","position":2,"name":"Send data between Apple devices (iPhone\/iPad\/macOS) using Multipeer Connectivity"}]},{"@type":"WebSite","@id":"https:\/\/www.albertopasca.it\/whiletrue\/#website","url":"https:\/\/www.albertopasca.it\/whiletrue\/","name":"while (true) {\u00a0}","description":"notes from a developer, tutorials, how-to, ideas","publisher":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.albertopasca.it\/whiletrue\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac","name":"Alberto Pasca","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","caption":"Alberto Pasca"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g"},"description":"Software engineer @ Pirelli &amp; C. S.p.A. with a strong passion for mobile \uf8ff development, security, and connected things.","sameAs":["https:\/\/www.albertopasca.it","albertopasca","https:\/\/x.com\/lupasca"]}]}},"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2020\/04\/Schermata-2020-04-20-alle-12.24.21-1.png?fit=1738%2C1838&ssl=1","_links":{"self":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/2252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/comments?post=2252"}],"version-history":[{"count":78,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/2252\/revisions"}],"predecessor-version":[{"id":3395,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/2252\/revisions\/3395"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/media\/2466"}],"wp:attachment":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/media?parent=2252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/categories?post=2252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/tags?post=2252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}