前言
应用场景:文件下载、打印
i 第三方sdk分享文件
1.1 微信sdk
/** 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]; }
1.2 友盟sdk
#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
ii 原生api的文件预览及其他应用打开
- (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;//不包括包括快速预览菜单
- 获取nsurl
//方式1: nsstring* filepath = [[nsbundle mainbundle] pathforresource:@"ml" oftype:@"pdf"]; nsurl *url = [nsurl fileurlwithpath:filepath]; // 方式2 //nsurl *url = [[nsbundle mainbundle] urlforresource:@"ml" withextension:@"pdf"];
- 实例化uidocumentinteractioncontroller
uidocumentinteractioncontroller *documentcontroller = [uidocumentinteractioncontroller interactioncontrollerwithurl:url]; documentcontroller.delegate = self;//uidocumentinteractioncontrollerdelegate
2.1 预览文件
[documentcontroller presentpreviewanimated:yes]; // 预览文件
2.2 文件分享
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];//不包括包括快速预览菜单
2.3 控制是否显示copy、 print、savetocameraroll
#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; }
iii 案例
3.1 文件下载和预览
- (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; }
3.2 使用数据模型保存下载文件路径
懒加载
// 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]; // 预览文件 }
3.3 使用数据模型分享文件
@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]; }
3.4 清理缓存
获取沙盒缓存路径
+ (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文件预览分享的资料请关注代码网其它相关文章!
发表评论