diff --git a/Project/Project/ASAAppDelegate.m b/Project/Project/ASAAppDelegate.m index 20c221f..32c4716 100644 --- a/Project/Project/ASAAppDelegate.m +++ b/Project/Project/ASAAppDelegate.m @@ -33,6 +33,7 @@ - (BOOL) application:(UIApplication *)application // permissons:[kVKPermissionsArray componentsSeparatedByString:@","] // webView:self.webView // delegate:self]; + _rm = [[VKRequestManager alloc] initWithDelegate:self]; _rm.startAllRequestsImmediately = NO; @@ -58,7 +59,7 @@ - (void) connector:(VKConnector *)connector NSLog(@"Access token: %@", accessToken); -// _rm = [[VKRequestManager alloc] initWithDelegate:self]; +// _rm = [[VKRequestManager alloc] initWithDelegate:self user:[VKUser currentUser]]; // _rm.startAllRequestsImmediately = NO; // // VKRequest *r = [_rm info:@{@"user_ids": @"christian.burns"}]; diff --git a/Project/Vkontakte-iOS-SDK-LV/VKStorage/VKStorage.m b/Project/Vkontakte-iOS-SDK-LV/VKStorage/VKStorage.m index d4644b9..ef9b153 100644 --- a/Project/Vkontakte-iOS-SDK-LV/VKStorage/VKStorage.m +++ b/Project/Vkontakte-iOS-SDK-LV/VKStorage/VKStorage.m @@ -2,26 +2,26 @@ // Created by AndrewShmig on 6/28/13. // // Copyright (c) 2013 Andrew Shmig -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following +// Software is furnished to do so, subject to the following // conditions: -// +// // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE -// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // #import "VKStorage.h" @@ -31,7 +31,7 @@ @implementation VKStorage { - NSMutableDictionary *_storageItems; + NSMutableDictionary *_storageItems; } #pragma mark Visible VKStorage methods @@ -39,238 +39,250 @@ @implementation VKStorage - (instancetype)init { - VK_LOG(); - - self = [super init]; - - if (self) { - _storageItems = [[NSMutableDictionary alloc] init]; - } - - return self; + VK_LOG(); + + self = [super init]; + + if (self) { + _storageItems = [[NSMutableDictionary alloc] init]; + } + + return self; } #pragma mark - Shared storage + (instancetype)sharedStorage { - VK_LOG(); - - static VKStorage *sharedStorage; - static dispatch_once_t predicate; - - dispatch_once(&predicate, ^ - { - sharedStorage = [[[self class] alloc] init]; - -// проверим, если kVKStorageCachePath существует, если нет - создадим, -// а параллельно будет создан и kVKStoragePath - NSString *cacheStoragePath = [sharedStorage fullCacheStoragePath]; - - if (![[NSFileManager defaultManager] - fileExistsAtPath:cacheStoragePath]) { - - [[NSFileManager defaultManager] - createDirectoryAtPath:cacheStoragePath - withIntermediateDirectories:YES - attributes:nil - error:nil]; - } - }); - - [sharedStorage loadStorage]; - - return sharedStorage; + VK_LOG(); + + static VKStorage *sharedStorage; + static dispatch_once_t predicate; + + dispatch_once(&predicate, ^ + { + sharedStorage = [[[self class] alloc] init]; + + // проверим, если kVKStorageCachePath существует, если нет - создадим, + // а параллельно будет создан и kVKStoragePath + NSString *cacheStoragePath = [sharedStorage fullCacheStoragePath]; + + if (![[NSFileManager defaultManager] + fileExistsAtPath:cacheStoragePath]) { + + [[NSFileManager defaultManager] + createDirectoryAtPath:cacheStoragePath + withIntermediateDirectories:YES + attributes:nil + error:nil]; + } + }); + + [sharedStorage loadStorage]; + + return sharedStorage; } #pragma mark - Getters - (BOOL)isEmpty { - VK_LOG(); - - return ([_storageItems count] == 0); + VK_LOG(); + + return ([_storageItems count] == 0); } - (NSUInteger)count { - VK_LOG(); - + VK_LOG(); + + // ghost-user не считаем за реального пользователя + if (nil != _storageItems[@0]) { + return [_storageItems count] - 1; + } else { return [_storageItems count]; + } } - (VKStorageItem *)createStorageItemForAccessToken:(VKAccessToken *)token { - VK_LOG(); - - VKStorageItem *storageItem = [[VKStorageItem alloc] - initWithAccessToken:token - mainCacheStoragePath:[self fullCacheStoragePath]]; - - return storageItem; + VK_LOG(); + + VKStorageItem *storageItem = [[VKStorageItem alloc] + initWithAccessToken:token + mainCacheStoragePath:[self fullCacheStoragePath]]; + + return storageItem; } - (NSArray *)storageItems { - VK_LOG(); - - return [_storageItems allValues]; + VK_LOG(); + + // возвращаем из хранилища только реальных пользователей + // без ghost user + NSMutableDictionary *items = [_storageItems copy]; + + if (nil != items[@0]) + items[@0] = nil; + + return [items allValues]; } #pragma mark - Storage manipulation methods - (void)storeItem:(VKStorageItem *)item { - VK_LOG(@"%@", @{ - @"item": item - }); - - if (nil == item || nil == item.accessToken || nil == item.cache) - return; - -// удаляем данные предыдущей записи - [self removeItem:item]; - -// добавляем новые данные - id storageKey = @(item.accessToken.userID); - _storageItems[storageKey] = item; - - [self saveStorage]; + VK_LOG(@"%@", @{ + @"item": item + }); + + if (nil == item || nil == item.accessToken || nil == item.cache) + return; + + // удаляем данные предыдущей записи + [self removeItem:item]; + + // добавляем новые данные + id storageKey = @(item.accessToken.userID); + _storageItems[storageKey] = item; + + [self saveStorage]; } - (void)removeItem:(VKStorageItem *)item { - VK_LOG(@"%@", @{ - @"item": item - }); - - id storageKey = @(item.accessToken.userID); - - [item.cache removeCacheDirectory]; - [_storageItems removeObjectForKey:storageKey]; - - [self saveStorage]; + VK_LOG(@"%@", @{ + @"item": item + }); + + id storageKey = @(item.accessToken.userID); + + [item.cache removeCacheDirectory]; + [_storageItems removeObjectForKey:storageKey]; + + [self saveStorage]; } - (void)clean { - VK_LOG(); - - [_storageItems removeAllObjects]; - [self cleanCachedData]; - - [self saveStorage]; + VK_LOG(); + + [_storageItems removeAllObjects]; + [self cleanCachedData]; + + [self saveStorage]; } - (void)cleanCachedData { - VK_LOG(); - - dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); - - dispatch_async(backgroundQueue, ^ - { - - [[NSFileManager defaultManager] - removeItemAtPath:[self fullCacheStoragePath] - error:nil]; - - [[NSFileManager defaultManager] - createDirectoryAtPath:[self fullCacheStoragePath] - withIntermediateDirectories:YES - attributes:nil - error:nil]; - }); + VK_LOG(); + + dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); + + dispatch_async(backgroundQueue, ^ + { + + [[NSFileManager defaultManager] + removeItemAtPath:[self fullCacheStoragePath] + error:nil]; + + [[NSFileManager defaultManager] + createDirectoryAtPath:[self fullCacheStoragePath] + withIntermediateDirectories:YES + attributes:nil + error:nil]; + }); } - (VKStorageItem *)storageItemForUserID:(NSUInteger)userID { - VK_LOG(@"%@", @{ - @"userID": @(userID) - }); - - id storageKey = @(userID); - return _storageItems[storageKey]; + VK_LOG(@"%@", @{ + @"userID": @(userID) + }); + + id storageKey = @(userID); + return _storageItems[storageKey]; } - (VKStorageItem *)storageItemForUser:(VKUser *)user { - return [self storageItemForUserID:user.accessToken.userID]; + return [self storageItemForUserID:user.accessToken.userID]; } #pragma mark - Storage paths - (NSString *)fullStoragePath { - VK_LOG(); - - NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; - return [cachePath stringByAppendingFormat:@"%@", kVKStoragePath]; + VK_LOG(); + + NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; + return [cachePath stringByAppendingFormat:@"%@", kVKStoragePath]; } - (NSString *)fullCacheStoragePath { - VK_LOG(); - - NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; - return [cachePath stringByAppendingFormat:@"%@", kVKStorageCachePath]; + VK_LOG(); + + NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; + return [cachePath stringByAppendingFormat:@"%@", kVKStorageCachePath]; } #pragma mark - Storage hidden methods - (void)loadStorage { - VK_LOG(); - - NSDictionary *storageDefaults = [[NSUserDefaults standardUserDefaults] - objectForKey:kVKStorageUserDefaultsKey]; - -// хранилище пустое, создаем - if (nil == storageDefaults) { - [[NSUserDefaults standardUserDefaults] - setObject:@{} - forKey:kVKStorageUserDefaultsKey]; - } else { -// загрузка данных из хранилища - NSDictionary *storage = [[NSUserDefaults standardUserDefaults] - objectForKey:kVKStorageUserDefaultsKey]; - - [storage enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) - { - VKAccessToken *token = [NSKeyedUnarchiver unarchiveObjectWithData:obj]; - - VKStorageItem *storageItem = [[VKStorageItem alloc] - initWithAccessToken:token - mainCacheStoragePath:[self fullCacheStoragePath]]; - - _storageItems[@(token.userID)] = storageItem; - }]; - } + VK_LOG(); + + NSDictionary *storageDefaults = [[NSUserDefaults standardUserDefaults] + objectForKey:kVKStorageUserDefaultsKey]; + + // хранилище пустое, создаем + if (nil == storageDefaults) { + [[NSUserDefaults standardUserDefaults] + setObject:@{} + forKey:kVKStorageUserDefaultsKey]; + } else { + // загрузка данных из хранилища + NSDictionary *storage = [[NSUserDefaults standardUserDefaults] + objectForKey:kVKStorageUserDefaultsKey]; + + [storage enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) + { + VKAccessToken *token = [NSKeyedUnarchiver unarchiveObjectWithData:obj]; + + VKStorageItem *storageItem = [[VKStorageItem alloc] + initWithAccessToken:token + mainCacheStoragePath:[self fullCacheStoragePath]]; + + _storageItems[@(token.userID)] = storageItem; + }]; + } } - (void)saveStorage { - VK_LOG(); - -// сохраняем только данные токенов доступов -// данные кэшев мы сможем потом просто восстановить - NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults]; - NSMutableDictionary *newStorage = [[NSMutableDictionary alloc] init]; - - [_storageItems enumerateKeysAndObjectsUsingBlock:^(id key, - id obj, - BOOL *stop) - { - VKAccessToken *token = [(VKStorageItem *) obj accessToken]; - - [newStorage setObject:[NSKeyedArchiver archivedDataWithRootObject:token] - forKey:[NSString stringWithFormat:@"%d", token.userID]]; - }]; - - [myDefaults setObject:newStorage - forKey:kVKStorageUserDefaultsKey]; - - [myDefaults synchronize]; + VK_LOG(); + + // сохраняем только данные токенов доступов + // данные кэшев мы сможем потом просто восстановить + NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults]; + NSMutableDictionary *newStorage = [[NSMutableDictionary alloc] init]; + + [_storageItems enumerateKeysAndObjectsUsingBlock:^(id key, + id obj, + BOOL *stop) + { + VKAccessToken *token = [(VKStorageItem *) obj accessToken]; + + [newStorage setObject:[NSKeyedArchiver archivedDataWithRootObject:token] + forKey:[NSString stringWithFormat:@"%d", token.userID]]; + }]; + + [myDefaults setObject:newStorage + forKey:kVKStorageUserDefaultsKey]; + + [myDefaults synchronize]; } @end \ No newline at end of file diff --git a/Project/Vkontakte-iOS-SDK-LV/VKUser/VKUser.m b/Project/Vkontakte-iOS-SDK-LV/VKUser/VKUser.m index d83bf68..489350e 100644 --- a/Project/Vkontakte-iOS-SDK-LV/VKUser/VKUser.m +++ b/Project/Vkontakte-iOS-SDK-LV/VKUser/VKUser.m @@ -95,22 +95,23 @@ + (instancetype)ghostUser { return _ghostUser; } - // проверим, если призрачный пользователь был ранее создан VKStorageItem *item = [[VKStorage sharedStorage] storageItemForUser:_ghostUser]; + VKStorageItem *storageItem; if (nil == item) { - // пользователь не создан - создаем VKAccessToken *ghostUserAccessToken = [[VKAccessToken alloc] initWithUserID:kGhostUserID accessToken:@""]; // сохраняем токен доступа в хранилище - VKStorageItem *storageItem = [[VKStorage sharedStorage] - createStorageItemForAccessToken:ghostUserAccessToken]; + storageItem = [[VKStorage sharedStorage] + createStorageItemForAccessToken:ghostUserAccessToken]; [[VKStorage sharedStorage] storeItem:storageItem]; - - _ghostUser = [[VKUser alloc] initWithStorageItem:storageItem]; + } else { + storageItem = [[VKStorage sharedStorage] storageItemForUser:kGhostUserID]; } + _ghostUser = [[VKUser alloc] initWithStorageItem:storageItem]; + return _ghostUser; } diff --git a/Vkontakte-iOS-SDK-LV-Workspace.xcworkspace/xcuserdata/new.xcuserdatad/UserInterfaceState.xcuserstate b/Vkontakte-iOS-SDK-LV-Workspace.xcworkspace/xcuserdata/new.xcuserdatad/UserInterfaceState.xcuserstate index 8d2d2ff..ff80110 100644 Binary files a/Vkontakte-iOS-SDK-LV-Workspace.xcworkspace/xcuserdata/new.xcuserdatad/UserInterfaceState.xcuserstate and b/Vkontakte-iOS-SDK-LV-Workspace.xcworkspace/xcuserdata/new.xcuserdatad/UserInterfaceState.xcuserstate differ