Write the client for Yandex.Metric for iPhone

As many of you probably know, Yandex.Metric is a wonderful API, allowing to obtain the data for your counters in XML or JSON.
I've been itching to write a mobile client, because during the day I often look at attendance figures of resources that are involved.
Existing clients for the iPhone it seemed to me terrible and finally the hands stopped scratching came to write
The client can be divided into the following stages:
-
the
- sign up applications in Yandex the
- OAuth user authorisation and getting token' the
- Save token' the
- Receiving of various data the
- Display
the
Registration
Registration applications are all clear — add the app, note the rights that the application wants to receive. During authorization the user must allow or deny permission to receive data, and will see what rights requests the application.
the
OAuth authorization
Personally, I was a little confusing when you are installing an unofficial client for some service, and he asks to enter the username and password in their form. You never know where they're going. In my opinion, the authorization process looks more transparent, if POPs up a browser window with the authorization on the service website.
Technically, OAuth authorization is quite simple — add to the app's own URL Scheme (e.g. myapp), the application settings on the Yandex website (where recorded the application) you specify as the Callback URI is something like myapp:// then the code below after logging in the app was trying to access the url at the address: myapp://, so the url will be opened in our application, and it has all the data, including the access_token.
On habré there are excellent article it was with this authorization and for Yandex, which saved me time, so much more to describe the authorization will not, not to be repeated. After login we have a token which we save.
Technical detail: logically, what ever the user wants to log out within the app. Anybody had no access to statistics but him, or, for example, to log into another account. When you login via a browser (i.e., via UIWebView), not enough to remove the previously obtained token. Your authorization is stored in the UIWebView, so when razloginivanie user need to clean cookies in the sandbox of the application or, if autorizatii you are automatically logged in to the same account. Simple code in my case was enough:
the
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies]) {
[storage deleteCookie:cookie];
}
[NSUserDefaults standardUserDefaults] synchronize];
the
Save token'
Important point
Picking his opportunities for the first time, even with articles in their native language, there is a slight desire to Bang your head on something. To understand all this when you just need to store n symbols and then take them out, really do not want. For the lazy like me, Apple's documentation have made the KeychainItemWrapper. The name speaks for itself — Abarca around the KeyChain. However, this class was written long ago and without the support of the ARC, but on github you can find his fork with support for ARC. this, for example (also using CocoaPods and can connect).
Well, now the saving and deleting token'and reduced to a few lines:
the
// save
KeychainItemWrapper *keychainWrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"OAuthToken" accessGroup:nil];
[keychainWrapper setObject:tokenToSave forKey:(__bridge id)(kSecValueData)];
the
// remove
KeychainItemWrapper *keychainWrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"OAuthToken" accessGroup:nil];
[keychainWrapper resetKeychainItem];
the
Getting different data
Personally, I like to get the data in JSON. With queries it is quite simple — helmet required query method of the API included in the header Authorization: OAuth <access_token>. By the way, can and token as get parameter to pass, for example:
api-metrika.yandex.ru/counters?oauth_token=<access_token>
Since we paranoid from the very beginning about safety, then all queries can be send via https (but can be via http). We get the data in JSON parsim through the built-in NSJSONSerialization:
the
dataObject = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:&e];
The result, for example, to retrieve a list of counters:

Well, then the received data need to be displayed.
the
Display
If you look at web-based Metrics, there are mainly 2 presentation of data — tables with text data and graphics.
Tables everything seems clear — use a UITableView. Almost all data contain the same General metrics — pageviews, visits, unique visitors, depth of view, time on site, bounce rate. So I did a class-a descendant of UITableViewCell, not to produce a bunch of identical code in each of your uiviewcontroller, then in the method
— (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
we just create a cell with already received data, for example:
the
NSString *CellIdentifier = [NSString stringWithFormat:@"CellId_%li_%li", (long)indexPath.section, (long)indexPath.row];
SourcesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( cell == nil ) {
cell = [[SourcesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier
textOnLabel:@"views" labelTextColor:[UIColor black]];
}
// set values
[cell
changeValuesWithpageViewsNum:pageViewsNum
visitTime:visitTime
denials:denialsValue
visitsNum:visitsNum
];
The time spent on the website, by the way, is returned in seconds, so it should nicely lead in the format mm: SS, and the failure rate from 0 to 1, so a 50% return as 0.5.
Drawing graphs there are several ready-made solutions. Something, again, find on habré. Personally, I liked on github PNChart — minimalist, nice. Connected via CocoaPods and the road.

Examples of creating graphs in PNChart is on the project page on Github.
Assembled all UITabsViewController, a little bit made out and produced by the quick stats in your pocket.

The result, I decided to upload in the AppStore, anyone can ustanoviti.
For the stimulus to continue development, make the app paid. Making 10 promo codes:
A49RNFA4W6KR
37LWRW7TYH36
RMPHML3EL6EL
JKWWFATEJ4YE
4RETFRLEPRFW
AWKN4LYRM4LK
L4K6NJNTN4RJ
NT9Y97HN7HKA
TRPXHFXX47PE
E96MN3JP9X9Y
Useful links:
CocоaPods is a powerful tool in the hands of an Objective-C developer
the Yandex OAuth authorization in iOS
PNChart (drawing graphs)
KeychainItemWrapper (a wrapper around the KeyChain)
Комментарии
Отправить комментарий