ios怎么打开c语言文件夹,iOS中打开的文件如何用其他应用打开选择自己的app

效果如图:

a950b411a8e7

用其他应用打开

a950b411a8e7

选择某一应用

1、设置 Info.plist

CFBundleDocumentTypes

CFBundleTypeIconFiles

96.png

96@2x.png

CFBundleTypeName

com.myapp.common-data

LSItemContentTypes

com.microsoft.powerpoint.ppt

public.item

com.microsoft.word.doc

com.adobe.pdf

com.microsoft.excel.xls

public.image

public.content

public.composite-content

public.archive

public.audio

public.movie

public.text

public.data

2、设置 AppDelegate

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

if (self.window) {

if (url) {

NSString *fileName = url.lastPathComponent; // 从路径中获得完整的文件名(带后缀)

// path 类似这种格式:file:///private/var/mobile/Containers/Data/Application/83643509-E90E-40A6-92EA-47A44B40CBBF/Documents/Inbox/jfkdfj123a.pdf

NSString *path = url.absoluteString; // 完整的url字符串

path = [self URLDecodedString:path]; // 解决url编码问题

NSMutableString *string = [[NSMutableString alloc] initWithString:path];

if ([path hasPrefix:@"file://"]) { // 通过前缀来判断是文件

// 去除前缀:/private/var/mobile/Containers/Data/Application/83643509-E90E-40A6-92EA-47A44B40CBBF/Documents/Inbox/jfkdfj123a.pdf

[string replaceOccurrencesOfString:@"file://" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, path.length)];

// 此时获取到文件存储在本地的路径,就可以在自己需要使用的页面使用了

NSDictionary *dict = @{@"fileName":fileName,

@"filePath":string};

[[NSNotificationCenter defaultCenter] postNotificationName:@"FileNotification" object:nil userInfo:dict];

return YES;

}

}

}

return YES;

}

// 当文件名为中文时,解决url编码问题

- (NSString *)URLDecodedString:(NSString *)str {

NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));

DLog(@"decodedString = %@",decodedString);

return decodedString;

}

3、接收通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileNotification:) name:@"FileNotification" object:nil];

- (void)fileNotification:(NSNotification *)notifcation {

NSDictionary *info = notifcation.userInfo;

// fileName是文件名称、filePath是文件存储在本地的路径

// jfkdfj123a.pdf

NSString *fileName = [info objectForKey:@"fileName"];

// /private/var/mobile/Containers/Data/Application/83643509-E90E-40A6-92EA-47A44B40CBBF/Documents/Inbox/jfkdfj123a.pdf

NSString *filePath = [info objectForKey:@"filePath"];

NSLog(@"fileName=%@---filePath=%@", fileName, filePath);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值