1,使用cocoapods安装方案
第一步:新建一个项目(如果你在已经有的项目构建,则不需要新建项目了)
touch一个文件:文件名字叫
Podfile
内容为:
platform :ios, '7.0'
target 'test' do
pod "Qiniu", '~>7.0'
end
第二步:安装cocoapods
CocoaPods是用Ruby开发的,可以通过系统自带的Ruby来安装。
gem install cocoapods
pod setup # 创建~/.cocoapods/repos
pod setup 会克隆 https://github.com/CocoaPods/Specs 的主分支,保存在本地。
注意:官方源延迟较大,可以先更换为国内的源:
gem sources -l
gem sources -r https://rubygems.org/
gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
第三步:然后打开终端,进入到工程目录。
$ pod install
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (2.6.3)
Installing HappyDNS (0.3.5)
Installing Qiniu (7.0.20)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `test.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 3 total pods installed.
第四步,在文件系统中找到你创建的工程的目录,找到*.xcworkspace这个文件然后右键用xcode打开。
正常的话,会有如下的信息:
第五步:在Main.storyboard这个文件中设置一个按钮(button)
此时点击编辑器右上角的第二个按钮(两个圈圈),将视图和代码分两边显示,选中button按住control和左键移动,会拉一个线到ViewController.m这个页面。会提示你button事件的方法名。然后在ViewController.m中写你的上传方法。
//
// MasterViewController.m
// test
//
// Created by LiuHanlin on 16/6/3.
// Copyright © 2016年 LiuHanlin. All rights reserved.
//
#import "MasterViewController.h"
#import "DetailViewController.h"
#import "QiniuSDK.h"
#import "QNEtag.h"
#import "QNConfiguration.h"
@interface MasterViewController ()
@property NSMutableArray *objects;
@end
@implementation MasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
- (void)viewWillAppear:(BOOL)animated {
self.clearsSelectionOnViewWillAppear = self.splitViewController.isCollapsed;
[super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)sender {
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - Segues
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = self.objects[indexPath.row];
DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
[controller setDetailItem:object];
controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
controller.navigationItem.leftItemsSupplementBackButton = YES;
}
}
- (IBAction)qiniu_upload:(id)sender {
// 测试上传
QNConfiguration *config = [QNConfiguration build:^(QNConfigurationBuilder *builder) {
QNServiceAddress *s = [[QNServiceAddress alloc] init:@"https://up.qbox.me" ips:nil];
builder.zone = [[QNZone alloc] initWithUp:s upBackup:nil];
}];
QNUploadManager *upManager = [[QNUploadManager alloc] initWithConfiguration:config];
// NSData *data1 = [@"Hello, World!" dataUsingEncoding : NSUTF8StringEncoding];
NSURL *fileUrl=[NSURL URLWithString:@"file:///Users/liuhanlin/Downloads/ddd.txt"];
NSData *data = [NSData dataWithContentsOfURL:fileUrl ];
[upManager putData:data key:nil token:@"cjBCKBOX4uk0c455PFCH7OJNBnXJg5IdePBaJShL:PLuw18jpgrpn6-kS7uyFLEY2y4Y=:eyJzY29wZSI6Im1lbmdiYW9zaG93IiwiZGVhZGxpbmUiOjE0NDI2NTk1Njh9"
complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
NSLog(@"%@", info);
NSLog(@"七牛返回信息%@", resp);
} option:nil];
// 测试文件的本地hash
NSURL *fileUrl2=[NSURL URLWithString:@"file:///Users/yishiyaonie/Downloads/trim.4C7DE72F-3BEC-4DBB-8FA4-4F11BFDEF0ED.MOV"];
NSData *data2 = [NSData dataWithContentsOfURL:fileUrl ];
NSString *key = [QNEtag data:data];
NSLog(@"%@", key);
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSDate *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
@end
如果需要选择不同区域的空间上传,可以通过:
客户端上传可以这样指定上传存储区
iOS 指定华北机房上传:
#import "QNConfiguration.h"
QNConfiguration *config = [QNConfiguration build:^(QNConfigurationBuilder *builder) {
builder.zone = [QNZone zone0];
}];
QNUploadManager *upManager = [[QNUploadManager alloc] initWithConfiguration:config];
只需要对应demo上面的代码替换即可
demo链接:
http://liuhanlin-work.qiniudn.com/qiniu-ios-demo.tar.gz