我开始使用 AFNetworking 来替换不幸失效的 ASIHTTPRequest。人们似乎喜欢这个新的“AFNetworking”,但它没有构建在我的项目中,我不明白为什么。Class class = NSClassFromString(className);我在HTTPRequestOperationWithRequest:success:failure文件中开始遇到构建错误AFHTTPClient.m。
错误是“匿名类的声明必须是定义”,我已经问过谷歌但没有得到任何结果。帮助?
语境:
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
AFHTTPRequestOperation *operation = nil;
NSString *className = nil;
NSEnumerator *enumerator = [self.registeredHTTPOperationClassNames reverseObjectEnumerator];
while (!operation && (className = [enumerator nextObject])) {
Class class = NSClassFromString(className);
if (class && [class canProcessRequest:urlRequest]) {
operation = [[(AFHTTPRequestOperation *)[class alloc] initWithRequest:urlRequest] autorelease];
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:根据下面的答案解决。补丁文件在这里。
编辑 2:这些更改已被纳入最新的 AFNetworking 中,因此将来发现此内容的任何人:只需下载新版本即可。还有,未来怎么样?你好未来。
有人能解释异步NSURL请求与GCD和NSOperationQueues之间的关系吗?
我不确定何时使用它们.
现在,当我需要将数据提取/上传到服务器时,我一直在"逃避"异步NSURL请求.但有人建议我应该使用GCD.我的问题是我不知道现实生活中的例子GCD会更好.有没有人对我有任何常见用例?例如,如果我使用GCD存储10个异步NSURL GET请求的队列,这对我有什么好处?在大型中央调度队列或NSOperationQueue中有异步NSURL请求是否有意义?
谢谢!
nsurlconnection nsoperationqueue grand-central-dispatch ios afnetworking
我正在制作一个应用程序,我必须调用一些web服务.我选择与AFNetworking合作.
我按照库中提供的Twitter示例进行操作.一切都运作良好,除了我在通知栏中永久保留了小"处理圈"(见下图).

这是我的请求代码:
- (id)initWithAttributes:(NSDictionary *)attributes
{
self = [super init];
if (!self) {
return nil;
}
_name = [attributes valueForKeyPath:@"name"];
return self;
}
+ (void)itemsListWithBlock:(void (^)(NSArray *items))block
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *user = [defaults objectForKey:@"user"];
NSDictionary *company = [defaults objectForKey:@"company"];
NSMutableDictionary *mutableParameters = [NSMutableDictionary dictionary];
/*
** [ Some stuff to set the parameters in a NSDictionnary ]
*/
MyAPIClient *client = [MyAPIClient sharedClient];
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
[[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];
NSURLRequest *request = [client …Run Code Online (Sandbox Code Playgroud) 我使用AFNetworking从我的rails api获取啤酒细节,我想从返回的url中显示一个图像.
示例API响应
description = "Super tasty";
id = 3;
"label_url" = "/system/beers/labels/000/000/003/medium/Pliny.png?1363034728";
"location_id" = 2;
name = "Pliny The Elder";
price = "3.5";
style = IPA;
Run Code Online (Sandbox Code Playgroud)
label_url是图像的url,我试图在BeerDetailViewController中显示它.这是我到目前为止所尝试的,它没有错误但没有图像显示.
BeerDetailViewController.m
#import "UIImageView+AFNetworking.h"
#import "AFImageRequestOperation.h"
- (void)viewDidLoad
{
[super viewDidLoad];
beerLabel.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString: beer_label]]];
Run Code Online (Sandbox Code Playgroud)
任何想法为什么它不起作用?我可以在这个视图中显示这样的网址,beerLabel.text = beer_label我知道它正在通过并阅读使用,只是不确定为什么它不显示图像.
我正在尝试在IOS中运行以下代码.
AFHTTPRequestOperation *requestOperation = [self.httpClient HTTPRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url.text]]
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"reply data = %@", [[NSString alloc] initWithData:operation.responseData encoding:NSUTF8StringEncoding]);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
[requestOperation setAuthenticationAgainstProtectionSpaceBlock:^BOOL(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace) {
return YES;
}];
Run Code Online (Sandbox Code Playgroud)
然后我得到这样的错误:
- [AFHTTPRequestOperation setAuthenticationAgainstProtectionSpaceBlock:]:无法识别的选择器发送到实例0x75a81f0
据我所知,我传递的块具有正确的返回类型和参数.我在这做错了什么?
我想删除服务器上的多个文件,我需要将多个参数值发送到服务器。例如:upload_id="11,12,13",每个值都是一个逗号分隔的字符串。我使用了 NSArray 但它不起作用。服务器响应不成功。这是我的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([_tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){
[_tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}else{
[_tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
NSArray *selectedRows = [_tableView indexPathsForSelectedRows];
for (NSIndexPath *indexPath in selectedRows) {
uploadidString =[uploadidArray objectAtIndex:indexPath.row];
}
[iduploadArray insertObject:uploadidString atIndex:0];
NSLog(@"ID UPLOAD Array %@",iduploadArray);
}
-(void) deleteItems
{
NSURL *url1 = [NSURL URLWithString:@"http://myserver"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL: url1] ;
NSDictionary *params1 = @{
@"upload_id" : [NSArray arrayWithObjects:iduploadArray,nil],
};
NSMutableURLRequest *afRequest = [httpClient requestWithMethod:@"POST" path:nil parameters:params1] ;
AFHTTPRequestOperation …Run Code Online (Sandbox Code Playgroud) 在 Podfile 我
pod 'AFNetworking', '~> 2.6.0'
Run Code Online (Sandbox Code Playgroud)
pod 更新,我使用 xcworkspace 打开项目。
一切都如我所愿,但是:
当我导入 AFNetworking 时,Xcode 提示 'No such module AFNetworking'
我搜索了这个问题的解决方案,并这样做:在“内置设置->用户标题搜索路径”中添加${SRCROOT}并选择递归。
但它没有用。Xcode 抛出相同的消息:'No such module AFNetworking'
我正在使用AFN检查网络。
__block BOOL connect;
AFNetworkReachabilityManager*manager = [AFNetworkReachabilityManager sharedManager];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case -1:
connect=YES;
break;
case 0:
connect=NO;
break;
case 1:
connect=YES;
break;
case 2:
connect=YES;
break;
default:
break;
}
}];
[manager startMonitoring];
Run Code Online (Sandbox Code Playgroud)
我想让布尔连接到另一个文件
if (![ValueUtils isConnectNet])
Run Code Online (Sandbox Code Playgroud)
但是它没有立即解决问题,我该如何先解决问题,然后再做“如果没有”的事情呢?
现在我使用Reachability,如果在startMonitoring之后立即使用AFN的isReachable,它就无法立即获得当前的网络状态。
虽然我们有很多人问同样的问题,但没有一个答案有助于解决我的问题.所以这是我的代码,
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *text = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@", text);
//The NSJSONSerialization method to transform the NSData responseObject into a dictionnary does work
NSJSONSerialization *jsonResponse = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
//This NSLog makes the app crash with an unrecognized selector sent error
NSLog(@"JSON: %@",jsonResponse);
NSArray *result1=(NSArray *)[jsonResponse valueForKeyPath:@"result"];
NSLog(@"result1: %@",result1);
NSMutableString *shipPrice =[[NSMutableString alloc]init];
NSMutableString *freeLimitString =[[NSMutableString alloc]init];
if(result1!=Nil && ![result1 count]==0)
{
for (int i=0; i<result1.count; i++) …Run Code Online (Sandbox Code Playgroud) 如何使用UTF8编码以查询字符串格式URL / param1 / param2 / param3对URL参数进行编码
我什至尝试了URLHostAllowedCharacterSet,它编码特殊字符,但不编码“ +”字符。
如何使用almofire请求对包含+字符的电子邮件进行编码?
我想创建一个从Internet下载任何文件并存储在文件夹中的应用程序.我在谷歌和这个网站搜索,我明白使用AFNetworking更好.在这个框架中,我可以显示progessViewshow下载状态.
我是AFNetworking的新手,我希望你指导我,并告诉我如何使用AFNetworking创建一个下载文件并显示progessView状态下载的应用程序代码.
我正在通过AFNetworking异步下载数千张图像并将它们存储在iDevice中,但是当控制台上出现错误时,我的应用程序变慢了"响应超时"
以下是我用来下载图像的代码.
[NSThread detachNewThreadSelector:@selector(DownloadImages) toTarget:self withObject:nil];
-(void)DownloadImages
{
for(int i = 0; i<=4600;i++)
{
NSString *FrameSmall = [NSString stringWithFormat:@"myimageurl%i.png",i];
[self setbuttonImg:FrameSmall];
}
}
-(void)setbuttonImg:(NSString *)str
{
NSArray* badWords = @[@":", @"/", @".",@" "];
NSMutableString* mString = [NSMutableString stringWithString:str];
for (NSString* string in badWords) {
mString = [[mString stringByReplacingOccurrencesOfString:string withString:@""] mutableCopy];
}
NSString *encoded = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:encoded]];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString * …Run Code Online (Sandbox Code Playgroud) afnetworking ×13
ios ×11
objective-c ×7
swift ×2
cocoapods ×1
iphone ×1
nsurl ×1
parameters ×1
request ×1
uiimage ×1
uiimageview ×1