我有一个客户端通过cloudsearch在他们的网站上运行他们的搜索功能。我已经阅读了几天的文档,并且无法成功搜索请求。我创建了一个NSMutableRequest对象,并通过AWSSignature方法[signature interceptRequest:request]运行该请求;但我的task.result回来了(null)。
这是我的代码:
AWSTask *task = [signature interceptRequest:request];
[task continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) {
NSLog(@"task.result fromSearch:%@", task.result);
NSData *responseData = task.result;
NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"newStr:%@", newStr);
NSLog(@"task.error:%@", task.error);
return nil;
}];
我是在正确的轨道上,还是有更好的方法通过aws iOS sdk做到这一点?
为了给罗伯特评论的骨子多一点点,我在AFNetworking的帮助下做到了这样:
#import <AWSCore/AWSSignature.h>
#import <AWSCore/AWSService.h>
#import <AWSCore/AWSCategory.h>
#import <AWSCore/AWSCredentialsProvider.h>
#import <AWSCore/AWSTask.h>
#import "AFNetworking.h"
- (void)viewDidLoad {
[super viewDidLoad];
self.queue = [[NSOperationQueue alloc] init];
}
- (void)performSearch {
AWSAnonymousCredentialsProvider* credentialsProvider = [[AWSAnonymousCredentialsProvider alloc] init];
NSString* searchHost = @"<CloudSearchEndPoint>.eu-west-1.cloudsearch.amazonaws.com";
NSString* query = [self.searchTerms aws_stringWithURLEncoding];
NSURL* searchURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@/2013-01-01/search?q=%@", searchHost, query]];
AWSEndpoint* endpoint = [[AWSEndpoint alloc] initWithURL:searchURL];
AWSSignatureV4Signer* signer = [[AWSSignatureV4Signer alloc] initWithCredentialsProvider:credentialsProvider endpoint:endpoint];
NSMutableURLRequest* mutableRequest = [[NSMutableURLRequest alloc] initWithURL:searchURL];
AWSTask* task = [signer interceptRequest:mutableRequest];
[task continueWithBlock:^id(AWSTask* _Nonnull t) {
if (t.error) {
NSLog(@"Error: %@", t.error);
} else if (t.completed) {
NSLog(@"Result is %@", t.result);
}
AFJSONRequestOperation* operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:mutableRequest success:^(NSURLRequest* request, NSHTTPURLResponse* response, id JSON) {
NSLog(@"Success fetching results!");
if (JSON) {
NSDictionary* hitsContainer = [JSON objectForKey:@"hits"];
NSArray* hits = [hitsContainer objectForKey:@"hit"];
NSMutableArray* allResults = [[NSMutableArray alloc] initWithCapacity:hits.count];
for (NSDictionary* hit in hits) {
NSDictionary* fields = [hit objectForKey:@"fields"];
[allResults addObject:fields];
}
self.searchResults = allResults;
[self.tableView reloadData];
}
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Failure fetching search results :-( %@", error);
}
];
[self.queue addOperation:operation];
return nil;
}];