前面一篇博文--- 新浪微部落格戶端Demo實踐之(一)OAuth2.0認證 已經談得如何擷取access_token了,那麼現在要做的是授權介面的視圖部分--UI。
授權頁面是一個webview,根據url就可以load到授權介面,下面就是授權的API 是https://api.weibo.com/oauth2/authorize,完整的授權介面的url如下
+ (NSString *) returnOAuthUrlString { return [NSString stringWithFormat:@"%@?client_id=%@&redirect_uri=%@&response_type=code&display=mobile&state=authorize",OAuth_URL,APP_KEY,APP_REDIRECT_URL];}
下面介紹一下我在這個視圖中是如何處理的。
(1)首先要儲存acess_token,用NSUserDefaults實現資料持久化,這樣的好處是,如果是第一次登陸就出現授權介面,授權後儲存access_token,之後登陸就不出現授權介面,直接進入微博首頁視圖了。
處理的相關代碼如下(在這個處理過程中我還添加了一個方法:擷取使用者的uid,這個參數在之後的API 呼叫中也是經常用到的,uid也儲存到NSUserDefaults):
- (void) getAccessToken : (NSString *) code{ //access token調用URL的string NSMutableString *accessTokenUrlString = [[NSMutableString alloc] initWithFormat:@"%@?client_id=%@&client_secret=%@&grant_type=authorization_code&redirect_uri=%@&code=",ACCESS_TOKEN_URL,APP_KEY,APP_SECRET,APP_REDIRECT_URL]; [accessTokenUrlString appendString:code]; //同步POST請求 NSURL *urlstring = [NSURL URLWithString:accessTokenUrlString]; //第二步,建立請求 NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:urlstring cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; [request setHTTPMethod:@"POST"];//佈建要求方式為POST,預設為GET //第三步,串連伺服器 NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *backString = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding]; //如何從backString中擷取到access_token NSDictionary *dictionary = [backString objectFromJSONString]; [[NSUserDefaults standardUserDefaults] setObject:[dictionary objectForKey:@"access_token"] forKey:@"access_token"]; [[NSUserDefaults standardUserDefaults] synchronize]; //在擷取到access_token之後就擷取使用者uid [self getUIDString];}- (void) getUIDString { NSString *uidURLString = [[NSString alloc] initWithFormat:@"%@?access_token=%@",GET_UID_URL,[InfoForSina returnAccessTokenString]]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:uidURLString]]; NSError *error; NSData *uidData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error]; NSString *uidString = [[NSString alloc] initWithData:uidData encoding:NSUTF8StringEncoding]; NSDictionary *uidDictionary = [uidString objectFromJSONString]; [[NSUserDefaults standardUserDefaults] setObject:[uidDictionary objectForKey:@"uid"] forKey:@"uid"]; [[NSUserDefaults standardUserDefaults] synchronize];}
下面簡單介紹一下NSUserDefaults:
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections
a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.
NSUserDefaults 是本機存放區資料最簡單的一種方式,NSUserDefaults用於儲存資料量小的資料,例如使用者配置。並不是所有的東西都能往裡放的,只支援:NSString, NSNumber, NSDate, NSArray, NSDictionary,如果你想儲存一些自訂的(除了前面提到的以外)資料,你就應該將他轉換成NSData
的形式(有人可能會想到說把我自訂的資料存到NSArray中,不就可以了嗎?答案是否定的!)
The synchronize method, which is automatically invoked at periodic intervals, keeps the in-memory cache in sync with a user’s defaults database.
定期調用同步方法 synchronize就可以保持資料,實現資料同步
Values returned from NSUserDefaults are immutable, even if you set a mutable object as the value.
從NSUserDefaults進行返回的值是不可改變的,即使你設定一個可變的對象作為value。
Persistence of NSURL and file reference URLs
當然也可以持久化保持NSURL和URLs
可以在文檔中找到相關的getter和setter 方法進行設定。這裡就不多說了。
下面詳細講一下如何在NSUserDefaults讀取和寫入自訂的資料(一般都是些自訂的類)。
解決方案:對自訂的類對象進行編碼成NSData形式進行儲存,使用時再對其進行解碼。需要在自訂的類中實現<NSCoding>協議中的兩個方法:- (id) initWithCoder: (NSCoder *)coder (編碼) 和 - (void) encodeWithCoder:(NSCoder
*)aCoder (解碼)。
下面以一個小小的demo程式實踐一下。我定義了一個student類,裡面有兩個property:chinaeseNameString 和 englishNameString,在實現方法中實現編碼和解碼的兩個方法。(注意添加NSCoding協議)。
以下分別是.h和.m檔案代碼,十分的簡單。
#import <Foundation/Foundation.h>@interface Student : NSObject <NSCoding>@property (nonatomic, strong) NSString *chinaeseNameString;@property (nonatomic, strong) NSString *englishNameString;@end
#import "Student.h"@implementation Student@synthesize chinaeseNameString = _chinaeseNameString;@synthesize englishNameString = _englishNameString;- (void) encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:_chinaeseNameString forKey:@"chinaeseNameString"]; [aCoder encodeObject:_englishNameString forKey:@"englishNameString"];}- (id) initWithCoder: (NSCoder *)coder { if (self = [super init]) { _chinaeseNameString = [coder decodeObjectForKey:@"chinaeseNameString"]; _englishNameString = [coder decodeObjectForKey:@"englishNameString"]; } return self;}@end
下面這段代碼建立一個student執行個體,編碼成NSData,儲存到NSUserDefaults,然後取出解碼,進行顯示。
Student *stu = [[Student alloc] init]; stu.chinaeseNameString= @"小鄧"; stu.englishNameString = @"xiaodeng"; NSData *data = [[NSData alloc] init]; data = [NSKeyedArchiver archivedDataWithRootObject:stu]; [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"Student"]; [[NSUserDefaults standardUserDefaults] synchronize]; Student *anotherStu = [[Student alloc] init]; NSData *anotherData = [[NSData alloc] init]; anotherData = [[NSUserDefaults standardUserDefaults] objectForKey:@"Student"]; anotherStu = [NSKeyedUnarchiver unarchiveObjectWithData:anotherData]; NSLog(@"anotherStu chinaeseName:%@",anotherStu.chinaeseNameString); NSLog(@"anotherStu englishName:%@",anotherStu.englishNameString);
注意到兩個方法:
+ (id)unarchiveObjectWithData:(NSData *)data
Decodes and returns the object graph previously encoded by NSKeyedArchiver and stored in a given NSData object.是 NSKeyedArchiver 的類方法
+ (id)unarchiveObjectWithData:(NSData *)data
Decodes and returns the object graph previously encoded by NSKeyedArchiver and stored in a given NSData object. 是 NSKeyedUnarchiver 的類方法
(2)程式啟動並執行最開始我設定了一個歡迎介面視圖imageview,顯示一張sina微博的圖片,延遲0.8秒進行動畫跳轉。根據NSUserDefaults中是否儲存有access_token決定跳轉到哪一個視圖(授權視圖webView,還是微博首頁視圖mainView)。
代碼實現如下:
- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view. hud = [[MBProgressHUD alloc] init]; if ([[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"] == nil) { hud.labelText = @"正在載入授權頁面..."; [hud show:YES]; [self.view addSubview:hud]; NSString *oauthUrlString = [InfoForSina returnOAuthUrlString]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:oauthUrlString]]; [self.webView setDelegate:self]; [self.webView loadRequest:request]; _imageView.hidden = NO; timer = [NSTimer scheduledTimerWithTimeInterval:0.8 target:self selector:@selector(goWebView) userInfo:nil repeats:NO]; } else { hud.labelText = @"正在載入微博內容..."; [hud show:YES]; [self.view addSubview:hud]; _imageView.hidden = NO; timer = [NSTimer scheduledTimerWithTimeInterval:0.8 target:self selector:@selector(goMainView) userInfo:nil repeats:NO]; } }- (void) goWebView { [UIView beginAnimations:nil context:NULL];[UIView setAnimationDuration:0.8];[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; _imageView.hidden = YES; [UIView commitAnimations];}- (void) goMainView { [UIView beginAnimations:nil context:NULL];[UIView setAnimationDuration:0.8];[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; _imageView.hidden = YES; [UIView commitAnimations]; [hud removeFromSuperview]; [self performSegueWithIdentifier:@"MainSegue" sender:nil];}- (void) webViewDidFinishLoad:(UIWebView *)webView { [hud removeFromSuperview];}