最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS文件預(yù)覽分享小技能示例

 更新時(shí)間:2022年08月16日 11:36:01   作者:公眾號iOS逆向  
這篇文章主要為大家介紹了iOS文件預(yù)覽分享小技能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言

應(yīng)用場景:文件下載、打印

I 第三方SDK分享文件

1.1 微信SDK

/**
enum WXScene {
    WXSceneSession  = 0, 
    WXSceneTimeline = 1,
    WXSceneFavorite = 2,
};
文件真實(shí)數(shù)據(jù)內(nèi)容
 * @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 多媒體消息中包含的文件數(shù)據(jù)對象
 *
 * @see UMShareObject
 */
@interface UMShareFileObject : UMShareObject
/** 文件后綴名
 * @note 長度不超過64字節(jié)
 */
@property (nonatomic, retain) NSString  *fileExtension;
/** 文件真實(shí)數(shù)據(jù)內(nèi)容
 * @note 大小不能超過10M
 */
@property (nonatomic, retain) NSData    *fileData;
/** 文件的名字(不包含后綴)
 * @note 長度不超過64字節(jié)
 */
@property (nonatomic, retain) NSString  *fileName;
@end

II 原生API的文件預(yù)覽及其他應(yīng)用打開

- (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;//預(yù)覽文件
// 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;//包括快速預(yù)覽菜單、打印、復(fù)制
- (BOOL)presentOpenInMenuFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;//不包括包括快速預(yù)覽菜單
  • 獲取NSURL
//方式1:
    NSString* filePath = [[NSBundle mainBundle] pathForResource:@"ML" ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
// 方式2
    //NSURL *url = [[NSBundle mainBundle] URLForResource:@"ML" withExtension:@"pdf"];
  • 實(shí)例化UIDocumentInteractionController
UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
documentController.delegate = self;//UIDocumentInteractionControllerDelegate

2.1 預(yù)覽文件

[documentController presentPreviewAnimated:YES]; // 預(yù)覽文件

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];//包括快速預(yù)覽菜單、打印、復(fù)制
//        [documentController presentOpenInMenuFromRect:rect inView:self.view animated:YES];//不包括包括快速預(yù)覽菜單

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)  //當(dāng)前選擇器的名字
//    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 文件下載和預(yù)覽

- (void)openfile:(CRMfilePreviewCellM*)m{
    //        NSURL *relativeToURL = [NSURL URLWithString:m.url ];//必須先下載,否則無法查看文件內(nèi)容
    [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]; // 預(yù)覽文件
    }
}
#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)  //當(dāng)前選擇器的名字
//    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 使用數(shù)據(jù)模型保存下載文件路徑

懶加載

    //        NSURL *relativeToURL = [NSURL URLWithString:m.url ];//必須先下載,否則無法查看文件內(nèi)容
- (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;
}

預(yù)覽文件

- (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]; // 預(yù)覽文件
}

3.3 使用數(shù)據(jù)模型分享文件

@property (nonatomic,copy) NSString *fileName;
@property (nonatomic,copy) NSString *url;
//
@property (nonatomic,copy) NSString *filePathFromUrl;
/**
/** 文件真實(shí)數(shù)據(jù)內(nèi)容
 * @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文件預(yù)覽分享小技能示例的詳細(xì)內(nèi)容,更多關(guān)于iOS文件預(yù)覽分享的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 詳解iOS App設(shè)計(jì)模式開發(fā)中對于享元模式的運(yùn)用

    詳解iOS App設(shè)計(jì)模式開發(fā)中對于享元模式的運(yùn)用

    這篇文章主要介紹了iOS App設(shè)計(jì)模式開發(fā)中對于享元模式的運(yùn)用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-04-04
  • 淺談iOS中的鎖的介紹及使用

    淺談iOS中的鎖的介紹及使用

    本篇文章主要介紹了淺談iOS中的鎖的介紹及使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • 全面解析Objective-C中的block代碼塊的使用

    全面解析Objective-C中的block代碼塊的使用

    這篇文章主要介紹了Objective-C中的block代碼塊的使用,包括閉包等重要特性的講解,需要的朋友可以參考下
    2015-11-11
  • iOS常用組件之高效切圓角的方法匯總

    iOS常用組件之高效切圓角的方法匯總

    最近在研究切圓角的方法,也找了下網(wǎng)上的資料,所以下面這篇文章主要給大家總結(jié)介紹了關(guān)于iOS常用組件之高效切圓角的一些方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • iOS利用Label實(shí)現(xiàn)的簡單高性能標(biāo)簽TagView

    iOS利用Label實(shí)現(xiàn)的簡單高性能標(biāo)簽TagView

    這篇文章主要給大家介紹了關(guān)于iOS利用Label實(shí)現(xiàn)的簡單高性能標(biāo)簽TagView的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • IOS UI學(xué)習(xí)教程之使用UIImageView控件制作動畫

    IOS UI學(xué)習(xí)教程之使用UIImageView控件制作動畫

    這篇文章主要為大家詳細(xì)介紹了IOS UI學(xué)習(xí)教程之使用UIImageView控件制作動畫,感興趣的小伙伴們可以參考一下
    2016-03-03
  • iOS應(yīng)用中UITableView左滑自定義選項(xiàng)及批量刪除的實(shí)現(xiàn)

    iOS應(yīng)用中UITableView左滑自定義選項(xiàng)及批量刪除的實(shí)現(xiàn)

    這篇文章主要介紹了iOS應(yīng)用中UITableView左滑自定義選項(xiàng)及批量刪除的實(shí)現(xiàn),UITableView列表中即通訊錄左滑呼出選項(xiàng)的那種效果在刪除時(shí)能夠?qū)崿F(xiàn)多行刪除將更加方便,需要的朋友可以參考下
    2016-03-03
  • 清除WKWebView cookies的方法

    清除WKWebView cookies的方法

    下面小編就為大家?guī)硪黄宄齏KWebView cookies的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • 詳解iOS11關(guān)于導(dǎo)航欄問題

    詳解iOS11關(guān)于導(dǎo)航欄問題

    本篇文章主要介紹了詳解iOS11關(guān)于導(dǎo)航欄問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • 詳解Xcode編譯選項(xiàng)功能

    詳解Xcode編譯選項(xiàng)功能

    本篇文章詳細(xì)分析了一下Xcode編譯選項(xiàng)功能以及相關(guān)的介紹,有這方面需要的參考學(xué)下下。
    2018-01-01

最新評論

洛宁县| 时尚| 子洲县| 盘锦市| 乐平市| 宁津县| 当阳市| 登封市| 冷水江市| 彭阳县| 敖汉旗| 文安县| 嘉义市| 金昌市| 南木林县| 龙海市| 固镇县| 东平县| 信丰县| 灵石县| 嵩明县| 宁德市| 太保市| 获嘉县| 宜兰县| 永泰县| 清涧县| 临沂市| 西乡县| 凤台县| 永德县| 松滋市| 客服| 尼木县| 杨浦区| 始兴县| 岫岩| 玉田县| 夏邑县| 大连市| 华容县|