ios - NSURL 始终为空

标签 ios iphone nsurlconnection nsurl nsurlrequest

我有一个调用 api 并返回这样的字符串的方法

     -(NSString *)urlReturn{

          NSURL *aUrl = [NSURL URLWithString:@"http://deluxecms.net/dev/api/index.php"];
         NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
        cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];
          [request setHTTPMethod:@"POST"];
          NSString *postString =    @"method=getDocument&
         deviceId=cdad86b3d8bca5c09885af94990eda193c40ab03& 
      cipher=803ae952ff4a785588397362860d045e&
      version=1&lastSync=0";
        NSLog(@"postString %@",postString);
       [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

       NSHTTPURLResponse* urlResponse = nil;
       NSError *error = [[NSError alloc] init];
       NSData *responseData = [NSURLConnection sendSynchronousRequest:request
       returningResponse:&urlResponse error:&error];

    if(error.domain!=nil)
     {
        NSString *errorDesc=[[error userInfo] objectForKey:@"NSLocalizedDescription"];
         NSLog(@"Response Error === %@",errorDesc);
        return nil;
     }

        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:responseData
                                                       options:0 error:&error];
     if (doc == nil) {
        return nil;
     }

      NSArray *partyMembers = [doc nodesForXPath:@"//assets/asset[11]/file" error:nil];
      NSLog(@"ASSTES %@",partyMembers);
        NSString *urlStringmine;
        GDataXMLElement *urlElement = (GDataXMLElement *)[partyMembers objectAtIndex:0];
        urlStringmine = urlElement.stringValue;
        NSLog(@"MY URL STRING %@",urlString);

        return urlStringmine;
    }


    - (void)viewDidLoad
   {
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
     urlString = [self urlReturn];
       NSURL *urlPattern = [[NSURL alloc]initWithString:urlString];
       UIImage *image = [UIImage imageWithData: [NSData
      dataWithContentsOfURL:urlPattern]];
     NSLog(@"Imge %@",urlPattern);
     [self.imageView setImage:image];
  }

然后在 viewDidLoad 方法中,我从这个字符串制作一个 url,最后制作一个图像
像这样从这个 URL

但是当我打印 urlString 时,我得到了这样的确切值 http://deluxecms.net/dev/resource/document/Shop/Deluxe截图.JPG 但是当我打印 urlPattern 时它总是空的。

我无法理解这种行为? 请帮助我。

最佳答案

在使用之前尝试转义字符串:

urlString = [[self urlReturn] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

关于ios - NSURL 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21795115/

相关文章:

ios - 与名为 com.apple.nsurlstorage-cache 的服务的连接已失效

ios - 处理失去连接的情况

ios - 在transitionFromViewController中传递数据

ios - Swift TableView 不显示 Nib 中的任何内容

java - Android 是否像 iPhone 一样在顶部栏中有一个互联网状态转圈?

ios - 自动布局网格按钮间距恒定

ios - 类型 'MapViewController' 没有成员 'mapTypeChanged' Swift Big Nerd Ranch Guide

ios - 对齐 UIBarButtonItem 标题以防止其他按钮重叠?

ios - 如何以编程方式在 iOS 中重启 iPhone 应用程序

ios - 即使在 plist 文件上添加临时异常后,在 xcode 版本 9.3 ios 11 中 HTTP 加载失败