<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How do you use State Preservation and Restoration in iOS?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/7543/how-do-you-use-state-preservation-and-restoration-in-ios</link><description>I have been working on an application for iOS for a few months now, and I want to give my users the best experience possible, when it comes to the peripheral I intend to use with the app, and in order to do that, I would like to be able to use state preservation</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 22 Feb 2017 04:31:47 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/7543/how-do-you-use-state-preservation-and-restoration-in-ios" /><item><title>RE: How do you use State Preservation and Restoration in iOS?</title><link>https://devzone.nordicsemi.com/thread/26886?ContentTypeID=1</link><pubDate>Wed, 22 Feb 2017 04:31:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:285a4fbc-581f-450a-9119-d140f21d170b</guid><dc:creator>Nik</dc:creator><description>&lt;p&gt;Hi @maansson, I am considering that you have added the background mode for your central manager. If yes then whenever your application gets kill due to lack of memory then in any BLE events like Button clicked, or connection/disconnection your app will get launched in background. and then you can get your last connected peripheral with the identifier.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you use State Preservation and Restoration in iOS?</title><link>https://devzone.nordicsemi.com/thread/26885?ContentTypeID=1</link><pubDate>Wed, 19 Aug 2015 18:08:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:70f97ad3-8105-42f5-96eb-a14a3b4199b3</guid><dc:creator>Maansson</dc:creator><description>&lt;p&gt;If I connect to my peripheral, and the OS then kills the application, while I am still connected, then I am no longer receiving Bluetooth events in my application. Example: Start app, scan for peripheral, connect to peripheral, kill app, press button on peripheral -- No reaction from app.&lt;/p&gt;
&lt;p&gt;Also, State Preservation and Restoration doesn&amp;#39;t seem to work if I kill my application, and then turn Bluetooth off and on again. The application doesn&amp;#39;t get woken up again. Can this be fixed?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you use State Preservation and Restoration in iOS?</title><link>https://devzone.nordicsemi.com/thread/26884?ContentTypeID=1</link><pubDate>Wed, 19 Aug 2015 18:04:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c702aa00-e002-4c3e-98c7-bcbfb4830ac2</guid><dc:creator>Maansson</dc:creator><description>&lt;p&gt;Hey Kamran, I forgot to tell you that it works, and has been working for a couple of months now :)!!! Thank you so much, I just have a couple more questions if you don&amp;#39;t mind&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you use State Preservation and Restoration in iOS?</title><link>https://devzone.nordicsemi.com/thread/26878?ContentTypeID=1</link><pubDate>Mon, 15 Jun 2015 07:55:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9f09b7c5-7420-4d12-9caa-cc3a7198f702</guid><dc:creator>Kamran</dc:creator><description>&lt;p&gt;you can place them in your viewcontroller where you are trying to connect/scan peripheral.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you use State Preservation and Restoration in iOS?</title><link>https://devzone.nordicsemi.com/thread/26883?ContentTypeID=1</link><pubDate>Mon, 15 Jun 2015 04:12:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:102b298b-b3d4-48a7-a616-8f0e97d9d4e1</guid><dc:creator>Maansson</dc:creator><description>&lt;p&gt;Do I have to put the CentralManager and the willRestoreState method in my AppDelegate class, or can I write in my ViewController?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you use State Preservation and Restoration in iOS?</title><link>https://devzone.nordicsemi.com/thread/26877?ContentTypeID=1</link><pubDate>Thu, 11 Jun 2015 13:27:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a87330a2-60c9-42e8-a7ea-a4f19d33f92d</guid><dc:creator>Kamran</dc:creator><description>&lt;p&gt;sounds good,It is also important to note that you should check if peripheral identifier is already saved or not.
Following code will check that.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if ([perId objectForKey:@&amp;quot;peripheralIdentifier&amp;quot;]) {

//Peripheral identifier is already saved

}

else {

//Peripheral identifier is not already saved

}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you use State Preservation and Restoration in iOS?</title><link>https://devzone.nordicsemi.com/thread/26882?ContentTypeID=1</link><pubDate>Thu, 11 Jun 2015 13:09:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:222b1c54-4506-40b2-8e94-207f7398c4f3</guid><dc:creator>Maansson</dc:creator><description>&lt;p&gt;I am saving my peripheral identifier in my didConnectPeripheral method, I am retrieving the identifier in my didFinishLaunchingWithOptions method, and I am trying to reconnect, or scan, in my willRestoreState method. Does that sound right?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you use State Preservation and Restoration in iOS?</title><link>https://devzone.nordicsemi.com/thread/26881?ContentTypeID=1</link><pubDate>Thu, 11 Jun 2015 12:23:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:39793e5b-ea7f-4eba-962f-38a46582c6a4</guid><dc:creator>Maansson</dc:creator><description>&lt;p&gt;I will, thank you so much. I&amp;#39;m pretty new to iOS, and I really want to get this done, so I really appreciate your help a lot. Fingers crossed!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you use State Preservation and Restoration in iOS?</title><link>https://devzone.nordicsemi.com/thread/26880?ContentTypeID=1</link><pubDate>Thu, 11 Jun 2015 12:19:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:695c6460-7409-49e8-a80b-974eb76dca14</guid><dc:creator>Kamran</dc:creator><description>&lt;p&gt;hopefully it will work, if not then just let us know :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you use State Preservation and Restoration in iOS?</title><link>https://devzone.nordicsemi.com/thread/26879?ContentTypeID=1</link><pubDate>Thu, 11 Jun 2015 12:11:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:918bd7cc-b81b-4094-acb0-56f7d1856330</guid><dc:creator>Maansson</dc:creator><description>&lt;p&gt;So, the only thing I did right was the initialisation of the centralManager?
I will try to use NSUserDefaults, and then I just write the retrieval part in my willRestoreState method?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you use State Preservation and Restoration in iOS?</title><link>https://devzone.nordicsemi.com/thread/26876?ContentTypeID=1</link><pubDate>Thu, 11 Jun 2015 10:53:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:937ac30b-e063-4219-b305-f8a484be1f96</guid><dc:creator>Kamran</dc:creator><description>&lt;p&gt;In Apple documentation,
&lt;a href="https://goo.gl/DHYPMm"&gt;https://goo.gl/DHYPMm&lt;/a&gt;
There are 3 ways to get the peripheral.&lt;/p&gt;
&lt;p&gt;1- retrievePeripheralsWithIdentifiers: for known peripherals and you need peripheral identifier (NSUUID).&lt;/p&gt;
&lt;p&gt;2- retrieveConnectedPeripheralsWithServices: for connected peripherals and you need to provide Primary Bluetooth LE Service (CBService).&lt;/p&gt;
&lt;p&gt;3- scanForPeripheralsWithServices: for unknown peripherals.&lt;/p&gt;
&lt;p&gt;In order to retrieve known peripheral you must save peripheral identifier. you can use NSUserDefault to save and retrieve the peripheral identifier.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;//save peripheral identifier using NSUserDefault

NSUserDefaults *perId = [NSUserDefaults standardUserDefaults];

[perId setObject:[peripheral.identifier] forKey:@&amp;quot;peripheralIdentifier&amp;quot;];

[perId synchronize];

//retrive peripheral identifier when next time user launch app

NSUserDefaults *perId = [NSUserDefaults standardUserDefaults];

NSUUID *peripheralIdentifier = [perId objectForKey:@&amp;quot;peripheralIdentifier&amp;quot;];

//Retrive know peripheral by giving the identifier

NSArray *peripherals = [self.centralManager 
retrievePeripheralsWithIdentifiers:@[peripheralIdentifier]];

if ([peripherals count]&amp;gt;0)

{
      CBPeripheral *peripheral=[peripherals objectAtIndex:0];

      peripheral.delegate=self;

      [self.connectingPeripherals addObject:peripheral];

      NSLog(@&amp;quot;Retrieved Known peripheral %@ its name %@&amp;quot;,peripheral.identifier,peripheral.name);

     [self.centralManager connectPeripheral:peripheral options:nil]; 

     } else {

     //scan peripheral

     }
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>