我正在使用这个方法
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
当我在应用程序中打开文件时,它会给出特定的文件 URL。该网址看起来像
file://localhost/Users/User/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/229C1D14-28D4-4B82-8CBB-8B9BC472E7A3/Documents/Inbox/Trinity_2016-4.pdf
我正在尝试将其从文件 url 转换为路径,以便我可以将它与开源库 SCRFTPRequest 一起使用。 .
问题是当我尝试指向一个文件时,我收到了 cocoa 错误 260,我只是想确保我的设置正确。
NSFileReadNoSuchFileError = 260, // Read error (no such file)
这是示例代码
SCRFTPRequest *ftpRequest = [[SCRFTPRequest alloc] initWithURL:[NSURL URLWithString:@"ftp://192.168.1.101/"]
toUploadFile:[[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"]];
这就是我改变它的方式
NSString *fileurl = @"file://Users/User....pdf";
NSString *path = [fileurl substringFromIndex:16];
SCRFTPRequest *ftpRequest = [[SCRFTPRequest alloc] initWithURL:[NSURL URLWithString:@"ftp://192.168.1.101/"]
toUploadFile:path];
这段代码的格式正确吗?正如所写,它使用 substringFromIndex 将文件 url 从 file://localhost/Users/User... 更改为/Users/User...
最佳答案
NSString *fileurl = @"file://Users/User....pdf";
NSString *path = [fileurl substringFromIndex:16];
您缺少正确的索引。此代码不会使 path
成为 /Users/User....pdf
,而是 ....pdf
。如果没有方法从 URL 中提取路径
NSString *path = [fileurl substringFromIndex:6];
就足够了。但是,由于我们有很棒的 NSURL 类,所以没有借口不写
NSURL *realURL = [NSURL URLWithString:fileurl];
NSString *path = [realURL path];
关于iphone - Cocoa错误260,文件路径结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12591097/