iOS 從iCloud中獲取文件

  • iCloud

使用UIDocumentPickerViewController

- (void)presentDocumentPicker {
    NSArray *documentTypes = @[@"public.content", @"public.text", @"public.source-code ", @"public.image", @"public.audiovisual-content", @"com.adobe.pdf", @"com.apple.keynote.key", @"com.microsoft.word.doc", @"com.microsoft.excel.xls", @"com.microsoft.powerpoint.ppt"];
    
    UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes
                                                                                                                          inMode:UIDocumentPickerModeOpen];
    documentPickerViewController.delegate = self;
    [self presentViewController:documentPickerViewController animated:YES completion:nil];
}

在UIDocumentPickerDelegate依次執(zhí)行样悟,獲取文件名锣夹,文件數(shù)據(jù),寫入沙盒Documents

#pragma mark - UIDocumentPickerDelegate

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
    
    NSArray *array = [[url absoluteString] componentsSeparatedByString:@"/"];
    NSString *fileName = [array lastObject];
    fileName = [fileName stringByRemovingPercentEncoding];
    
    if ([iCloudManager iCloudEnable]) {
        [iCloudManager downloadWithDocumentURL:url callBack:^(id obj) {
            NSData *data = obj;
            
            //寫入沙盒Documents
             NSString *path = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",fileName]];
            [data writeToFile:path atomically:YES];
        }];
    }
}

iCloudManager

#import <Foundation/Foundation.h>

typedef void(^downloadBlock)(id obj);

@interface iCloudManager : NSObject

+ (BOOL)iCloudEnable;

+ (void)downloadWithDocumentURL:(NSURL*)url callBack:(downloadBlock)block;

@end

判斷iCloud是否可用

+ (BOOL)iCloudEnable {
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSURL *url = [manager URLForUbiquityContainerIdentifier:nil];

    if (url != nil) {
        return YES;
    }
    
    NSLog(@"iCloud 不可用");
    return NO;
}

通過UIDocument子類ZZDocument墅垮,傳入url獲取數(shù)據(jù)

+ (void)downloadWithDocumentURL:(NSURL*)url callBack:(downloadBlock)block {
    
    ZZDocument *iCloudDoc = [[ZZDocument alloc]initWithFileURL:url];
    
    [iCloudDoc openWithCompletionHandler:^(BOOL success) {
        if (success) {
            
            [iCloudDoc closeWithCompletionHandler:^(BOOL success) {
                NSLog(@"關(guān)閉成功");
            }];
            
            if (block) {
                block(iCloudDoc.data);
            }
            
        }
    }];
}

ZZDocument

#import <UIKit/UIKit.h>

@interface ZZDocument : UIDocument
@property (nonatomic, strong) NSData *data;
@end

#import "ZZDocument.h"

@implementation ZZDocument

- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName error:(NSError * _Nullable __autoreleasing *)outError {
    self.data = contents;
    return YES;
}

@end

參考博客
http://www.cocoachina.com/ios/20160817/17382.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末民假,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子抓于,更是在濱河造成了極大的恐慌做粤,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件捉撮,死亡現(xiàn)場離奇詭異怕品,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)巾遭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門肉康,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人灼舍,你說我怎么就攤上這事吼和。” “怎么了骑素?”我有些...
    開封第一講書人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵炫乓,是天一觀的道長。 經(jīng)常有香客問我砂豌,道長厢岂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任阳距,我火速辦了婚禮塔粒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘筐摘。我一直安慰自己卒茬,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開白布咖熟。 她就那樣靜靜地躺著圃酵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪馍管。 梳的紋絲不亂的頭發(fā)上郭赐,一...
    開封第一講書人閱讀 51,488評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音确沸,去河邊找鬼捌锭。 笑死俘陷,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的观谦。 我是一名探鬼主播拉盾,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼豁状!你這毒婦竟也來了捉偏?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤泻红,失蹤者是張志新(化名)和其女友劉穎夭禽,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體承桥,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡驻粟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了凶异。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蜀撑。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖剩彬,靈堂內(nèi)的尸體忽然破棺而出酷麦,到底是詐尸還是另有隱情,我是刑警寧澤喉恋,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布沃饶,位于F島的核電站,受9級(jí)特大地震影響轻黑,放射性物質(zhì)發(fā)生泄漏糊肤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一氓鄙、第九天 我趴在偏房一處隱蔽的房頂上張望馆揉。 院中可真熱鬧,春花似錦抖拦、人聲如沸升酣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽噩茄。三九已至,卻和暖如春复颈,著一層夾襖步出監(jiān)牢的瞬間绩聘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留君纫,地道東北人驯遇。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親缤弦。 傳聞我的和親對象是個(gè)殘疾皇子罢维,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354