Coder Social home page Coder Social logo

qiniu / objc-sdk Goto Github PK

View Code? Open in Web Editor NEW
436.0 24.0 197.0 2.72 MB

Qiniu Resource (Cloud) Storage Objective-C SDK for Mac/iOS

Home Page: http://developer.qiniu.com

License: MIT License

Ruby 0.36% Objective-C 99.18% Shell 0.27% C 0.04% Swift 0.16%

objc-sdk's Introduction

Qiniu Resource Storage SDK for Objective-C

@qiniu on weibo Software License Build Status Badge w/ Version GitHub release codecov Platform

安装

通过 CocoaPods

pod "Qiniu", "~> 8.7.1" 

通过 Swift Package Manager (Xcode 11+)

App 对接:
File -> Swift Packages -> Add Package Dependency,输入库链接,选择相应版本即可
库链接: https://github.com/qiniu/objc-sdk

库对接:
let package = Package(
    dependencies: [
        .package(url: "https://github.com/qiniu/objc-sdk", from: "8.7.1")
    ],
    // ...
)

运行环境

Qiniu SDK 版本 最低 iOS版本 最低 OS X 版本 Notes
8.7.x iOS 9 OS X 10.15 Xcode 最低版本 11
8.6.x iOS 7 OS X 10.15 Xcode 最低版本 11
8.5.x iOS 7 OS X 10.15 Xcode 最低版本 11
8.4.x iOS 7 OS X 10.15 Xcode 最低版本 11
8.3.x iOS 7 OS X 10.15 Xcode 最低版本 11
8.2.x iOS 7 OS X 10.15 Xcode 最低版本 11
8.1.x iOS 7 OS X 10.15 Xcode 最低版本 11
8.0.x iOS 7 OS X 10.15 Xcode 最低版本 11
7.5.x iOS 7 OS X 10.9 Xcode 最低版本 6.
7.4.x iOS 7 OS X 10.9 Xcode 最低版本 6.
7.3.x iOS 7 OS X 10.9 Xcode 最低版本 6.
7.2.x iOS 7 OS X 10.9 Xcode 最低版本 6.
7.1.x / AFNetworking-3.x iOS 7 OS X 10.9 Xcode 最低版本 6.
7.0.x / AFNetworking-2.x iOS 6 OS X 10.8 Xcode 最低版本 5.
7.x / AFNetworking-1.x iOS 5 OS X 10.7 Xcode 最低版本 5.
6.x iOS 6 None Xcode 最低版本 5.

使用方法

简单上传

#import <QiniuSDK.h>
...
    NSString *token = @"从服务端SDK获取";
    QNUploadManager *upManager = [[QNUploadManager alloc] init];
    NSData *data = [@"Hello, World!" dataUsingEncoding : NSUTF8StringEncoding];
    [upManager putData:data key:@"hello" token:token
        complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        NSLog(@"%@", info);
        NSLog(@"%@", resp);
    } option:[QNUploadOption defaultOptions]];
...

如使用最新版的sdk,默认自动判断上传空间。如需要指定上传区域,可以按如下方式上传:

#import <QiniuSDK.h>
...
    QNConfiguration *config = [QNConfiguration build:^(QNConfigurationBuilder *builder) {
        builder.useHttps = YES;// 是否使用https
        builder.zone = [[QNAutoZone alloc] init];// 根据 bucket 自动查询区域
        // builder.zone = [QNFixedZone createWithRegionId:@"z0"];// 指定华东区域
        // builder.zone = [QNFixedZone createWithRegionId:@"z1"];// 指定华北区域
        // builder.zone = [QNFixedZone createWithRegionId:@"z2"];// 指定华南区域
        // builder.zone = [QNFixedZone createWithRegionId:@"na0"];// 指定北美区域
        // builder.zone = [QNFixedZone createWithRegionId:@"as0"];// 指定东南亚区域
    }];
    
    QNUploadManager *upManager = [[QNUploadManager alloc] initWithConfiguration:config];
    QNUploadOption *option = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
        NSLog(@"progress %f", percent);
    }];
    
    NSData *data = [@"Hello, World!" dataUsingEncoding:NSUTF8StringEncoding];
    NSString *token = @"从服务端SDK获取";
    [upManager putData:data key:@"hello" token:token complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        NSLog(@"%@", info);
        NSLog(@"%@", resp);
    } option:option];
...

建议 QNUploadManager 创建一次重复使用, 或者使用单例方式创建.

测试

所有测试

$ xcodebuild test -workspace QiniuSDK.xcworkspace -scheme QiniuSDK_Mac -configuration Release -destination 'platform=macOS,arch=x86_64'

指定测试

可以在单元测试上修改, 熟悉 SDK

$ xcodebuild test -workspace QiniuSDK.xcworkspace -scheme QiniuSDK_Mac -configuration Release -destination 'platform=macOS,arch=x86_64' -only-testing:"QiniuSDK_MacTests/QNResumeUploadTest/test5M"

示例代码

  • 完整的demo 见 QiniuDemo 目录下的代码
  • 具体细节的一些配置 可参考 QiniuSDKTests 下面的一些单元测试,以及源代码

常见问题

  • 如果碰到 crc 链接错误, 请把 libz.dylib 加入到项目中去
  • 如果碰到 res_9_ninit 链接错误, 请把 libresolv.dylib 加入到项目中去
  • 如果需要支持 iOS 5 或者支持 RestKit, 请用 AFNetworking 1.x 分支的版本
  • 如果碰到其他编译错误, 请参考 CocoaPods 的 troubleshooting
  • iOS 9+ 强制使用https,需要在project build info 添加NSAppTransportSecurity类型Dictionary。在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES。 具体操作可参见 http://blog.csdn.net/guoer9973/article/details/48622823
  • 上传返回错误码理解,status code 注释

代码贡献

详情参考 代码提交指南.

贡献记录

联系我们

  • 如果需要帮助, 请提交工单 (在 portal 右侧点击咨询和建议提交工单, 或者直接向 [email protected] 发送邮件)
  • 如果有什么问题, 可以到问答社区提问, 问答社区
  • 更详细的文档, 见 官方文档站
  • 如果发现了 bug, 欢迎提交 issue
  • 如果有功能需求, 欢迎提交 issue
  • 如果要提交代码, 欢迎提交 pull request
  • 欢迎关注我们的 微信 && 微博, 及时获取动态信息

代码许可

The MIT License (MIT). 详情见 License 文件.

objc-sdk's People

Contributors

bachue avatar carlji avatar e06084 avatar greystar avatar huxia avatar jemygraw avatar kivenhaoyu avatar lintianzhi avatar longbai avatar lyyyuna avatar mei-zhao avatar nepliang avatar sutra avatar wangliangliang2 avatar wickedgenius avatar xiaopingsun avatar xwen-winnie avatar yangsen-qn avatar zheniantoushipashi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

objc-sdk's Issues

Swift 2.0上传文件回调问题

upManager.putData(ImageData, key: self.getRandomKey(), token: token, complete: { (info:QNResponseInfo!, key:String!, resp:[NSObject : AnyObject]!) -> Void in
//上传成功后resp 为空
}, option: QNUploadOption(mime: "text/plain", progressHandler: { (key:String!, percent:Float) -> Void in
}, params: nil, checkCrc: true, cancellationSignal: nil))

CocoaPods 里启用 “use_frameworks!” 后编译失败

Hi,

如题,我在 Podfile 里加上 “use_frameworks!” 后就会遇到下面的错误:

Undefined symbols for architecture armv7:
  "_crc32", referenced from:
      +[QNCrc32 data:] in QNCrc32.o
      +[QNCrc32 file:error:] in QNCrc32.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我使用的是 AFNetworking-1.x branch,有人遇到同样的错误或者知道解决方案吗?

上传文件403错误

QNResponseInfo.error

Error Domain=qiniu.com Code=403 "(null)" UserInfo={error=key doesn't match with scope}

iOS9 对https的要求导致上传失败

1.Description:
上传使用的是http地址,将会导致在iOS9上上传失败。

  1. Comments:
    并不想开启允许http链接的选项,添加exceptional domain后仍然失败。是否有什么workaround?
    Reference: http://stackoverflow.com/questions/30731785/how-do-i-load-an-http-url-with-app-transport-security-enabled-in-ios-9

以下为info.plist

NSAppTransportSecurity

NSExceptionDomains

upload.qiniu.com

NSIncludesSubdomains

NSTemporaryExceptionAllowsInsecureHTTPLoads




"

3.log
Log如下:
UserInfo={NSUnderlyingError=0x14feec9a0 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLStringKey=http://101.71.78.240:80/, NSErrorFailingURLKey=http://101.71.78.240:80/, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}>

我用的是Pod拉回来的AFNetworking 2.6.0,上传的时候报了这个错,怎么都上传不了

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -streamStatus only defined for abstract class. Define -[AFMultipartBodyStream streamStatus]!'
*** First throw call stack:
(0x181d182d8 0x1939ec0e4 0x181d18218 0x182c07f70 0x182c13a6c 0x1817088ac 0x181714ba8 0x181713d3c 0x181717670 0x18165373c 0x181bf8cdc 0x1816535e8 0x18165349c 0x1816532c8 0x181cd0240 0x181ccf4e4 0x181ccd594 0x181bf92d4 0x1816d6594 0x182c19db8 0x19424bdc8 0x19424bd24 0x194248ef8)

QNResponseInfo.m init 函数里如果 response body 非json格式 uInfo 为nil 需要检查处理

Objc 修改仅一行: (注释)
QNResponseInfo.m

  • (instancetype)init:(int)status
    withReqId:(NSString *)reqId
    withXLog:(NSString *)xlog
    withBody:(NSData *)body {

if (self = [super init]) {
_statusCode = status;
_reqId = [reqId copy];
_xlog = [xlog copy];
if (status != 200) {
if (body == nil) {
_error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:nil];
} else {
NSError *tmp;
NSDictionary *uInfo = [NSJSONSerialization JSONObjectWithData:body options:NSJSONReadingMutableLeaves error:&tmp];

        /* 这段需要做 uInfo nil 判断, 否则代码执行会蹦 */
        if (tmp != nil && uInfo != nil) {
            uInfo = @{ @"error":[[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding] };
        }
        _error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:uInfo];
    }
}
else if (body == nil || body.length == 0) {
    NSDictionary *uInfo = @{ @"error":@"no response json" };
    _error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:uInfo];
}

}
return self;
}

请把QNStats.count类型设成NSUInteger

因为你们这个QNStats.count类型定了int,导致很多其他第三方库有使用@selector(count)的都报错了!!!

错误原因:multiple methods named count found with mismatched result

id arr = @[];    
[arr performSelector:@selector(count)];

请把改一下类型为@property NSUInteger count;

编译出错

Undefined symbols for architecture armv7:
"_kQNInvalidArgument", referenced from:
-[QNFormUploadTest testNoData] in QNFormUploadTest.o
-[QNFormUploadTest testNoFile] in QNFormUploadTest.o
-[QNFormUploadTest testNoToken] in QNFormUploadTest.o
"_kQNRequestCancelled", referenced from:
-[QNFileRecorderTest template:pos:] in QNFileRecorderTest.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

上传可能出现crash

在QNResponseInfo.m里

            NSError *tmp;
                NSDictionary *uInfo = [NSJSONSerialization JSONObjectWithData:body options:NSJSONReadingMutableLeaves error:&tmp];
                if (tmp != nil) {
                    uInfo = @{ @"error":[[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding] };
                }

可能会出现crash, [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]有可能为nil

内存泄漏

目前一个项目,需要连续上传1M~3M的图片若干张。经测试发现,如果使用七牛SDK上传,每上传一张图片后,内存会增加1M~2M,导致上传20张图片后内存增加20M~40M。将上传方法改为 AFNetWorking的 上传方法,内存持续增加的问题消失。
项目中使用的七牛上传代码如下:
QNUploadOption * option = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
if (progressBlcok) {
progressBlcok(percent);
}
}];

QNUploadManager * manager = [QNUploadManager sharedInstanceWithConfiguration:nil];
__weak typeof(self) wself = self;
[manager putData:imageData key:nil token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
    NSError * error = info.error;
    NSString * imageKey = resp[@"key"];
    if (wself.completedBlock) {
        wself.completedBlock(imageKey,error);
    }
    wself.completedBlock = nil;
    [wself done];
} option:option]; 

使用AFNetworking的上传方法代码如下:
AFHTTPRequestOperationManager *operationManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://upload.qiniu.com"]];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[@"token"] = token;
NSMutableURLRequest *request = [operationManager.requestSerializer
multipartFormRequestWithMethod:@"POST"
URLString:@"http://upload.qiniu.com"
parameters:parameters
constructingBodyWithBlock:^(id formData) {
[formData appendPartWithFileData:imageData name:@"file" fileName:@"file" mimeType:@"image/jpeg"];
} error:nil];
__weak typeof(self) wself = self;
AFHTTPRequestOperation *operation = [operationManager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {

                                                                                  NSString * imageKey = responseObject[@"key"];
                                                                                  if (wself.completedBlock) {
                                                                                      wself.completedBlock(imageKey,nil);
                                                                                  }
                                                                                  wself.completedBlock = nil;
                                                                                  [wself done];

                                                                              } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                                                                  if (wself.completedBlock) {
                                                                                      wself.completedBlock(nil,error);
                                                                                  }
                                                                                  wself.completedBlock = nil;
                                                                                  [wself done];
                                                                              }];

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    if (progressBlcok) {
        double percent = totalBytesWritten * 1.0 / totalBytesExpectedToWrite;
        progressBlcok(percent);
    }
}];

[self.queue addOperation:operation];

pod安装出来是Qiniu 7.0.16+AFNetworking 2.6.3

不是应该支持AFNetworking 3.0以上吗?
platform :ios, '7.0'
target 'QiNiuDemo' do
pod 'AFNetworking'
pod 'Qiniu'
end

安装成功后显示

ownloading dependencies
Installing AFNetworking 2.6.3 (was 3.0.4)
Installing HappyDNS (0.2.3)
Installing Qiniu 7.0.16 (was 6.3.3.1)
Generating Pods project
Integrating client project
Sending stats

Swift下出现连接错误

环境:

  • iOS SDK8.2
  • Swift
  • Xcode 6.2
  • pod 'Qiniu', '~> 7.0'

错误信息:

Undefined symbols for architecture arm64:
  "_OBJC_CLASS_$_QNResponseInfo", referenced from:
      __TMaCSo14QNResponseInfo in ImageComposeViewController.o
  "_OBJC_CLASS_$_QNUploadOption", referenced from:
      __TMaCSo14QNUploadOption in ImageComposeViewController.o
  "_OBJC_CLASS_$_QNUploadManager", referenced from:
      __TMaCSo15QNUploadManager in ImageComposeViewController.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

应该是没有支持64bit

Upgrade to 7.0.14 occur duplicate symbols for architecture x86_64

duplicate symbol OBJC_CLASS$_Reachability in:
/Users/lilo/Library/Developer/Xcode/DerivedData/YDDApp-eumrpyxnudorchahpayajtodfrfp/Build/Intermediates/YDDApp.build/Debug-iphonesimulator/YDDApp.build/Objects-normal/x86_64/Reachability.o
/Users/lilo/Library/Developer/Xcode/DerivedData/YDDApp-eumrpyxnudorchahpayajtodfrfp/Build/Products/Debug-iphonesimulator/libQiniu.a(Reachability.o)
duplicate symbol OBJC_METACLASS$_Reachability in:
/Users/lilo/Library/Developer/Xcode/DerivedData/YDDApp-eumrpyxnudorchahpayajtodfrfp/Build/Intermediates/YDDApp.build/Debug-iphonesimulator/YDDApp.build/Objects-normal/x86_64/Reachability.o
/Users/lilo/Library/Developer/Xcode/DerivedData/YDDApp-eumrpyxnudorchahpayajtodfrfp/Build/Products/Debug-iphonesimulator/libQiniu.a(Reachability.o)
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

You may include some redundant libraries like Reachability etc into your staic library.

swift2 编译错误

前几天pod安装的qiniusdk,今天早上pod udpate 发现程序无法运行了,QNStatus.h报错无法找到"AFNetworking.h" 刚才看了下git的更新时间是19小时前,是不是这次更新对qiniusdk产生了影响导致了无法编译,还是什么原因导致的无法运行?
snip20151110_1

sdk demo

官网的sdk使用指南说得很不清楚,那代码中是否能够添加一个简易的使用demo?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.