在iOS中,如何从Web服务器接收响应的http状态代码(404,500 200等).我假设它在NSUrlConnectionDelegate中.
Objective-C或Monotouch .NET回答好.
iphone xamarin.ios http-response-codes ios nsurlconnectiondelegate
我在特定文件夹上设置了NSURLCache(../../Application Support/Offline,因此它不会被OS随机删除),然后我发送NSURLRequest和NSURLRequestReturnCacheDataElseLoad策略.
如何判断由符合NSURLConnectionDelegate的类处理的响应是来自缓存还是来自网络?
nsurlconnection nsurlrequest nsurlcache nsurlconnectiondelegate
是否有可能取消NSURLConnection开始sendAsynchronousRequest:queue:completionHandler:?
为什么不sendAsynchronousRequest:queue:completionHandler:返回NSURLConnection它创建的对象以便我可以取消它?
nsurlconnection nsurlconnectiondelegate sendasynchronousrequest
现在5.0已经启动,我们可以在不违反Apple的NDA的情况下进行讨论,我对NSURLConnection的新版本存在问题.这有一个新的委托,NSURLConnectionDownloadDelegate有两个关键方法.
connection:didWriteData:totalBytesWritten:expectedTotalBytes: 在文件下载进行时重复调用
connectionDidFinishDownloading:destinationURL: 下载完成后调用一次.
然后下载的文件应该在destinationURL(至少在这个方法的生命周期中 - 意图是你得到它并将它移动到永久的某个地方).问题是:它不在那里.那个目录是空的.我已将此报告为Apple的错误,他们告诉我这是他们已经知道的问题的重复.
如果有人有解决方法,或者发现他们可以成功使用此委托,请告诉我.
更新10/17/2011:我已经放弃了这一点,并回到旧代表,它仍然可以在5.0中正常工作,即使文档说委托方法只能通过4.3.
我如何对我的NSURLConnection代表进行单元测试?我创建了一个ConnectionDelegate类,它符合不同的协议,以便将数据从Web提供给不同的ViewControllers.在我走得太远之前,我想开始编写单元测试.但我不知道如何在没有互联网连接的情况下将它们作为一个单元进行测试.我还想对待异步回调我应该怎么做.
unit-testing ocmock nsurlconnection ios nsurlconnectiondelegate
我使用react本机Fetch API来获取JSON数据, https://api.github.com/users/{username}但请求失败,并显示以下错误消息.
"TypeError:网络请求失败{stack:(...),message:'Network request failed'}".
我相信对于https,有时你会得到NSURLAuthenticationChallenge.我不知道如何实现这一点.有人对此有任何想法吗?
我知道如果我使用以下nsurlconnectiondelegate它将被修复
- 连接:willSendRequestForAuthenticationChallenge: - 连接:canAuthenticateAgainstProtectionSpace
但我正在尝试使用
sendAsynchronousRequest:队列:completionHandler:
所以你没有得到回调.我调查了它所说的苹果文档
如果需要进行身份验证才能下载请求,则必须将所需凭据指定为URL的一部分.如果身份验证失败或缺少凭据,则连接将尝试在没有凭据的情况下继续.
我无法弄清楚如何做到这一点.当我抬起头时,我得到的是这个私人电话
+(void)setAllowsAnyHTTPSCertificate:(BOOL)inAllow forHost:(NSString*)inHost;
知道怎么做吗?
以下是我得到的错误
此服务器的证书无效.您可能正在连接到假装为"example.com = 0x8b34da0 {NSErrorFailingURLStringKey = https://example.com/test/,NSLocalizedRecoverySuggestion =您想要连接到服务器的服务器吗?",NSErrorFailingURLKey = https:/ /example.com/test/,NSLocalizedDescription =此服务器的证书无效.您可能正在连接到冒充"example.com"的服务器,这可能会使您的机密信息面临风险.,NSUnderlyingError = 0xa26c1c0"此服务器的证书无效.您可能正在连接到假装的服务器是"example.com",可能会使您的机密信息面临风险.",NSURLErrorFailingURLPeerTrustErrorKey =
我使用Xcode beta6.我创建了一个具有Downloader类的应用程序,这是Downloader类:
class Downloader : NSObject {
private var _connection : NSURLConnection?
private var _downloadedData: NSMutableData?
func getDataFromURLString(urlToRequest: String!, aType: DownloadedDataType) {
_downloadedData = NSMutableData()
var request : NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlToRequest), cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 20.0)
request.setValue("", forHTTPHeaderField: "Accept-Encoding")
self._connection = NSURLConnection(request: request, delegate:self)
}
func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
println("Data expected size: \(response.expectedContentLength)")
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
println("finished")
}
func connection(connection: NSURLConnection!, didFailWithError error: NSError!) {
println("error: \(error)")
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现 nsurlconnectiondatadelegate,因为我需要支持异步模式 - 在同步模式下,我不想要自动遵循重定向。
作为参考,我有使用 urlRequest 等在同步模式下工作的代码。
问题是我无法让 FPC/Lazarus 编译我的代码。
...
代码片段
{$mode objfpc}
{$modeswitch objectivec1}
{$modeswitch objectivec2}
Run Code Online (Sandbox Code Playgroud)
...
// We need to implement support for NSURLConnectionDelegate and NSURLConnectionDataDelegate
TmsMacRequestDelegate = objcclass(NSObject)
public
// this will set flag when done
procedure connectionDidFinishLoading(ANSUC: NSURLConnection); message onnectionDidFinishLoading:'; override;
// ... implement rest?
end;
Run Code Online (Sandbox Code Playgroud)
...
requestDelegate := TmsMacRequestDelegate.alloc.init;
urlConnection := NSURLConnection.connectionWithRequest_delegate(urlRequest, requestDelegate);
// ... setup flag
urlConnection.start;
// ... wait here in loop checking flag set by "finish loading"
Run Code Online (Sandbox Code Playgroud)
...
有了上述内容,初步测试似乎不太顺利。我们到达 urlConnection.start; 但 …
我目前正在开发使用异步下载的iPad iOS 6应用程序.为了接收进度信息,我使用了代表NSURLConnectionDownloadDelegate.下载和收到的进度
– connection:didWriteData:totalBytesWritten:expectedTotalBytes:
Run Code Online (Sandbox Code Playgroud)
工作得很好.
但是一旦下载完成,我不知道如何NSURL destinationURL从delegate方法提供的数据中提取数据
– connectionDidFinishDownloading:destinationURL:
Run Code Online (Sandbox Code Playgroud)
在String中收到的destinationURL看起来像" /private/var/mobile/Applications/7CB3B194-9E79-4F0B-ACFD-7B87AA8C7BAF/tmp/filename.mp4"
然后我尝试从给定的NSURL中提取数据:
NSData *data = [[NSData alloc] initWithContentsOfURL:destinationURL];
Run Code Online (Sandbox Code Playgroud)
但数据是空的......
NSLog(@"data Size: %@", data.length); //prints out 0
Run Code Online (Sandbox Code Playgroud)
有人使用相同的问题NSURLConnectionDownloadDelegate吗?任何想法如何解决?
编辑:我尝试先复制文件,然后[NSData initWithContentsOFFile]按照建议使用,但数据大小仍为0.
- (void) connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL
{
[[NSFileManager defaultManager] copyItemAtPath:destinationURL.path toPath:DEST_PATH error:nil];
NSData *data = [[NSData alloc] initWithContentsOfFile:DEST_PATH];
NSLog(@"data Size: %i", data.length); //still returns 0..
}
Run Code Online (Sandbox Code Playgroud)
Edit2:我可以提取使用NSData时的sendAsynchronousRequest方法NSURLConnection.但是像这样,我无法确定下载的进度......
[NSURLConnection sendAsynchronousRequest:theRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData …Run Code Online (Sandbox Code Playgroud) ios ×6
iphone ×2
freepascal ×1
ios5 ×1
ios6 ×1
lazarus ×1
nsdata ×1
nsurl ×1
nsurlcache ×1
nsurlrequest ×1
ocmock ×1
pascal ×1
react-native ×1
swift ×1
unit-testing ×1
xamarin.ios ×1