API
The API code is used to login to a Cloud Service Account. We create an instance of that class by calling the constructor initWithService:
RAPI *rAPI = [[RAPI alloc]initWithService:rService];
But, if we already have an instance of a service, it is better to call the getAPI method:
RAPI *rAPI = [rService getAPI];
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.
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.
Normal Login
Here the user is asked to enter his/her email and password. For that we create a UI with three TextFields, and a “Login” 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.
Once the user taps on the Login button, we pass the value of these two fields to the RAPI instance:
rAPI.email = <email>;
rApi.password = <password>;
rApi.name = <name>;
In the Demo code, we also give a value to the variable “name” 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.
The Login button calls the method checkServices on that RAPI instance:
[rAPI checkServices];
Which Cloud Services use this type of Login?
- iDrive
- MediaFire
- SugarSync
OAuth2 Login
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:
ROAuthAPI *oauthAPI = (ROAuthAPI*)[rService getAPI];
[webView setNavigationDelegate:oauthAPI];
The ROAuthAPI class is a superclass of the RAPI class. The method getAPI of the Service class will always return the correct instance.
Once again, do not forget to set the name variable of the ROAuthAPI instance.
Which Cloud Services use the OAuth2 Login?
- Box
- Dropbox
- Google Drive
- OneDrive
- ShareFile
- SkyDrive
Even though the Google Drive uses an OAuth2 Login to login to an account. The above code won’t work. Instead you have run the following code:
[rainbow connectToGoogle:service withName:service.name presentingViewController:self andResponseBlock:^(NSString *error) {
}];
“rainbow” is the instance of the Rainbow class, and “service” 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:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *, id> *)options
{
return [rainbow application:app openURL:url options:options];
}
Delegate
The RAPI class uses a delegate to inform us about the login status. It is named “delegate” we set it in the normal way:
[rAPI setDelegate:self];
and you need to implement the following two methods:
-(void)APILoginWasSuccessful:(RServiceItem*)serviceItem forAPI:(RAPI*)gAPI;
-(void)APISomethingWentWrong:(RServiceItem*)serviceItem forAPI:(RAPI*)gAPI errorMessage:(NSString*)msg;
If the login is successful, the RServiceItem instance is the account object that you will use in your code to create a CloudManager instance. Also, a notification with the name SAVE_SERVICE_ACCOUNTS will be posted to the notification center.
If there was an error, the msg would provide some useful information about what went wrong.
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.
Block
The RAPI class also has a block:
void (^onStatusChecked)(RAPI *gAPI, BOOL success) ;
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’t give you any feedback on the error message.
Summary
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.