{"id":566,"date":"2020-03-01T10:46:25","date_gmt":"2020-03-01T10:46:25","guid":{"rendered":"https:\/\/dev.i-smartcloud.com\/?page_id=566"},"modified":"2020-03-23T09:37:07","modified_gmt":"2020-03-23T09:37:07","slug":"api-ios","status":"publish","type":"page","link":"https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/","title":{"rendered":"API"},"content":{"rendered":"\n<p>The API code is used to login to a Cloud Service Account. We create an instance of that class by calling the constructor initWithService: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RAPI *rAPI = [[RAPI alloc]initWithService:rService];<\/code><\/pre>\n\n\n\n<p>But, if we already have an instance of a service, it is better to call the getAPI method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RAPI *rAPI = [rService getAPI];<\/code><\/pre>\n\n\n\n<p>The preferred method is the second one, and this is the method that is used in the Demo code. In the above example we created an instance of the RAPI class. You should never create such an instance. This is a generic class,  that has code that is used by the super-classes.<\/p>\n\n\n\n<p>There are two ways to login to an account: Normal Login, and OAuth 2 Login. Based on the login type you need to create a different UI and provide different data to the RAPI instance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Normal Login<\/h2>\n\n\n\n<p>Here the user is asked to enter his\/her email and password. For that we create a UI with three TextFields, and a &#8220;Login&#8221; button. We need to check that the value from those two fields is not an empty string. The code will not crash if they are an empty string, but, there is no need to make such a request. <\/p>\n\n\n\n<p>Once the user taps on the Login button, we pass the value of these two fields to the RAPI instance:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rAPI.email = &lt;email>;\nrApi.password = &lt;password>;\nrApi.name = &lt;name>;<\/code><\/pre>\n\n\n\n<p>In the Demo code, we also give a value to the variable &#8220;name&#8221; of the RAPI instance. You do not have to do this, but, if your users can connect to more than one account that variable will help them to distinguish these accounts.<\/p>\n\n\n\n<p>The Login button calls the method  checkServices on that RAPI instance:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[rAPI checkServices];<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Which Cloud Services use this type of Login?<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>iDrive<\/li><li>MediaFire<\/li><li>SugarSync<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">OAuth2 Login<\/h2>\n\n\n\n<p>This type of Login requires fewer code to write, as the only thing that you need to do is to create a UI with an WKWebView  instance. Then you set the  navigationDelegate of the WKWebVIew instance to be that ROAuthAPI instance:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ROAuthAPI *oauthAPI = (ROAuthAPI*)[rService getAPI];\n[webView setNavigationDelegate:oauthAPI];<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>The  ROAuthAPI  class is a superclass of the RAPI class. The method getAPI of the Service class will always return the correct instance.<\/p><\/blockquote>\n\n\n\n<p>Once again, do not forget to set the name variable of the ROAuthAPI  instance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Which Cloud Services use the OAuth2 Login?<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Box<\/li><li>Dropbox<\/li><li>Google Drive<\/li><li>OneDrive<\/li><li>ShareFile<\/li><li>SkyDrive<\/li><\/ul>\n\n\n\n<p>Even though the Google Drive uses an OAuth2 Login to login to an account. The above code won&#8217;t work. Instead you have run the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[rainbow connectToGoogle:service withName:service.name presentingViewController:self andResponseBlock:^(NSString *error) {\n            \n        }];<\/code><\/pre>\n\n\n\n<p>&#8220;rainbow&#8221; is the instance of the <a href=\"https:\/\/dev.i-smartcloud.com\/index.php\/rainbow-ios\/\">Rainbow<\/a> class, and &#8220;service&#8221; is the instance of the GoogleDriveService class. We may say, that connecting to a Google Drive account is a lot easier than connecting to any other accounts. You only need to call that method and in the appDelegate add the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary&lt;NSString *, id> *)options\n{\n    return [rainbow application:app openURL:url options:options];\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Delegate<\/h2>\n\n\n\n<p>The RAPI class uses a delegate to inform us about the login status. It is named &#8220;delegate&#8221; we set it in the normal way:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[rAPI setDelegate:self];<\/code><\/pre>\n\n\n\n<p> and you need to implement the following two methods:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-(void)APILoginWasSuccessful:(RServiceItem*)serviceItem forAPI:(RAPI*)gAPI;\n-(void)APISomethingWentWrong:(RServiceItem*)serviceItem forAPI:(RAPI*)gAPI errorMessage:(NSString*)msg;<\/code><\/pre>\n\n\n\n<p>If the login is successful, the RServiceItem instance is the account object that you will use in your code to create a <a href=\"https:\/\/dev.i-smartcloud.com\/index.php\/cloud-manager-ios\/\">CloudManager<\/a> instance. Also, a notification with the name SAVE_SERVICE_ACCOUNTS will be posted to the notification center.<\/p>\n\n\n\n<p>If there was an error, the msg would provide some useful information about what went wrong. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>The login will failed only in two cases: Wrong value of the clientID, clientSecret, and redirectURI or wrong credentials. In the first case, you need to double check their values. In the second case, you need to inform the user.<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Block<\/h2>\n\n\n\n<p>The RAPI class also has a block:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void (^onStatusChecked)(RAPI *gAPI, BOOL success) ;<\/code><\/pre>\n\n\n\n<p>There is no need to use that block. It is used by the CloudManager instances to check and refresh the Access Tokens. Also, this block should not be used during the Login process because it won&#8217;t give you any feedback on the error message.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>The API class is perhaps the most important class in the Rainbow SDK. If you follow our Demo code, you only need to implement the two methods of the delegate, and leave the rest to the SDK. You may need to listen for the notification with the name  SAVE_SERVICE_ACCOUNTS. The Rainbow SDK is listening for it, and if you have given a filePath to the Rainbow instance, it will save the new account into the local filesystem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The API code is used to login to a Cloud Service Account. We create an instance of that class by calling the constructor initWithService: But, if we already have an&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-566","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>API - Rainbow SDK<\/title>\n<meta name=\"description\" content=\"Instructions on how to connect to a Cloud Service account. Implement the two methods of the RAPI delegate, and let the Rainbow SDK do the rest.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"API - Rainbow SDK\" \/>\n<meta property=\"og:description\" content=\"Instructions on how to connect to a Cloud Service account. Implement the two methods of the RAPI delegate, and let the Rainbow SDK do the rest.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/\" \/>\n<meta property=\"og:site_name\" content=\"Rainbow SDK\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-23T09:37:07+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/\",\"url\":\"https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/\",\"name\":\"API - Rainbow SDK\",\"isPartOf\":{\"@id\":\"https:\/\/dev.i-smartcloud.com\/#website\"},\"datePublished\":\"2020-03-01T10:46:25+00:00\",\"dateModified\":\"2020-03-23T09:37:07+00:00\",\"description\":\"Instructions on how to connect to a Cloud Service account. Implement the two methods of the RAPI delegate, and let the Rainbow SDK do the rest.\",\"breadcrumb\":{\"@id\":\"https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/dev.i-smartcloud.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"API\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/dev.i-smartcloud.com\/#website\",\"url\":\"https:\/\/dev.i-smartcloud.com\/\",\"name\":\"Rainbow SDK\",\"description\":\"The Rainbow Cloud Manager at your disposal\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/dev.i-smartcloud.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"API - Rainbow SDK","description":"Instructions on how to connect to a Cloud Service account. Implement the two methods of the RAPI delegate, and let the Rainbow SDK do the rest.","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:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/","og_locale":"en_US","og_type":"article","og_title":"API - Rainbow SDK","og_description":"Instructions on how to connect to a Cloud Service account. Implement the two methods of the RAPI delegate, and let the Rainbow SDK do the rest.","og_url":"https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/","og_site_name":"Rainbow SDK","article_modified_time":"2020-03-23T09:37:07+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/","url":"https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/","name":"API - Rainbow SDK","isPartOf":{"@id":"https:\/\/dev.i-smartcloud.com\/#website"},"datePublished":"2020-03-01T10:46:25+00:00","dateModified":"2020-03-23T09:37:07+00:00","description":"Instructions on how to connect to a Cloud Service account. Implement the two methods of the RAPI delegate, and let the Rainbow SDK do the rest.","breadcrumb":{"@id":"https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/dev.i-smartcloud.com\/index.php\/api-ios\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/dev.i-smartcloud.com\/"},{"@type":"ListItem","position":2,"name":"API"}]},{"@type":"WebSite","@id":"https:\/\/dev.i-smartcloud.com\/#website","url":"https:\/\/dev.i-smartcloud.com\/","name":"Rainbow SDK","description":"The Rainbow Cloud Manager at your disposal","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/dev.i-smartcloud.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/dev.i-smartcloud.com\/index.php\/wp-json\/wp\/v2\/pages\/566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.i-smartcloud.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/dev.i-smartcloud.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/dev.i-smartcloud.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.i-smartcloud.com\/index.php\/wp-json\/wp\/v2\/comments?post=566"}],"version-history":[{"count":46,"href":"https:\/\/dev.i-smartcloud.com\/index.php\/wp-json\/wp\/v2\/pages\/566\/revisions"}],"predecessor-version":[{"id":845,"href":"https:\/\/dev.i-smartcloud.com\/index.php\/wp-json\/wp\/v2\/pages\/566\/revisions\/845"}],"wp:attachment":[{"href":"https:\/\/dev.i-smartcloud.com\/index.php\/wp-json\/wp\/v2\/media?parent=566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}