iOS 共享到Box

iOS 共享爬坑记

和Dropbox类似,一样是在最开始的时候,直接一脸懵逼,注册App还需要收费的那种?好在最终还是找到了正确的位置,创建了App,免费的那种。

OK,话不多说,直接开始。


info配置.png

pod 导入,应该没有记错swift导入

pod 'box-ios-sdk' #OC
#pod 'BoxSDK', '~> 3.0' #swift

注册

[BOXContentClient setClientID:@"svgdg6ekzvdhr2za0gqi9r78st54pkjh" clientSecret:@"KjjMQHPEvuqvCWEtFhrn5q9TE2WZXWUM"];

在delegate文件的openURL方法中:

/// box
    if ([@"boxsdk-svgdg6ekzvdhr2za0gqi9r78st54pkjh" isEqualToString:url.scheme]) {
        return YES;
    }

代码开始

- (void)boxAction {
    [self boxAuthor];
}

授权

// 授权
- (void)boxAuthor {
    BOXContentClient *client = [BOXContentClient defaultClient];
    [client authenticateWithCompletionBlock:^(BOXUser *user, NSError *error) {
        if (error == nil) {
            [self boxFileUploadWithFilePath:[[NSBundle mainBundle] pathForResource:@"4" ofType:@"pdf"]];
        }else {
            NSLog(@"授权失败");
        }
    }];
}

上传文件

- (void)boxFileUploadWithFilePath:(NSString *)filePath {
    BOXContentClient *contentClient = [BOXContentClient defaultClient];
    BOXFileUploadRequest *uploadRequest = [contentClient fileUploadRequestToFolderWithID:BOXAPIFolderIDRoot fromLocalFilePath:filePath];

    // Optional: By default the name of the file on the local filesystem will be used as the name on Box. However, you can
    // set a different name for the file by configuring the request:
    uploadRequest.fileName = @"filena.pdf";

    [uploadRequest performRequestWithProgress:^(long long totalBytesTransferred, long long totalBytesExpectedToTransfer) {
        NSLog(@"upload -- %.2f",1.0*totalBytesTransferred/totalBytesExpectedToTransfer);
    } completion:^(BOXFile *file, NSError *error) {
        NSLog(@"%@",error);
        NSLog(@"%@",file);
    }];
}

帮助链接:
box官方帮助文档
box开发者帮助文档

你可能感兴趣的:(iOS 共享到Box)