我需要GET HTTP request用Basic 调用一个初始化Authentication.这将是第一次将请求发送到服务器并且我已经拥有了username & password这样的服务,因此不需要服务器进行授权的质询.
第一个问题:
是否NSURLConnection必须设置为同步才能执行Basic Auth?根据这篇文章的答案,如果你选择异步路由,似乎你不能做Basic Auth.
任何人都知道任何一些示例代码,说明了基本GET request身份验证,而无需质询响应?Apple的文档显示了一个示例,但仅在服务器向客户端发出质询请求之后.
我是SDK的新网络部分,我不确定我应该使用哪个其他类来实现这个功能.(我看到了NSURLCredential类,但它似乎只NSURLAuthenticationChallenge在客户端请求来自服务器的授权资源之后才使用它).
NSData *data;
data = [self fillInSomeStrangeBytes];
Run Code Online (Sandbox Code Playgroud)
我现在的问题是如何data以最简单的方式写入文件.
(我已经是NSURL了file://localhost/Users/Coding/Library/Application%20Support/App/file.strangebytes)
以下代码成功连接到我的Ruby on Rails API并使用AFNetworking返回JSON.我需要做什么来编辑它以传递用户名和密码,以便我的API可以使用HTTP基本身份验证?
我已经阅读了他们的文档,但我是Objective-C和AFNetworking的新手,目前还没有意义.
NSURL *url = [[NSURL alloc] initWithString:@"http://localhost:3000/tasks.json"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request
, NSHTTPURLResponse *response
, id JSON) {
self.tasks = [JSON objectForKey:@"results"];
[self.activityIndicatorView stopAnimating];
[self.tableView setHidden:NO];
[self.tableView reloadData];
NSLog(@"JSON");
} failure:^(NSURLRequest *request
, NSHTTPURLResponse *response
, NSError *error
, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
Run Code Online (Sandbox Code Playgroud) 我必须下载数据库并替换沙箱中的现有数据库:这是可行的方法:
DBHelpers *help=[[DBHelpers alloc] init];
NSString *targetPath=[[help DocumentsDirectory] stringByAppendingPathComponent:DATABASE_NAME];
NSLog(@"Target path: %@", targetPath);
NSFileManager *fileManager=[NSFileManager defaultManager];
//if([fileManager fileExistsAtPath:targetPath])
//{
// NSLog(@"Exists");
// return;
}
NSString *sourcePath=[help PathForResource:DATABASE_NAME];
NSLog(@"SourcePath path: %@", sourcePath);
NSError *error;
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"www.hello.com/mydb.sqlite"]];
[data writeToFile:targetPath atomically:NO];
// [fileManager copyItemAtPath:sourcePath toPath:targetPath error:&error];
NSLog(@"Error %@", error);
Run Code Online (Sandbox Code Playgroud) ios ×2
iphone ×2
objective-c ×2
afnetworking ×1
cocoa ×1
file ×1
file-io ×1
json ×1
nsurl ×1
nsurlrequest ×1
save ×1
sqlite ×1