相关疑难解决方法(0)

iOS中的NSURLConnection和基本HTTP身份验证

我需要GET HTTP request用Basic 调用一个初始化Authentication.这将是第一次将请求发送到服务器并且我已经拥有了username & password这样的服务,因此不需要服务器进行授权的质询.

第一个问题:

  1. 是否NSURLConnection必须设置为同步才能执行Basic Auth?根据这篇文章的答案,如果你选择异步路由,似乎你不能做Basic Auth.

  2. 任何人都知道任何一些示例代码,说明了基本GET request身份验证,而无需质询响应?Apple的文档显示了一个示例,但仅在服务器向客户端发出质询请求之后.

我是SDK的新网络部分,我不确定我应该使用哪个其他类来实现这个功能.(我看到了NSURLCredential类,但它似乎只NSURLAuthenticationChallenge在客户端请求来自服务器的授权资源之后才使用它).

iphone objective-c nsurlconnection ios

84
推荐指数
4
解决办法
9万
查看次数

将NSData写入文件的最简单方法

NSData *data;
data = [self fillInSomeStrangeBytes];
Run Code Online (Sandbox Code Playgroud)

我现在的问题是如何data以最简单的方式写入文件.

(我已经是NSURL了file://localhost/Users/Coding/Library/Application%20Support/App/file.strangebytes)

file-io cocoa file save

64
推荐指数
3
解决办法
8万
查看次数

使用AFNetworking和HTTP基本身份验证

以下代码成功连接到我的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)

iphone json objective-c nsurlrequest afnetworking

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

iOS下载sqlite数据库并替换现有数据库

我必须下载数据库并替换沙箱中的现有数据库:这是可行的方法:

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)

sqlite nsurl ios

0
推荐指数
1
解决办法
2411
查看次数