我在一个单独的线程上运行NSURLConnection(我知道它是异步的并且在主线程上运行时工作),但即使我将父线程作为委托传递,它也不会进行委托调用.有谁知道如何做到这一点?
码:
-(void)startConnectionWithUrlPath:(NSString*)URLpath {
//initiates the download connection - setup
NSURL *myURL = [[NSURL alloc] initWithString:URLpath];
myURLRequest = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[myURL release];
//initiates the download connection on a seperate thread
[NSThread detachNewThreadSelector:@selector(startDownloading:) toTarget:self withObject:self];
}
-(void)startDownloading:(id)parentThread {
NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
[NSURLConnection connectionWithRequest:myURLRequest delegate:parentThread];
//The delegate methods are setup in the rest of the class but they are never getting called...
[pool drain];
}
Run Code Online (Sandbox Code Playgroud)
编辑*
我需要在一个单独的线程上运行NSURLConnection的原因是因为我在我的iPhone应用程序中下载了一些内容,并且当用户锁定屏幕时下载取消(如果用户只需按下主页按钮并且应用程序进入后台,则继续下载).我理解这是因为我在主线程上异步运行连接,而不是单独的.
我在启动NSURLConnection时也尝试过这段代码(不是在一个单独的线程中):
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:myURLRequest delegate:self startImmediately:NO]; …Run Code Online (Sandbox Code Playgroud) iphone multithreading objective-c nsurlconnection nsurlconnectiondelegate
我正在尝试使用安全的restful服务,这会给出错误
错误=错误域= NSURLErrorDomain代码= -1202"此服务器的证书无效.您可能正在连接到一个伪装成是服务器'xxx.xxx.xxx.xxx’,它可以把您的机密信息处于危险之中."
在xCode 4.2上工作,错误或缺少任何步骤.
使用以下代码
RegisterUser.f
@interface RegisterUser : UIViewController<UITextFieldDelegate,
UIScrollViewDelegate, NSURLConnectionDelegate>
Run Code Online (Sandbox Code Playgroud)
RegisterUser.m
- (IBAction)SubmitBtnAction:(id)sender {
NSURL *url = [NSURL URLWithString:@"https://xx.xx.xx.xxx:8223/jaxrs/tunedoorgateway/getCountries"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] >0 && error == nil)
{
NSLog(@"Data = %@", data);
// DO YOUR WORK HERE
}
else if ([data length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error …Run Code Online (Sandbox Code Playgroud) iphone objective-c nsurlconnection ios nsurlconnectiondelegate
概观
我正在为一个iOS应用程序进行SAML登录(单点登录,类似于openID)解决方案,该解决方案涉及显示带有a的视图控制器,UIWebView并且在处理HTTP基本/摘要时我遇到了计时和/或超时问题认真的UIWebView.
具体来说,当客户端获得HTTP身份验证质询时,我会弹出UIAlertView提示用户输入用户ID和密码的信息.如果用户能够快速输入信息(<10秒),则可以正常工作.但是,如果条目超过10秒,则连接似乎已终止且没有任何反应.
问题
connection:didReceiveAuthenticationChallenge:会阻止我提示用户输入用户ID和密码(并且必须等待用户输入)?有没有人有解决方法(例如某种方式来延长连接超时)?UIWebView一个子类的HTTP基本/摘要auth NSURLProtocol?细节和代码
对于我们需要处理的大多数SAML系统,登录将显示为常规网页UIWebView.但是,我们需要处理的一些系统可以回退到使用移动浏览器的HTTP基本或HTTP摘要身份验证,因此我们也需要能够处理它.
最大的挑战始于UIWebView不会暴露下面的网络调用.为了得到我需要的东西,我已经创建了一个子类NSURLProtocol并在必要时注册了它:
[NSURLProtocol registerClass:[SMURLProtocol class]];
Run Code Online (Sandbox Code Playgroud)
这样,SMURLProtocol当发出HTTP基本/ auth质询时,会调用此方法,因此我返回YES,我们可以处理HTTP基本和摘要式身份验证:
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest]
|| [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]);
}
Run Code Online (Sandbox Code Playgroud)
现在我告诉网络堆栈SMURLProtocol可以处理auth挑战,所以它调用
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];
NSString *authenticationMethod = [protectionSpace authenticationMethod];
if ([authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]
|| [authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest]) {
// Stash the challenge in an IVAR so we can use …Run Code Online (Sandbox Code Playgroud) objective-c uiwebview nsurlprotocol ios nsurlconnectiondelegate
我正在开发一个使用NSURLConnection来获取某些网络内容的iPhone应用程序.我的iPhone应用程序应该适用于iOS> = 4.1
(1)以下代理方法是否适用于所有iOS> = 4.1,包括iOS5(Beta). - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data; - (void)connectionDidFinishLoading:(NSURLConnection*)连接; - (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error;
该文档提到这些方法是"在iOS 2.0到iOS 4.3中可用",并列在iOS5文档的"不推荐的NSURLConnection方法"部分中.(但是当我命令+点击这些方法时,会列在iOS5.0库的Foundation框架下)
(2)当我们打电话给[取消]时,请求是立即取消还是当请求被实际取消时我们是否得到任何回调?在取消调用后立即释放NSURLConnection_object是否安全?
谢谢和问候,Deepa
我成功地使用了connection:didReceiveAuthenticationChallenge:NSURLConnectionDelegate 的相当棒的委托方法.凉.
我想正确支持让用户取消身份验证挑战.因此,我向用户呈现的GUI具有取消按钮,问题是该按钮后面应该发生什么行为.
目前我这样做[[challenge sender] cancelAuthenticationChallenge:challenge],我已经实现了NSURLConnectionDelegate方法connection:didCancelAuthenticationChallenge:.在Apple文档中,我注意到这个回调的相当模糊的注释:
可作为iOS 5.0之前的非正式协议的一部分.
咦?实际触发的回调方法是connection:didFailWithError:
有人可以在这里说清楚吗?
basic-authentication nsurlconnection ios nsurlconnectiondelegate
最有可能是一个相当简单的问题,但完成块总是会被调用[NSURLConnection sendAsynchronousRequest: ...]吗?或者我必须实现超时计时器吗?
考虑以下我MBProgressView在调用之前添加a 并在完成块中仅删除它:
[self showHUDWithTitle:@"Configuring"];
[NSURLConnection sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if ([data length] >0 && error == nil) {
[self hideHUDWithFlag:YES
andTitle:@"Finished"
andSubtitle:@"(Box was configured)"];
} else if ([data length] == 0 && error == nil) {
[self hideHUDWithFlag:NO
andTitle:@"Failed"
andSubtitle:@"(Check box connection)"];
NSLog(@"Nothing was downloaded.");
} else if (error != nil) {
[self hideHUDWithFlag:NO
andTitle:@"Error"
andSubtitle:@"(Check box connection)"];
NSLog(@"Error = %@", error);
}
}];
Run Code Online (Sandbox Code Playgroud) 我需要在加载webview中的任何url时检查响应状态代码.现在我们可以考虑在web视图中加载的任何Web应用程序.所以我需要跟踪具有该webview的每个请求并相应地检查响应代码.为了找到响应代码,我需要在uiwebview的委托方法"shouldStartLoadWithrequest"中使用NSUrlConnection.有点像这样 -
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType{
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
if (conn == nil) {
NSLog(@"cannot create connection");
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
和NSURLConnectionDelegate一样 -
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"connection:didReceiveResponse");
[self log:@"received response."];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
int status = [httpResponse statusCode];
NSLog(@"http status code: %d", status);
if (status == 200) {
NSLog(@"Response OK");
}
else {
NSLog(@"Response is not OK");
}
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSLog(@"connection:didReceiveData");
}
- …Run Code Online (Sandbox Code Playgroud) iphone uiwebview ios uiwebviewdelegate nsurlconnectiondelegate
关于" didFailWithError"和" connectionDidFinishLoading"
他们都可以被召唤吗?或者它总是一个或另一个?
我正在使用POST请求将一些数据上传到服务器,我正在尝试根据方法的totalBytesWritten属性更新UIProgressView的进度.使用下面的代码,我没有得到正确的进度视图更新,它总是0.000直到它完成.我不确定要乘以或除以什么来获得更好的上传进度.didSendBodyDataNSURLConnection
我很感激提供任何帮助!码:
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
NSNumber *progress = [NSNumber numberWithFloat:(totalBytesWritten / totalBytesExpectedToWrite)];
NSLog(@"Proggy: %f",progress.floatValue);
self.uploadProgressView.progress = progress.floatValue;
}
Run Code Online (Sandbox Code Playgroud) objective-c nsurlconnection uiprogressview ios nsurlconnectiondelegate
目前使用来自NSURLConnection的sendAsynchronous来发布和获取请求,但无法获取响应中的状态代码.大多数帖子都建议使用NSURLConnection及其委托方法,我理解它们也是异步的.
我不明白委托如何将信息发送回调用方法(最终需要数据的方法).sendAsynchronous方法有一个我现在正在使用的回调.
我是新来的,谢谢你的帮助.
cocoa-touch http-status-code-401 nsurlconnectiondelegate sendasynchronousrequest ios6
-(void)method1
{
[self method2];
[self method3]; //After finishing execution of method2 and its delegates I want to execute method3
}
Run Code Online (Sandbox Code Playgroud)
这里,method2在调用时运行,但在执行其委托方法之前,method3开始执行.怎么避免呢?请提出任何建议或代码
我在方法2中调用了与其委托的nsurl连接
-(void)method2
{
....
connection= [[NSURLConnection alloc] initWithRequest:req delegate:self ];
....
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
}
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data
{
}
..
..
Run Code Online (Sandbox Code Playgroud)