新浪微博客户端总结(1)

  最近一直在做的是新浪微博客户端,暂时只实现了一个获取热门微博的小功能,当然各个功能都有其相通点,将这个功能做完善了,接下要实现其他功能就会相对简单。

  先简单介绍一下实现这个功能的流程。步骤很简单:

  第一步:我们先要进行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;
    }
其实整个功能还没有完整的实现,有些地方还可以进行进一步的抽象解偶,希望在下一篇的介绍中能有所改善。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值