iOS开发实践iOS开发零散知识

系统分享功能分享txt、pdf文件到微信、QQ等及分享微信失败

2018-10-18  本文已影响325人  pengweinan

首先我们常用的友盟分享是无法分享txt、pdf文件的,但我们可以用系统自带分享功能去实现。系统分享主要有个实现思路UIActivityViewController,SLComposeViewController(https://www.jianshu.com/p/1b485f6e5e1a 这里面有详解)。其实就是SLComposeViewController可以定制分享目标app,但代码复杂,所以如果无此需求,建议用UIActivityViewController,如有错误,还望各位不吝纠正!如果喜欢或解决了您的问题,还请点赞支持~

以下是用的UIActivityViewController,主要实现方法:

//先定义了几个宏:

#define  KDomainPaths NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)

#define  KDocumentsDirectory [KDomainPaths objectAtIndex:0]
#define  K_TheFilePath [KDocumentsDirectory stringByAppendingPathComponent:@"tradelog.txt"]
#define  KFileManager [NSFileManager defaultManager]


- (void)shareLoghandler: (UIActivityViewControllerCompletionWithItemsHandler)handler{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString*documentsDirectory = [pathsobjectAtIndex:0];
    NSString*theFilePath = [documentsDirectory stringByAppendingPathComponent:@"tradelog.txt"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
//如果文件已存在,先删除
    if([fileManagerfileExistsAtPath:theFilePath] ) {
        [fileManager removeItemAtPath:theFilePath error:nil];
    }
//向txt文件写入内容
    if(![fileManagerfileExistsAtPath:theFilePath]) {
        NSString *str = @"i Love u , baby";
        NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
        [data writeToFile:theFilePath atomically:true];  //写入
    }
//向文件末尾添加内容.(写在需要写入的地方,此处写在这里了)
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:theFilePath];
        [fileHandleseekToEndOfFile];  //将节点跳到文件的末尾
        NSData* stringData  = [[NSString stringWithFormat:@"%@",@"一堆的乱码"] dataUsingEncoding:NSUTF8StringEncoding];
        [fileHandle writeData:stringData];//追加写入数据
        [fileHandlecloseFile];
    if ([KFileManager fileExistsAtPath:K_TheFilePath]) {
/**
****
注意:以下两行是分享的内容,urlToShare,data两个都要写。不然分享到微信时候为提示获取数据失败,无法分享的错误
****
*/
        NSURL *urlToShare = [NSURLfileURLWithPath:K_TheFilePath];
        NSData  *data = [NSData dataWithContentsOfFile:K_TheFilePath];
        UIActivityViewController *activity = [[UIActivityViewController alloc]initWithActivityItems:@[data,urlToShare] applicationActivities:nil];
//UIPopoverPresentationController是为了适配ipad,如无此需求可不写
            UIPopoverPresentationController *popover = activity.popoverPresentationController;
           if(popover) {
                popover.sourceView=self.view;
                popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
            }
        [self presentViewController:activity animated:YES completion:NULL];
        // 分享之后的回调
        activity.completionWithItemsHandler= ^(UIActivityType  _NullableactivityType,BOOLcompleted,NSArray*_NullablereturnedItems,NSError*_NullableactivityError) {
handler(activityType, completed, returnedItems, activityError);//返回分享类型等,如有需求,就做相应处理
            if(completed) {
                NSLog(@">>>>completed");
                //分享 成功
            }else  {
               NSLog(@">>>>>>cancled");
                //分享 取消
            }
        };
    }
}

注意:UIActivityViewController有个属性:excludedActivityTypes用于指定不需要提供的服务,这个属性是一个数组,代表的是,如果我们不需要系统的某些服务就可以给它赋值, 这样UIActivityViewController就不会显示对应的服务,系统提供了以下服务:

UIActivityTypePostToFacebook

UIActivityTypePostToTwitter

UIActivityTypePostToWeibo

UIActivityTypeMessage

UIActivityTypeMail

UIActivityTypePrint

UIActivityTypeCopyToPasteboard

UIActivityTypeAssignToContact

UIActivityTypeSaveToCameraRoll

UIActivityTypeAddToReadingList

UIActivityTypePostToVimeo

UIActivityTypePostToTencentWeibo

UIActivityTypeAirDrop

UIActivityTypeOpenInIBooks

UIActivityTypeMarkupAsPDF

eg:加入向屏蔽分享到微博,则:activity.excludedActivityTypes = UIActivityTypePostToWeibo

上一篇下一篇

猜你喜欢

热点阅读