IOS第三天-新浪微博 - 版本新特性,OAuth受權認證

*********版本新特性web

#import "HWNewfeatureViewController.h"
#import "HWTabBarViewController.h"
#define HWNewfeatureCount 4

@interface HWNewfeatureViewController () <UIScrollViewDelegate>
@property (nonatomic, weak) UIPageControl *pageControl;

@property (nonatomic, weak) UIScrollView *scrollView;
@end

@implementation HWNewfeatureViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1.建立一個scrollView:顯示全部的新特性圖片
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    // 2.添加圖片到scrollView中
    CGFloat scrollW = scrollView.width;
    CGFloat scrollH = scrollView.height;
    for (int i = 0; i<HWNewfeatureCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.width = scrollW;
        imageView.height = scrollH;
        imageView.y = 0;
        imageView.x = i * scrollW;
        // 顯示圖片
        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
        
        // 若是是最後一個imageView,就往裏面添加其餘內容
        if (i == HWNewfeatureCount - 1) {
            [self setupLastImageView:imageView];
        }
    }
    
#warning 默認狀況下,scrollView一建立出來,它裏面可能就存在一些子控件了
#warning 就算不主動添加子控件到scrollView中,scrollView內部仍是可能會有一些子控件
    
    // 3.設置scrollView的其餘屬性
    // 若是想要某個方向上不能滾動,那麼這個方向對應的尺寸數值傳0便可
    scrollView.contentSize = CGSizeMake(HWNewfeatureCount * scrollW, 0);
    scrollView.bounces = NO; // 去除彈簧效果
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    
    // 4.添加pageControl:分頁,展現目前看的是第幾頁
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = HWNewfeatureCount;
    pageControl.backgroundColor = [UIColor redColor];
    pageControl.currentPageIndicatorTintColor = HWColor(253, 98, 42);
    pageControl.pageIndicatorTintColor = HWColor(189, 189, 189);
    pageControl.centerX = scrollW * 0.5;
    pageControl.centerY = scrollH - 50;
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
    
    // UIPageControl就算沒有設置尺寸,裏面的內容仍是照常顯示的
    //    pageControl.width = 100;
    //    pageControl.height = 50;
    //    pageControl.userInteractionEnabled = NO;
    
//    UITextField *text = [[UITextField alloc] init];
//    text.frame = CGRectMake(10, 20, 100, 50);
//    text.borderStyle = UITextBorderStyleRoundedRect;
//    [self.view addSubview:text];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double page = scrollView.contentOffset.x / scrollView.width;
    // 四捨五入計算出頁碼
    self.pageControl.currentPage = (int)(page + 0.5);
    // 1.3四捨五入 1.3 + 0.5 = 1.8 強轉爲整數(int)1.8= 1
    // 1.5四捨五入 1.5 + 0.5 = 2.0 強轉爲整數(int)2.0= 2
    // 1.6四捨五入 1.6 + 0.5 = 2.1 強轉爲整數(int)2.1= 2
    // 0.7四捨五入 0.7 + 0.5 = 1.2 強轉爲整數(int)1.2= 1
}

/**
 *  初始化最後一個imageView
 *
 *  @param imageView 最後一個imageView
 */
- (void)setupLastImageView:(UIImageView *)imageView
{
    // 開啓交互功能
    imageView.userInteractionEnabled = YES;
    
    // 1.分享給你們(checkbox)
    UIButton *shareBtn = [[UIButton alloc] init];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [shareBtn setTitle:@"分享給你們" forState:UIControlStateNormal];
    [shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    shareBtn.width = 200;
    shareBtn.height = 30;
    shareBtn.centerX = imageView.width * 0.5;
    shareBtn.centerY = imageView.height * 0.65;
    [shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:shareBtn];
//    shareBtn.backgroundColor = [UIColor redColor];
//    shareBtn.imageView.backgroundColor = [UIColor blueColor];
//    shareBtn.titleLabel.backgroundColor = [UIColor yellowColor];
    
    // top left bottom right
    
    // EdgeInsets: 自切
    // contentEdgeInsets:會影響按鈕內部的全部內容(裏面的imageView和titleLabel)
    //    shareBtn.contentEdgeInsets = UIEdgeInsetsMake(10, 100, 0, 0);
    
    // titleEdgeInsets:隻影響按鈕內部的titleLabel
    shareBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
    // imageEdgeInsets:隻影響按鈕內部的imageView
//    shareBtn.imageEdgeInsets = UIEdgeInsetsMake(20, 0, 0, 50);
    
    
    
//    shareBtn.titleEdgeInsets
//    shareBtn.imageEdgeInsets
//    shareBtn.contentEdgeInsets
    
    // 2.開始微博
    UIButton *startBtn = [[UIButton alloc] init];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    startBtn.size = startBtn.currentBackgroundImage.size;
    startBtn.centerX = shareBtn.centerX;
    startBtn.centerY = imageView.height * 0.75;
    [startBtn setTitle:@"開始微博" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
}

- (void)shareClick:(UIButton *)shareBtn
{
    // 狀態取反
    shareBtn.selected = !shareBtn.isSelected;
}

- (void)startClick
{
    // 切換到HWTabBarController
    /*
     切換控制器的手段
     1.push:依賴於UINavigationController,控制器的切換是可逆的,好比A切換到B,B又能夠回到A
     2.modal:控制器的切換是可逆的,好比A切換到B,B又能夠回到A
     3.切換window的rootViewController
     */
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[HWTabBarViewController alloc] init];
    
    // modal方式,不建議採起:新特性控制器不會銷燬
//    HWTabBarViewController *main = [[HWTabBarViewController alloc] init];
//    [self presentViewController:main animated:YES completion:nil];
}

- (void)dealloc
{
    HWLog(@"HWNewfeatureViewController-dealloc");
}

/*
 1.程序啓動會自動加載叫作Default的圖片
 1> 3.5inch 非retain屏幕:Default.png
 2> 3.5inch retina屏幕:Default@2x.png
 3> 4.0inch retain屏幕: Default-568h@2x.png

 2.只有程序啓動時自動去加載的圖片, 纔會自動在4inch retina時查找-568h@2x.png
 */

/*
 一個控件用肉眼看不見,有哪些可能
 1.根本沒有建立實例化這個控件
 2.沒有設置尺寸
 3.控件的顏色跟父控件的背景色同樣(實際上已經顯示了,只不過用肉眼看不見)
 4.透明度alpha <= 0.01
 5.hidden = YES
 6.沒有添加到父控件中
 7.被其餘控件擋住了
 8.位置不對
 9.父控件發生了以上狀況
 10.特殊狀況
 * UIImageView沒有設置image屬性,或者設置的圖片名不對
 * UILabel沒有設置文字,或者文字顏色和跟父控件的背景色同樣
 * UITextField沒有設置文字,或者沒有設置邊框樣式borderStyle
 * UIPageControl沒有設置總頁數,不會顯示小圓點
 * UIButton內部imageView和titleLabel的frame被篡改了,或者imageView和titleLabel沒有內容
 * .....
 
 添加一個控件的建議(調試技巧):
 1.最好設置背景色和尺寸
 2.控件的顏色儘可能不要跟父控件的背景色同樣
 */
@end

**********HWNewfeatureViewController .hcanvas

#import <UIKit/UIKit.h>

@interface HWNewfeatureViewController : UIViewController

@end

 ************OAuth受權認證api

#import "HWOAuthViewController.h"
#import "AFNetworking.h"

@interface HWOAuthViewController () <UIWebViewDelegate>

@end

@implementation HWOAuthViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 1.建立一個webView
    UIWebView *webView = [[UIWebView alloc] init];
    webView.frame = self.view.bounds;
    webView.delegate = self;
    [self.view addSubview:webView];
    
    // 2.用webView加載登陸頁面(新浪提供的)
    // 請求地址:https://api.weibo.com/oauth2/authorize
    /* 請求參數:
     client_id    true    string    申請應用時分配的AppKey。
     redirect_uri    true    string    受權回調地址,站外應用需與設置的回調地址一致,站內應用需填寫canvas page的地址。
    */
    NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=3235932662&redirect_uri=http://"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

#pragma mark - webView代理方法
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//    HWLog(@"----webViewDidFinishLoad");
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
//    HWLog(@"----webViewDidStartLoad");
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    // 1.得到url
    NSString *url = request.URL.absoluteString;
    
    // 2.判斷是否爲回調地址
    NSRange range = [url rangeOfString:@"code="];
    if (range.length != 0) { // 是回調地址
        // 截取code=後面的參數值
        int fromIndex = range.location + range.length;
        NSString *code = [url substringFromIndex:fromIndex];
        
        // 利用code換取一個accessToken
        [self accessTokenWithCode:code];
    }
    
    return YES;
}

/**
 *  利用code(受權成功後的request token)換取一個accessToken
 *
 *  @param code 受權成功後的request token
 */
- (void)accessTokenWithCode:(NSString *)code
{
/*
 URL:https://api.weibo.com/oauth2/access_token
 
 請求參數:
 client_id:申請應用時分配的AppKey
 client_secret:申請應用時分配的AppSecret
 grant_type:使用authorization_code
 redirect_uri:受權成功後的回調地址
 code:受權成功後返回的code
 */
    // 1.請求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//    mgr.responseSerializer = [AFJSONResponseSerializer serializer];
    
    // AFN的AFJSONResponseSerializer默認不接受text/plain這種類型
    
    // 2.拼接請求參數
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"client_id"] = @"3235932662";
    params[@"client_secret"] = @"227141af66d895d0dd8baca62f73b700";
    params[@"grant_type"] = @"authorization_code";
    params[@"redirect_uri"] = @"http://";
    params[@"code"] = code;
    
    // 3.發送請求
    [mgr POST:@"https://api.weibo.com/oauth2/access_token" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        HWLog(@"請求成功-%@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        HWLog(@"請求失敗-%@", error);
    }];
}
@end

***ide

#import <UIKit/UIKit.h>

@interface HWOAuthViewController : UIViewController

@end
相關文章
相關標籤/搜索