最近一直在做的是新浪微博客户端,暂时只实现了一个获取热门微博的小功能,当然各个功能都有其相通点,将这个功能做完善了,接下要实现其他功能就会相对简单。
先简单介绍一下实现这个功能的流程。步骤很简单:
第一步:我们先要进行OAuth授权,进行Web认证之后获取最重要的参数Access_token;
第二步:拿到Access_token之后获取热门微博的内容;
第三步:在这一步你可以添加其他功能,当然你也可以对获取到的内容进行详细加载,让受众看到更多的信息。
接下来将会对每一个步骤进行详细的介绍:
第一步:OAuth授权
自己尝试玩过微博客户端接口开发的朋友都知道,我们想要获取新浪微博提供给我们的用户数据,首先就要进行OAuth授权,新浪现在用的是OAuth2.0(关于什么是OAUth以及OAuth1.0与OAuth1.0的区别在上一篇博客中已有介绍,大家有兴趣可以去看看)。授权成功我们就做web认证,web认证的URL是:
https://api.weibo.com/oauth2/authorize?client_id=3914445803&redirect_uri=https://api.weibo.com/oauth2/default.html&response_type=code&display=mobile
在URL后面有四个参数,第一个client_id就是我么熟悉的Appkey了,申请一个应用后就可以得到。第二个参数是redirect_uri是回调网址。第三个response_type是最重要的参数,在接下来开发其他功能的时候都需要用到Access_token,这个参数就可以返回一个Access_token。第四个参数display,顾名思义就是展示方式。
这是加载登录界面的方法:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *url = URl;
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[self.webview setDelegate:self];
[self.webview loadRequest:request];
}
上面的URL就是OAuth的URL,同时在Webview上显示登录界面,效果如下:
当我们输入登录信息以后,我们已经得到了Access_token了,接下来要做得就是怎么去获取他。
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
_url = webView.request.URL.absoluteString;
if ([_url hasPrefix:URL_PAGE])
{
NSRange rangeOne=[_url rangeOfString:@"code="];
NSRange range = NSMakeRange(rangeOne.length+rangeOne.location, _url.length-(rangeOne.length+rangeOne.location));
_muString = [[NSMutableString alloc] initWithString:URL_CODE];
[_muString appendString:[_url substringWithRange:range]];
//创建URL
_urlString = [NSURL URLWithString:_muString];
//创建请求
_request =[[NSMutableURLRequest alloc]initWithURL:_urlString cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[_request setHTTPMethod:@"POST"];//设置请求方式为
_str = @"type=focus-c";//设置参数
_data = [_str dataUsingEncoding:NSUTF8StringEncoding];
[_request setHTTPBody:_data];
//连接服务器
_receive = [NSURLConnection sendSynchronousRequest:_request returningResponse:nil error:nil];
NSError *error;
_dictionary = [NSJSONSerialization JSONObjectWithData:_receive options:NSJSONReadingMutableContainers error:&error];
//获取Access_token
_access_token = [_dictionary objectForKey:@"access_token"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:_access_token forKey:@"access_token"];
[defaults synchronize];
}
}
第二步:获取热门微博得内容
在这里没有进行自定义表格得输出,在点击每一栏都会跳转到下一个界面看到更详细得信息。
TableView对应的.h内容如下:
#import <UIKit/UIKit.h>
@interface HotWeiBoViewController : UITableViewController
@property(nonatomic, strong)NSMutableArray *allItems;
@property(strong,nonatomic) NSString *m;
@property(strong,nonatomic) NSString *segue_image;
@property NSString *data1;
@property(strong,nonatomic)NSURL *url;
@property(strong,nonatomic)NSURLRequest *request;
@property(strong,nonatomic)NSData *response;
@property(strong,nonatomic)NSDictionary *weiboDic;
@property(strong,nonatomic)NSArray *arr;
@property(strong,nonatomic)NSArray *arr_user;
@end
然后我们会在.m文件中进行数据的加载:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"热门微博";
if(!self.allItems)
self.allItems=[NSMutableArray array];
self.refreshControl = [[UIRefreshControl alloc]init];
self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"ReFresh Data..."];
[self.refreshControl addTarget:self action:@selector(actionRefresh:)
forControlEvents:UIControlEventValueChanged];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *accessToken = [defaults objectForKey:@"access_token"];
NSString *PATH=[NSString stringWithFormat:URL_AccessToken,accessToken];
url = [NSURL URLWithString:PATH];
request = [NSURLRequest requestWithURL:url];
response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
weiboDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
arr= [weiboDic objectForKey:@"statuses"];
arr_user= [arr valueForKey:@"user"];
NSError* error =nil;
if (error != nil)
return;
for(int i=0;i<60;i++)
{
NSDictionary *d_us = arr_user[i];
NSDictionary *d = arr[i];
NewsItem *n=[[NewsItem alloc]initWithText:[d valueForKey:@"text"] andimageLink:[d_us valueForKey:@"profile_image_url"] andfriends_count:[d_us valueForKey:@"friends_count"] andfollowers_count:[d_us valueForKey:@"followers_count"] andname:[d_us valueForKey: @"name"] andDescription:[d_us valueForKey:@"description"]];
[self.allItems addObject:n];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure the cell...
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NewsItem *i=self.allItems[indexPath.row];
cell.textLabel.text=i.name;
cell.detailTextLabel.text = i.text;
NSURL *imageURL = [NSURL URLWithString:i.imageLink];
NSData *data = [[NSData alloc]initWithContentsOfURL:imageURL];
UIImage *img = [UIImage imageWithData:data];
CGSize sacleSize = CGSizeMake(30, 30);
UIGraphicsBeginImageContextWithOptions(sacleSize, NO, 0.0);
[img drawInRect:CGRectMake(0, 0, sacleSize.width, sacleSize.height)];
UIImage * resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image=resizedImage;
return cell;
}
上面这段代码实现的功能是设计表格每一栏要输出的信息,包括:用户头像、用户名、用户内容,想要获取更详细的信息,可以点击跳转到下一个界面
这是简单的实现,当然你想要在这个界面显示什么信息可以自己添加,而粉丝数量和关注人数两个信息我屏蔽了,所以没有显示。
#import <UIKit/UIKit.h>
#import "NewsItem.h"
@interface HotDetialViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextView *weiBoText;
@property (weak, nonatomic) IBOutlet UIImageView *weiBoImage;
@property (weak, nonatomic) IBOutlet UILabel *name_lable;
@property (weak, nonatomic) IBOutlet UITextView *description_textView;
@property (weak, nonatomic) IBOutlet UILabel *follows_lable;
@property (weak, nonatomic) IBOutlet UILabel *friends_lable;
@property NewsItem *d_i;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=@"Detail";
NSURL *imageURL = [NSURL URLWithString:_d_i.imageLink];
NSData *data = [[NSData alloc]initWithContentsOfURL:imageURL];
UIImage *img = [UIImage imageWithData:data];
self.weiBoText.text = _d_i.text;
self.weiBoImage.image = img;
self.name_lable.text = _d_i.name;
self.description_textView.text = _d_i.description;
//self.follows_lable.text = _d_i.followers_count;
//self.friends_lable.text = _d_i.friends_count;
}
其实整个功能还没有完整的实现,有些地方还可以进行进一步的抽象解偶,希望在下一篇的介绍中能有所改善。