小编Dan*_*ser的帖子

AFNetworking:全局处理错误并重复请求

我有一个相当常见的用例,但我找不到一个简单的方法来处理AFNetworking:

每当服务器返回任何请求的特定状态代码时,我想:

  • 删除缓存的身份验证令牌
  • 重新认证(这是一个单独的请求)
  • 重复失败的请求.

我认为这可以通过一些全局完成/错误处理程序来完成AFHTTPClient,但我没有找到任何有用的东西.那么,做"我想要的"的"正确"方式是什么?enqueueHTTPRequestOperation:在我的AFHTTPClient子类中重写,复制操作并用一个执行我想要的块(重新验证,排队复制操作)包装原始完成处理程序?或者我完全走错了路?

谢谢!

编辑:删除了对401状态代码的引用,因为当我使用令牌身份验证时,这可能是为HTTP basic保留的.

networking ios afnetworking

37
推荐指数
2
解决办法
2万
查看次数

AFNetworking:重试操作时访问完成处理程序

为了让一些背景:我试图实现对身份验证错误(使用令牌认证,而不是基本的),一个全球性的错误处理程序,它应该尝试重新进行身份验证,然后重复原始失败的请求(参见我刚才的问题:AFNetworking:手柄全局错误并重复请求)

当前的方法是注册一个观察者,该观察者进行AFNetworkingOperationDidFinishNotification重新认证,并且(如果auth成功)重复原始请求:

- (void)operationDidFinish:(NSNotification *)notification
{
    AFHTTPRequestOperation *operation = (AFHTTPRequestOperation *)[notification object];

    if(![operation isKindOfClass:[AFHTTPRequestOperation class]]) {
        return;
    }

    if(403 == [operation.response statusCode]) {
        // try to re-authenticate and repeat the original request
        [[UserManager sharedUserManager] authenticateWithCredentials...
            success:^{
                // repeat original request

                // AFHTTPRequestOperation *newOperation = [operation copy]; // copies too much stuff, eg. response (although the docs suggest otherwise)
                AFHTTPRequestOperation *newOperation = [[AFHTTPRequestOperation alloc] initWithRequest:operation.request];

                // PROBLEM 1: newOperation has no completion blocks. How to use the …
Run Code Online (Sandbox Code Playgroud)

networking ios afnetworking

7
推荐指数
1
解决办法
3435
查看次数

自定义NSAttributedString(iOS7 +)中的下划线模式

我正在尝试向NSAttributedString(在iOS7 + / TextKit上)添加点划线样式。当然,我尝试了内置功能NSUnderlinePatternDot

NSString *string = self.label.text;
NSRange underlineRange = [string rangeOfString:@"consetetur sadipscing elitr"];

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
[attString addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle | NSUnderlinePatternDot)} range:underlineRange];

self.label.attributedText = attString;
Run Code Online (Sandbox Code Playgroud)

但是,由此产生的样式实际上是虚线而不是虚线:

屏幕截图

我是否在这里缺少明显的东西(NSUnderlinePatternReallyDotted?;)?或者也许可以自定义线点图案?

underline nsattributedstring ios textkit

5
推荐指数
1
解决办法
1610
查看次数

AFNetworking:带有application/x-www-form-urlencoded的POST请求

我正在学习使用AFNetworking.

我知道我可以使用AFHTTPClientjson发出POST请求.

我的问题是:有没有办法制作标准的POST请求(即内容类型为application/x-www-form-urlencoded)?我的服务器后端不接受json,因为客户端应该使用相同的表单通过web登录.

在过去我用过ASIHTTPRequest,我用过这段代码:

url = [NSURL URLWithString:@"www.example.org/login/"];
request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:self.username forKey:@"username"];
[request setPostValue:self.password forKey:@"password"];
[request startAsynchronous];
Run Code Online (Sandbox Code Playgroud)

谢谢!

objective-c asihttprequest ios afnetworking

4
推荐指数
1
解决办法
9743
查看次数