使用NSURLSession从NAS接收目录

时间:2016-12-25 13:09:11

标签: ios xcode8 nsurlsession nas

我是NSURLSession的新手,我希望使用以下代码连接到我的本地NAS服务器:

NSURL *url = [NSURL URLWithString:@"http://192.168.1.2/"];
    NSString * user = [NSString stringWithUTF8String:"admin"];
    NSString * password = [NSString stringWithUTF8String:"xxx"];
    NSURLProtectionSpace * protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:url.host port:[url.port integerValue] protocol:url.scheme realm:nil authenticationMethod:NSURLAuthenticationMethodHTTPBasic];

    NSURLCredential *cred = [NSURLCredential
                             credentialWithUser:user
                             password:password
                             persistence:NSURLCredentialPersistenceForSession];


    NSURLCredentialStorage * cred_storage ;
    [cred_storage setCredential:cred forProtectionSpace:protectionSpace];

    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    sessionConfiguration.URLCredentialStorage = cred_storage;
    sessionConfiguration.allowsCellularAccess = YES;

    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
    NSLog(@"viewdidload");

    NSURLSessionDataTask *task = [session dataTaskWithURL:url];
    [task resume];

我的请求返回状态代码200并在我的委托方法中:

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
    didReceiveData:(NSData *)data{

    NSString *dataString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"didReceiveData:%@", dataString);

}

我得到这个日志:

didReceiveData:

  

     

Index_Page

             var queryArg = location.search;        if(queryArg.substring(1,5)==“url =”){
  var hiddenURL = queryArg.substring(5,queryArg.length);
       }        else if(queryArg.substring(1,7)==“mount =”){
  var hiddenURL = queryArg.substring(7,queryArg.length);
       }        其他{
  var hiddenURL = "";
       }              if(hiddenURL ==“”){
  var seperateHref = window.location.href.split("/");

  if (seperateHref[0] == "https:")

      window.location =
     “HTTPS://” + seperateHref [2] + “/” + seperateHref [3] + “/desktop,/login.html”;
  else

      window.location =
     的 “http://” + seperateHref [2] + “/” + seperateHref [3] + “/desktop,/login.html”;        }        其他{
  location.replace("/desktop,/login.html"+queryArg);
       }            

     

为什么即使我通过用户和pw,我的代码也不能通过NAS进行身份验证? 在我尝试了一下之后,我的代码中没有凭证,didReceiveChallenge - delegateMethod也永远不会被调用。我只是想收到填充我的tableview的目录,以创建一个简单的文件浏览器..

0 个答案:

没有答案