Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | 云栖社区 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Github | Search | About

第 3 章 AFNetWorking 3.0

目录

3.1. Oauth2 + Jwt 认证
3.2.
3.3.
3.4. 上传文件
3.4.1. appendPartWithFileData 方式
3.4.2. appendPartWithFileURL
3.4.3. 上传 MP4

3.1. Oauth2 + Jwt 认证

Config.h

		
#define BASE_URL              @"http://api.netkiller.cn:8080"
#define CLIENTID              @"api"
#define SECRET                @"secret"
#define OAUTH_TOKEN           @"/oauth/token"
#define OAUTH_USERNAME        @"blockchain"
#define OAUTH_PASSWORD        @"123456"		
		
		

AppDelegate.m

		
#import "AppDelegate.h"
#import "YTKNetworkConfig.h"
#import "AFOAuth2Manager.h"
#import <AFNetworking.h>
#import "Config.h"		
		
@interface AppDelegate ()
@property (nonatomic, strong) AFOAuthCredential *credential;
@property (nonatomic, strong) AFOAuth2Manager *OAuth2Manager;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
    //授权
    NSURL *url = [NSURL URLWithString:BASE_URL];
    
    AFOAuth2Manager *OAuth2Manager = [[AFOAuth2Manager alloc]initWithBaseURL:url clientID:CLIENTID secret:SECRET];
    self.OAuth2Manager = OAuth2Manager;
    [OAuth2Manager authenticateUsingOAuthWithURLString:OAUTH_TOKEN
                                              username:OAUTH_USERNAME
                                              password:OAUTH_PASSWORD
                                                 scope:@""
    success:^(AFOAuthCredential *credential) {
      // NSLog(@"********请求OauthToek成功***********");
            self.credential = credential;
//          [self testPost];
//          [self testGet];
//            [self uploadImage];
        [self testJson];
    }
    failure:^(NSError *error) {
     //  NSLog(@"********请求OauthToekn失败begin***********\r\n%@\r\n*********请求OauthToekn失败end************",error);
    }];
    // Override point for customization after application launch.
    return YES;
}

@end