2018年3月6日
app共享屬性打開后吕朵,用itunes打開,會在文件共享菜單下面顯示衅澈。
image.png
一.實現(xiàn):ios端app由于采用的是沙盒機制键菱,默認是不支持訪問程序外的文件(聯(lián)系方式和相冊除外,這些訪問應(yīng)該是蘋果底層框架實現(xiàn)的)
1.如果需要實現(xiàn)文件共享今布,app端要開啟itunes文件共享配置image.png
2.相關(guān)測樣代碼
//讀取沙盒文件
- (IBAction)readFileInDocumentDir:(id)sender {
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:docsDir];
NSString *fileName;
while (fileName = [dirEnum nextObject]) {
NSLog(@"fielName : %@" , fileName);
NSLog(@"filePath : %@" , [docsDir stringByAppendingPathComponent:fileName]) ;
}
}
//沙盒路徑下創(chuàng)建文件经备,便于從itunes軟件里面導(dǎo)出
- (IBAction)createImgFileToDoucument:(id)sender {
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *imageFilePath = [docsDir stringByAppendingPathComponent:@"testImage.png"] ;
UIImage *image = [self generateImage];
[[NSFileManager defaultManager] createFileAtPath:imageFilePath contents:UIImagePNGRepresentation(image) attributes:nil] ;
}
// 創(chuàng)建簡單圖片
- (UIImage*)generateImage{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 200), NO, 0.0) ;
CGContextRef ctx = UIGraphicsGetCurrentContext() ;
CGRect imageRect = CGRectMake(0.0, 0.0, 320, 200) ;
[[UIColor redColor] setFill] ;
CGContextFillRect(ctx, imageRect) ;
UIImage *image = UIGraphicsGetImageFromCurrentImageContext() ;
UIGraphicsEndImageContext() ;
return image ;
}
二、驗證
1.第一步,用itunes打開部默,拖入一張圖片到該app
image.png
2.點擊app內(nèi)讀取文件按鈕侵蒙,看是否有日志輸出
image.png
3.點擊創(chuàng)建文件后,在用itunes工具將剛剛創(chuàng)建的文件拖出即可傅蹂。
image.png
image.png
如果您發(fā)現(xiàn)本文對你有所幫助纷闺,如果您認為其他人也可能受益,請把它分享出去份蝴。