模型转对象使用归档保存在本地

//

//  IWAccount.h

//  ItcastWeibo

//

//  Created by apple on 14-5-8.

//  Copyright (c) 2014 itcast. All rights reserved.

//  帐号模型


#import <Foundation/Foundation.h>


@interface IWAccount : NSObject <NSCoding>

@property (nonatomic, copy) NSString *access_token;

// 如果服务器返回的数字很大, 建议用long long(比如主键, ID)

@property (nonatomic, assign) long long expires_in;

@property (nonatomic, assign) long long remind_in;

@property (nonatomic, assign) long long uid;


+ (instancetype)accountWithDict:(NSDictionary *)dict;

- (instancetype)initWithDict:(NSDictionary *)dict;

@end



//

//  IWAccount.m

//  ItcastWeibo

//

//  Created by apple on 14-5-8.

//  Copyright (c) 2014 itcast. All rights reserved.

//


#import "IWAccount.h"


@implementation IWAccount

+ (instancetype)accountWithDict:(NSDictionary *)dict

{

    return [[self alloc] initWithDict:dict];

}


- (instancetype)initWithDict:(NSDictionary *)dict

{

    if (self = [super init]) {

        [self setValuesForKeysWithDictionary:dict];

    }

    return self;

}


/**

 *  从文件中解析对象的时候调

 */

- (id)initWithCoder:(NSCoder *)decoder

{

    if (self = [super init]) {

        self.access_token = [decoder decodeObjectForKey:@"access_token"];

        self.remind_in = [decoder decodeInt64ForKey:@"remind_in"];

        self.expires_in = [decoder decodeInt64ForKey:@"expires_in"];

        self.uid = [decoder decodeInt64ForKey:@"uid"];

    }

    return self;

}


/**

 *  将对象写入文件的时候调用

 */

- (void)encodeWithCoder:(NSCoder *)encoder

{

    [encoder encodeObject:self.access_token forKey:@"access_token"];

    [encoder encodeInt64:self.remind_in forKey:@"remind_in"];

    [encoder encodeInt64:self.expires_in forKey:@"expires_in"];

    [encoder encodeInt64:self.uid forKey:@"uid"];

}

@end


/**

 *  通过code换取一个accessToken

 redirect_uri true string 回调地址,需需与注册应用里的回调地址一致。

 */

- (void)accessTokenWithCode:(NSString *)code

{

    // AFNetworking\AFN

    // 1.创建请求管理对象

    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

    // 说明服务器返回的JSON数据

    mgr.responseSerializer = [AFJSONResponseSerializer serializer];

    

    // 2.封装请求参数

    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    params[@"client_id"] = @"1359433872";

    params[@"client_secret"] = @"37c372aa97a9329fc561947151c1bd38";

    params[@"grant_type"] = @"authorization_code";

    params[@"code"] = code;

    params[@"redirect_uri"] = @"http://ios.itcast.cn";

    

    // 3.发送请求

    [mgr POST:@"https://api.weibo.com/oauth2/access_token" parameters:params

      success:^(AFHTTPRequestOperation *operation, id responseObject) {

          // 4.先将字典转为模型

          IWAccount *account = [IWAccount accountWithDict:responseObject];

          

          // 5.存储模型数据

          NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

          NSString *file = [doc stringByAppendingPathComponent:@"account.data"];

          [NSKeyedArchiver archiveRootObject:account toFile:file];

          

          // 6.新特性\去首页

          NSString *key = @"CFBundleVersion";

          

          // 取出沙盒中存储的上次使用软件的版本号

          NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

          NSString *lastVersion = [defaults stringForKey:key];

          

          // 获得当前软件的版本号

          NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];

          

          if ([currentVersion isEqualToString:lastVersion]) {

              // 显示状态栏

              [UIApplication sharedApplication].statusBarHidden = NO;

              self.view.window.rootViewController = [[IWTabBarViewController alloc] init];

          } else { // 新版本

              self.view.window.rootViewController = [[IWNewfeatureViewController alloc] init];

              // 存储新版本

              [defaults setObject:currentVersion forKey:key];

              [defaults synchronize];

          }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        IWLog(@"请求失败:%@", error);

    }];

}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    

    // 先判断有无存储账号信息

    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSString *file = [doc stringByAppendingPathComponent:@"account.data"];

    IWAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:file];

    

    if (account) { // 之前登录成功

        NSString *key = @"CFBundleVersion";

        

        // 取出沙盒中存储的上次使用软件的版本号

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        NSString *lastVersion = [defaults stringForKey:key];

        

        // 获得当前软件的版本号

        NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];

        

        if ([currentVersion isEqualToString:lastVersion]) {

            // 显示状态栏

            application.statusBarHidden = NO;

            self.window.rootViewController = [[IWTabBarViewController alloc] init];

        } else { // 新版本

            self.window.rootViewController = [[IWNewfeatureViewController alloc] init];

            // 存储新版本

            [defaults setObject:currentVersion forKey:key];

            [defaults synchronize];

        }

    } else { // 之前没有登录成功

        self.window.rootViewController = [[IWOAuthViewController alloc] init];

    }

    

    [self.window makeKeyAndVisible];

    return YES;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值