<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
應用場景:檔案下載、列印
/** enum WXScene { WXSceneSession = 0, WXSceneTimeline = 1, WXSceneFavorite = 2, }; 檔案真實資料內容 * @note 大小不能超過10M */ @property (nonatomic, retain) NSData *fileData; */ - (void)sendFileContent { WXMediaMessage *message = [WXMediaMessage message]; message.title = @"ML.pdf"; message.description = @"Pro CoreData"; [message setThumbImage:[UIImage imageNamed:@"res2.jpg"]]; WXFileObject *ext = [WXFileObject object]; ext.fileExtension = @"pdf"; NSString* filePath = [[NSBundle mainBundle] pathForResource:@"ML" ofType:@"pdf"]; ext.fileData = [NSData dataWithContentsOfFile:filePath]; //+ (nullable instancetype)dataWithContentsOfURL:(NSURL *)url; message.mediaObject = ext; SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease]; req.bText = NO; req.message = message; req.scene = WXSceneSession; [WXApi sendReq:req completion:nil]; }
#pragma mark - UMFileObject /*! @brief 多媒體訊息中包含的檔案資料物件 * * @see UMShareObject */ @interface UMShareFileObject : UMShareObject /** 檔案字尾名 * @note 長度不超過64位元組 */ @property (nonatomic, retain) NSString *fileExtension; /** 檔案真實資料內容 * @note 大小不能超過10M */ @property (nonatomic, retain) NSData *fileData; /** 檔案的名字(不包含字尾) * @note 長度不超過64位元組 */ @property (nonatomic, retain) NSString *fileName; @end
- (BOOL)presentOptionsMenuFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated; - (BOOL)presentOptionsMenuFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated; // Bypasses the menu and opens the full screen preview window for the item at URL. Returns NO if the item could not be previewed. // Note that you must implement the delegate method documentInteractionControllerViewControllerForPreview: to preview the document. - (BOOL)presentPreviewAnimated:(BOOL)animated;//預覽檔案 // Presents a menu allowing the user to open the document in another application. The menu // will contain all applications that can open the item at URL. // Returns NO if there are no applications that can open the item at URL. - (BOOL)presentOpenInMenuFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated;//包括快速預覽選單、列印、複製 - (BOOL)presentOpenInMenuFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;//不包括包括快速預覽選單
//方式1: NSString* filePath = [[NSBundle mainBundle] pathForResource:@"ML" ofType:@"pdf"]; NSURL *url = [NSURL fileURLWithPath:filePath]; // 方式2 //NSURL *url = [[NSBundle mainBundle] URLForResource:@"ML" withExtension:@"pdf"];
UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:url]; documentController.delegate = self;//UIDocumentInteractionControllerDelegate
[documentController presentPreviewAnimated:YES]; // 預覽檔案
CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); [documentController presentOptionsMenuFromRect:rect inView:self.view animated:YES];//包括快速預覽選單、列印、複製 // [documentController presentOpenInMenuFromRect:rect inView:self.view animated:YES];//不包括包括快速預覽選單
#pragma mark - UIDocumentInteractionControllerDelegate - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)interactionController{ return self; } // /** print: saveToCameraRoll: copy: */ - (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(SEL)action{ NSLog(@"canPerformAction %s %@ ", __func__,NSStringFromSelector(action)); //NSStringFromSelector(_cmd) //當前選擇器的名字 // return NO;不顯示copy print return YES;//顯示copy print } - (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller performAction:(SEL)action{ NSLog(@"canPerformAction %s", __func__); return YES;//顯示copy print // return NO; }
- (void)openfile:(CRMfilePreviewCellM*)m{ // NSURL *relativeToURL = [NSURL URLWithString:m.url ];//必須先下載,否則無法檢視檔案內容 [SVProgressHUD showWithStatus:@"載入中..."]; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:m.url]]; [SVProgressHUD dismiss]; if(data== nil){ [SVProgressHUD showInfoWithStatus:@"檔案下載失敗"]; return ; } // //用單例類 NSFileManager的物件,將檔案寫入本地 NSFileManager *fileManage = [NSFileManager defaultManager]; NSString *tmp = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; // NSString *tmp = NSTemporaryDirectory(); NSString *fileName = m.fileName; tmp =[tmp stringByAppendingPathComponent:fileName]; BOOL isSuccess = [fileManage createFileAtPath:tmp contents:data attributes:nil]; if(isSuccess){ NSURL *url = [NSURL fileURLWithPath:tmp]; UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:url]; //UIDocumentInteractionController delegate must implement documentInteractionControllerViewControllerForPreview: to allow preview documentController.delegate = self;//UIDocumentInteractionControllerDelegate [documentController presentPreviewAnimated:YES]; // 預覽檔案 } } #pragma mark - UIDocumentInteractionControllerDelegate - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)interactionController{ return self; } // /** print: saveToCameraRoll: copy: */ - (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(SEL)action{ NSLog(@"canPerformAction %s %@ ", __func__,NSStringFromSelector(action)); //NSStringFromSelector(_cmd) //當前選擇器的名字 // return NO;不顯示copy print return YES;//顯示copy print } - (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller performAction:(SEL)action{ NSLog(@"canPerformAction %s", __func__); return YES;//顯示copy print // return NO; }
懶載入
// NSURL *relativeToURL = [NSURL URLWithString:m.url ];//必須先下載,否則無法檢視檔案內容 - (NSString *)filePathFromUrl{ if(_filePathFromUrl !=nil){ return _filePathFromUrl; } NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.url]]; if(data== nil){ [SVProgressHUD showInfoWithStatus:@"檔案下載失敗"]; return nil; } // //用單例類 NSFileManager的物件,將檔案寫入本地 NSFileManager *fileManage = [NSFileManager defaultManager]; NSString *tmp = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; // NSString *tmp = NSTemporaryDirectory(); NSString *fileName = self.fileName; tmp =[tmp stringByAppendingPathComponent:fileName]; BOOL isSuccess = [fileManage createFileAtPath:tmp contents:data attributes:nil]; _filePathFromUrl = tmp; if(!isSuccess){ _filePathFromUrl = nil; } return _filePathFromUrl; }
預覽檔案
- (void)openfile:(CRMfilePreviewCellM*)m{ if(!m.filePathFromUrl){ return; } NSURL *url = [NSURL fileURLWithPath:m.filePathFromUrl]; UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:url]; //UIDocumentInteractionController delegate must implement documentInteractionControllerViewControllerForPreview: to allow preview documentController.delegate = self;//UIDocumentInteractionControllerDelegate [documentController presentPreviewAnimated:YES]; // 預覽檔案 }
@property (nonatomic,copy) NSString *fileName; @property (nonatomic,copy) NSString *url; // @property (nonatomic,copy) NSString *filePathFromUrl; /** /** 檔案真實資料內容 * @note微信檔案分享 大小不能超過10M */ @property (nonatomic, retain) NSData *fileData; - (void)sendFileContent; - (NSData *)fileData{ if(_fileData==nil){ NSString* filePath= [self filePathFromUrl]; _fileData =[NSData dataWithContentsOfFile:filePath]; } return _fileData; } - (void)sendFileContent { WXMediaMessage *message = [WXMediaMessage message]; message.title = self.fileName; message.description =self.fileName; [message setThumbImage:[UIImage imageNamed:self.iconName]]; WXFileObject *ext = [WXFileObject object]; ext.fileExtension =self.fileExtension; ext.fileData =self.fileData; //+ (nullable instancetype)dataWithContentsOfURL:(NSURL *)url; message.mediaObject = ext; SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init]; req.bText = NO; req.message = message; req.scene = WXSceneSession; [WXApi sendReq:req completion:nil]; }
獲取沙盒快取路徑
+ (nullable NSString *)userCacheDirectory { NSArray<NSString *> *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); return paths.firstObject; }
清理沙河檔案快取
- (void)removeAllData { [self.fileManager removeItemAtPath:self.diskCachePath error:nil]; [self.fileManager createDirectoryAtPath:self.diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL]; }
清理WKWebView的快取
+ (void)clearWebCacheCompletion:(dispatch_block_t)completion { if (@available(iOS 9.0, *)) { NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes]; NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0]; [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:completion]; } else { NSString *libraryDir = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0]; NSString *bundleId = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]; NSString *webkitFolderInLib = [NSString stringWithFormat:@"%@/WebKit",libraryDir]; NSString *webKitFolderInCaches = [NSString stringWithFormat:@"%@/Caches/%@/WebKit",libraryDir,bundleId]; NSString *webKitFolderInCachesfs = [NSString stringWithFormat:@"%@/Caches/%@/fsCachedData",libraryDir,bundleId]; NSError *error; /* iOS8.0 WebView Cache path */ [[NSFileManager defaultManager] removeItemAtPath:webKitFolderInCaches error:&error]; [[NSFileManager defaultManager] removeItemAtPath:webkitFolderInLib error:nil]; /* iOS7.0 WebView Cache path */ [[NSFileManager defaultManager] removeItemAtPath:webKitFolderInCachesfs error:&error]; if (completion) { completion(); } } }
清理圖片快取
+(void)clearCache:(NSString *)path{ NSFileManager *fileManager=[NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:path]) { NSArray *childerFiles=[fileManager subpathsAtPath:path]; for (NSString *fileName in childerFiles) { //如有需要,加入條件,過濾掉不想刪除的檔案 NSString *absolutePath=[path stringByAppendingPathComponent:fileName]; [fileManager removeItemAtPath:absolutePath error:nil]; } } // [[SDImageCache sharedImageCache] cleanDisk]; [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{ }]; }
以上就是iOS檔案預覽分享小技能範例的詳細內容,更多關於iOS檔案預覽分享的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45