首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >IOS: kCFErrorDomainCFNetwork错误-1002。错误Domain=NSURLErrorDomain代码=-1002“不支持的URL”

IOS: kCFErrorDomainCFNetwork错误-1002。错误Domain=NSURLErrorDomain代码=-1002“不支持的URL”
EN

Stack Overflow用户
提问于 2016-09-15 12:02:09
回答 1查看 4.2K关注 0票数 1

你好,我知道以前问过的这类问题,但是在我的项目中我没有从他们那里得到任何解决方案,我在登录视图中工作,当我在登录按钮上放置代码时,我得到了一个错误。

错误:错误Domain=NSURLErrorDomain代码=-1002“不支持的URL”UserInfo=0x7fb37b62c9a0 {NSLocalizedDescription=unsupported URL,NSUnderlyingError=0x7fb37b715a20“操作无法完成。(kCFErrorDomainCFNetwork错误-1002.)"}

但我使用的登录代码与我在以前的项目中使用的代码相同,而且在那里运行良好。

这是我的代码:

代码语言:javascript
运行
复制
-(IBAction)login:(id)sender
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSURL *url = [NSURL URLWithString:@"http://eyeforweb.info.bh-in-15.webhostbox.net/myconnect/api.php?token={LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUc4d0RRWUpLb1pJaHZjTkFRRUJCUUFEWGdBd1d3SlVBeWo0WE9JNjI4cnJRTG9YeEpXNG1zUWI1YmtvYk1hVQpzMnY1WjFKeXJDRWdpOVhoRzZlZk4rYTR0eGlMTVdaRXdNaS9uS1cyL1NCS2pCUnBYUzVGYUdiV0VLRG1WOXkvCkYrWHhsUXVoeER0MEV3YkRBZ01CQUFFPQotLS0tLUVORCBQVUJMSUMgS0VZLS0tLS0K}&method=user.getLogin"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];

    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"*/*" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
    NSString *mapData = [NSString stringWithFormat:@"login=abc@gmail.com&password=123456"];//,username.text, password.text];
    NSData *postData = [mapData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    [request setHTTPBody:postData];
    NSLog(@"map data is = %@",mapData);
    NSURLSessionDataTask * postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse* response, NSError * error) {

        if(error == nil)
        {
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
            NSString *text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
            NSLog(@"Data = %@",text);
            NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
            NSLog(@"jsondic= %@",jsonDic);

            NSDictionary *userDataDic = [jsonDic objectForKey:@"data"];
            NSLog(@"Dict is %@",userDataDic);

请帮助我解决这个问题,我已经看到了类似的问题,但是没有从这个问题上克服任何帮助。

EN

回答 1

Stack Overflow用户

发布于 2016-09-15 13:24:07

我试过您的code.Except您的URL,其他代码行是correct.If,传递您的corrct,它工作得很好。

代码语言:javascript
运行
复制
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://eyeforweb.info.bh-in-15.webhostbox.net/myconnect/api.php"]]; //pass your url here
 [request setHTTPMethod:@"POST"];
 //Passing The String to server
 NSString *strUserId = @"pradeep.kumar@eyeforweb.com";
 NSString *strPassword = @"admin123";


 NSString *userUpdate =[NSString stringWithFormat:@"login=%@&password=%@",strUserId,strPassword, nil];

 //Check The Value what we passed
 NSLog(@"the data Details is %@", userUpdate);


 //Convert the String to Data
 NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];
 NSLog(@"The postData is - %@",data1);


 //Apply the data to the body
 [request setHTTPBody:data1];

 NSURLSession *session = [NSURLSession sharedSession];
 NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
 if(httpResponse.statusCode == 200)
 {
 NSError *parseError = nil;
 NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
 NSLog(@"The response is - %@",responseDictionary);
 NSInteger success = [[responseDictionary objectForKey:@"success"] integerValue];
 if(success == 1)
 {
 NSLog(@"Login SUCCESS");
 }
 else
 {
 NSLog(@"Login FAILURE");
 }
 }
 else
 {
 NSLog(@"Error");
 }
 }];
 [dataTask resume];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39510552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档