Objective-C dataWithContentsOfURL返回nill [英] Objective-C dataWithContentsOfURL returns nill

查看:287
本文介绍了Objective-C dataWithContentsOfURL返回nill的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有这个代码:

NSString *stringURL = @"\\\\SERVER\\FOLDER\\FOLDER\\FTP\\ANC\\ANC.pdf";    
NSURL  *url = [NSURL fileURLWithPath:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];

但urlData返回nill

but urlData returned nill

我有试过datawithcontentsoffile但是在使用url变量时我收到了警告。

I have tried datawithcontentsoffile but I got a warning when using the url variable with it.

当我转到这个url文件时://SERVER/FOLDER/FOLDER/FTP/ANC.pdf在Windows中,它打开文件,但在mac上它不会。

When I goto this url file://SERVER/FOLDER/FOLDER/FTP/ANC.pdf in Windows, it opens the file, but on mac it does not not.

我也尝试了以下内容:

NSString *stringURL = @"file://SERVER/FOLDER/FOLDER/FTP/ANC.pdf"; 
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:stringURL]
            completionHandler:^(NSData *data,
                                NSURLResponse *response,
                                NSError *error) {
                // handle response

            }] resume];

// NSURL  *url = [NSURL URLWithString:stringURL];  
// NSData *urlData = [NSData dataWithContentsOfURL:url];

但是会收到以下错误:

NSErrorFallingURLStringKey
NSErrorFallingURLKey
NSLocalizedDescription
NSUnderlyingError

更新我能够通过以下内容获取网址:

UPDATE I am able to get url not return nill with the following:

NSURL  *url = [NSURL fileURLWithPath:@"file:///server/FOLDER/FOLDER/FTP/ANC/ANC.pdf"];

然而,这会返回nill:

however, this returns nill:

NSData *urlData = [NSData dataWithContentsOfURL:url];

我添加了错误:to dataWithContentsofURL

I added the error: to dataWithContentsofURL

它返回了这个:

connectionError NSError *   domain: @"NSCocoaErrorDomain" - code: 260   0x166d8af0

我通过Get Info查看了有问题的文件,它开始像这样 smb:// server /folder/folder/ANC/ANC.pdf

I looked at the file in question via Get Info and it starts out like this smb://server/folder/folder/ANC/ANC.pdf

推荐答案

NSData和网址:有龙



+ [NSData dataWithContentsOfURL:] 可以返回 nil 任何原因。如果任何在尝试使用该URL时出错,则此方法将返回 nil 。例如,文件可能不存在,网络连接可能超时,或者URL甚至可能格式错误。返回 nil 并且应用程序不知道原因。

NSData and URLs: There be dragons

+[NSData dataWithContentsOfURL:] can return nil for any number of reasons. If anything goes wrong when attempting to use that URL this method will return nil. For example the file may not exist, the network connection may time out, or the URL may even be malformed. nil is returned and the application has no idea why.

+ [NSData dataWithContentsOfURL:选项:错误:] 另一方面,告诉来电者出了什么问题。当返回 nil 时,错误参数将填充一个描述发生问题的对象。使用此方法将直接回答为什么的问题。

+ [NSData dataWithContentsOfURL:options:error:] on the other hand, will tell the caller what went wrong. When nil is returned the error argument will be populated with an object that describes the problem that occured. Using this method would directly answer the question of "why".

这两种方法都是同步方法,用于处理文件,网络资源,尤其是文件服务。不鼓励使用网络资源。这些方法将阻止调用者,并不是真正用于这些用途。最好使用输入流或 NSURLSession

Both of these are synchronous methods and their use for working with files, network resources, and especially files served from a network resource is discouraged. These methods will block the caller and are not really intended for these kinds of uses. It's better to use an input stream or NSURLSession instead.

从您的问题看,您似乎正在尝试访问SMB共享中存在的文件。不幸的是,iOS不支持SMB - 即使你有一个格式正确的smb URL(即smb://servername/sharename/filename.pdf),如果不使用第三方SMB实现,你将无法访问它。

From your question though it seems you are trying to access a file that exists on an SMB share. Unforunately iOS does not support SMB - even if you had a correctly formatted smb URL (i.e. smb://servername/sharename/filename.pdf) you would be unable to access it without using a third party SMB implementation.

然而,使用FTP代替SMB应该可行。

Using FTP in place of SMB, however, should work.

这篇关于Objective-C dataWithContentsOfURL返回nill的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆