Reputation: 79
I have a method in which i am calling a api and returning a string like this
-(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];
}
then in viewDidLoad Method i am making a url from this string and finally making a image
from this URL like this
but when i am printing urlString i get the exact value like this http://deluxecms.net/dev/resource/document/Shop/Deluxe screenshot.JPG but when i print urlPattern it is always null.
i am not able to understand the behaviour? please help me.
Upvotes: 1
Views: 461
Reputation: 5303
Try escape string before using this:
urlString = [[self urlReturn] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Upvotes: 2
Reputation: 21912
From the NSURL
doc for initWithString:
An NSURL object initialized with URLString. If the URL string was malformed, returns nil.
Should be a good clue that the URL is messed up. Replace this space with a %20 and it'll work.
So in short, do this:
NSString *urlString = @"http://deluxecms.net/dev/resource/document/Shop/Deluxe screenshot.JPG";
urlString = [urlString stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
NSURL *urlPattern = [NSURL URLWithString:urlString];
NSLog(@"%@", urlPattern);
Upvotes: 0