{"id":2171,"date":"2014-07-29T17:42:48","date_gmt":"2014-07-29T13:42:48","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=2171"},"modified":"2014-07-30T13:31:43","modified_gmt":"2014-07-30T09:31:43","slug":"on-ios-state-preservation-and-restoration-mechanism","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=2171","title":{"rendered":"iOS State Restoration vs NSCoding"},"content":{"rendered":"<p>My goal was to create a mechanism to store user interface state in my app. So I decided to study Apple&#8217;s State Preservation and Restoration, that was first introduced in iOS 6. As far, as I could understand on my own experience use:<br \/>\n1. <strong>iOS State Preservation and Restoration<\/strong>, if you want to store navigation state in an app, that doesn&#8217;t change root controller of a window at some moment. It is convenient in this case.<br \/>\n2. <strong>NSCoding Protocol<\/strong> for simpler tasks, when you don&#8217;t want to navigate, but want to save interface states for only several forms, because it is much more flexible &#8211; you completely control the process.<\/p>\n<p>I have tried to implement <a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/iphone\/conceptual\/iphoneosprogrammingguide\/StatePreservation\/StatePreservation.html#\/\/apple_ref\/doc\/uid\/TP40007072-CH11-SW13\" title=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/iphone\/conceptual\/iphoneosprogrammingguide\/StatePreservation\/StatePreservation.html#\/\/apple_ref\/doc\/uid\/TP40007072-CH11-SW13\" target=\"_blank\">State Preservation and Restoration<\/a> that was introduced in iOS 6 in my app but unfortunately I failed. The problem was that it&#8217;s mechanism is not flexible enough. It can restore state of View Controllers in a linear tree. But if your app as mine changes a Root Controller of your Window at some moment after user logins, for instance, this mechanism will not work. However, in other apps with simpler architecture it can be successfully used. It helps to bring a user right to the place where he stopped using your app last time even if app is removed from the background.<\/p>\n<p>There is a <a href=\"http:\/\/www.slideshare.net\/robby_brown\/ios-state-preservation-and-restoration\" title=\"http:\/\/www.slideshare.net\/robby_brown\/ios-state-preservation-and-restoration\" target=\"_blank\">good presentation<\/a> on this topic on SlideShare. <\/p>\n<p>I used <a href=\"http:\/\/useyourloaf.com\/blog\/2013\/05\/21\/state-preservation-and-restoration.html\" title=\"http:\/\/useyourloaf.com\/blog\/2013\/05\/21\/state-preservation-and-restoration.html\">this tutorial<\/a> also. There is a <a href=\"https:\/\/github.com\/kharrison\/CodeExamples\/tree\/master\/Restorer\" title=\"https:\/\/github.com\/kharrison\/CodeExamples\/tree\/master\/Restorer\" target=\"_blank\">code on GitHub<\/a>, that works. <\/p>\n<p>However, there is a good flexible alternative: saving objects to text files using NSCoding protocol: <\/p>\n<pre>\r\n- (void)viewDidLoad\r\n{\r\n  [super viewDidLoad];\r\n\r\n  \/\/ Restoring form object from the file\r\n  NSString *formFilePath = [self formFilePath];\r\n  RRCreateResumeForm *form = [NSKeyedUnarchiver unarchiveObjectWithFile:formFilePath];\r\n  if (form != nil) {\r\n    self.formController.form = form;\r\n  }\r\n}\r\n\r\n\r\n- (void)viewWillDisappear:(BOOL)animated\r\n{\r\n  [super viewWillDisappear:animated];\r\n  \r\n  \/\/ Saving the form object to the file\r\n  NSString *formFilePath = [self formFilePath];\r\n  [NSKeyedArchiver archiveRootObject:self.formController.form toFile:formFilePath];\r\n}\r\n\r\n\r\n\/\/ Returns a file path to the file with stored form data for form controller\r\n- (NSString *)formFilePath\r\n{\r\n  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);\r\n  NSString *documentsDirectory = [paths objectAtIndex:0];\r\n  NSString *formClassName = NSStringFromClass( [self.formController.form class] );\r\n  NSString *formFileName = [NSString stringWithFormat:@\"%@.txt\", formClassName];\r\n  NSString *formFilePath = [documentsDirectory stringByAppendingPathComponent:formFileName];\r\n  \r\n  return formFilePath;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>My goal was to create a mechanism to store user interface state in my app. So I decided to study Apple&#8217;s State Preservation and Restoration, that was first introduced in iOS 6. As far, as I could understand on my own experience use: 1. iOS State Preservation and Restoration, if you want to store navigation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[187,85,84],"class_list":["post-2171","post","type-post","status-publish","format-standard","hentry","category-ios","tag-ios","tag-nscoding","tag-state-restoration"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>iOS State Restoration vs NSCoding - Denis Kutlubaev<\/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:\/\/blog.alwawee.ru\/?p=2171\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"iOS State Restoration vs NSCoding - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"My goal was to create a mechanism to store user interface state in my app. So I decided to study Apple&#8217;s State Preservation and Restoration, that was first introduced in iOS 6. As far, as I could understand on my own experience use: 1. iOS State Preservation and Restoration, if you want to store navigation [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=2171\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2014-07-29T13:42:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-07-30T09:31:43+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.alwawee.com\/?attachment_id=1982\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2171#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2171\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"iOS State Restoration vs NSCoding\",\"datePublished\":\"2014-07-29T13:42:48+00:00\",\"dateModified\":\"2014-07-30T09:31:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2171\"},\"wordCount\":260,\"commentCount\":0,\"keywords\":[\"iOS\",\"NSCoding\",\"State Restoration\"],\"articleSection\":[\"iOS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2171#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2171\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2171\",\"name\":\"iOS State Restoration vs NSCoding - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"datePublished\":\"2014-07-29T13:42:48+00:00\",\"dateModified\":\"2014-07-30T09:31:43+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2171#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2171\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2171#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"iOS State Restoration vs NSCoding\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"iOS State Restoration vs NSCoding - Denis Kutlubaev","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:\/\/blog.alwawee.ru\/?p=2171","og_locale":"en_US","og_type":"article","og_title":"iOS State Restoration vs NSCoding - Denis Kutlubaev","og_description":"My goal was to create a mechanism to store user interface state in my app. So I decided to study Apple&#8217;s State Preservation and Restoration, that was first introduced in iOS 6. As far, as I could understand on my own experience use: 1. iOS State Preservation and Restoration, if you want to store navigation [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=2171","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2014-07-29T13:42:48+00:00","article_modified_time":"2014-07-30T09:31:43+00:00","og_image":[{"url":"http:\/\/blog.alwawee.com\/?attachment_id=1982","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=2171#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=2171"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"iOS State Restoration vs NSCoding","datePublished":"2014-07-29T13:42:48+00:00","dateModified":"2014-07-30T09:31:43+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=2171"},"wordCount":260,"commentCount":0,"keywords":["iOS","NSCoding","State Restoration"],"articleSection":["iOS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=2171#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=2171","url":"https:\/\/blog.alwawee.ru\/?p=2171","name":"iOS State Restoration vs NSCoding - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"datePublished":"2014-07-29T13:42:48+00:00","dateModified":"2014-07-30T09:31:43+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=2171#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=2171"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=2171#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"iOS State Restoration vs NSCoding"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2171"}],"version-history":[{"count":0,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2171\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}