github: https://github.com/hellovoidworld/HVWWeibo
A.app启动步骤
1.加入了授权步骤之后,最先要判断app内是否已经登陆了账号
2.在程序启动的时候,先检测是否已有登陆账号
AppDelegate:
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // Override point for customization after application launch. 3 4 // 启动后显示状态栏 5 UIApplication *app = [UIApplication sharedApplication]; 6 app.statusBarHidden = NO; 7 8 // 设置window 9 self.window = [[UIWindow alloc] init]; 10 self.window.frame = [UIScreen mainScreen].bounds; 11 [self.window makeKeyAndVisible]; 12 13 // 检查是否已有登陆账号 14 NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 15 NSString *filePath = [docPath stringByAppendingPathComponent:@"accountInfo.plist"]; 16 NSDictionary *accountInfo = [NSDictionary dictionaryWithContentsOfFile:filePath]; 17 18 if (!accountInfo) { // 如果不存在登陆账号,要先进行授权 19 self.window.rootViewController = [[HVWOAuthViewController alloc] init]; 20 } else { 21 /** 新版本特性 */ 22 // app现在的版本 23 // 由于使用的时Core Foundation的东西,需要桥接 24 NSString *versionKey = (__bridge NSString*) kCFBundleVersionKey; 25 NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; 26 NSString *currentVersion = [infoDic objectForKey:versionKey]; 27 28 // 上次使用的版本 29 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 30 NSString *lastVersion = [defaults stringForKey:versionKey]; 31 32 // 如果版本变动了,存储新的版本号并启动新版本特性图 33 if (![lastVersion isEqualToString:currentVersion]) { 34 35 // 存储 36 [defaults setObject:currentVersion forKey:versionKey]; 37 [defaults synchronize]; 38 39 // 开启app显示新特性 40 HVWNewFeatureViewController *newFeatureVC = [[HVWNewFeatureViewController alloc] init]; 41 self.window.rootViewController = newFeatureVC; 42 } else { 43 // 创建根控制器 44 HVWTabBarViewController *tabVC = [[HVWTabBarViewController alloc] init]; 45 self.window.rootViewController = tabVC; 46 } 47 } 48 49 return YES; 50 }
在授权控制器,授权完毕之后也要继续进入app:
1 // HVWOAuthViewController.m 2 /** 根据access_code获取access_token */ 3 - (void) accessTokenWithAccessCode:(NSString *) accessCode { 4 // 创建AFN的http操作请求管理者 5 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 6 7 // 参数设置 8 NSMutableDictionary *param = [NSMutableDictionary dictionary]; 9 param[@"client_id"] = @"3942775926"; 10 param[@"client_secret"] = @"cc577953b2aa3aa8ea220fd15775ea35"; 11 param[@"grant_type"] = @"authorization_code"; 12 param[@"code"] = accessCode; 13 param[@"redirect_uri"] = @"http://www.cnblogs.com/hellovoidworld/"; 14 15 // 发送请求 16 [manager POST:@"https://api.weibo.com/oauth2/access_token" parameters:param success:^(AFHTTPRequestOperation *operation, NSDictionary *accountInfo) { 17 [MBProgressHUD hideHUD]; 18 19 // 返回的是用户信息字典 20 // 存储用户信息,包括access_token到沙盒中 21 NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 22 NSString *filePath = [docPath stringByAppendingPathComponent:@"accountInfo.plist"]; 23 [accountInfo writeToFile:filePath atomically:YES]; 24 25 /** 新版本特性 */ 26 // app现在的版本 27 // 由于使用的时Core Foundation的东西,需要桥接 28 NSString *versionKey = (__bridge NSString*) kCFBundleVersionKey; 29 NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; 30 NSString *currentVersion = [infoDic objectForKey:versionKey]; 31 32 // 上次使用的版本 33 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 34 NSString *lastVersion = [defaults stringForKey:versionKey]; 35 36 UIWindow *window = [UIApplication sharedApplication].keyWindow; 37 38 // 如果版本变动了,存储新的版本号并启动新版本特性图 39 if (![lastVersion isEqualToString:currentVersion]) { 40 // 存储 41 [defaults setObject:currentVersion forKey:versionKey]; 42 [defaults synchronize]; 43 44 // 开启app显示新特性 45 HVWNewFeatureViewController *newFeatureVC = [[HVWNewFeatureViewController alloc] init]; 46 window.rootViewController = newFeatureVC; 47 } else { 48 // 创建根控制器 49 HVWTabBarViewController *tabVC = [[HVWTabBarViewController alloc] init]; 50 window.rootViewController = tabVC; 51 } 52 53 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 54 [MBProgressHUD hideHUD]; 55 HVWLog(@"请求access_token失败 ----> %@", error); 56 }]; 57 58 }
第一次启动:
需要授权+新特性显示
时间: 2024-10-11 04:07:33