BlackSheep
BlackSheep

Reputation: 1087

Cannot pass NSString to a NSURL URLWithString

Try to pass my NSString to a URLWithString but no url appear:

//here i get my url from my API and replacing tags
NSString *queryContent = [[[(webLinks)[@"content"] stringByReplacingOccurrencesOfString:@"&" withString:@"&"]
                                                   stringByReplacingOccurrencesOfString:@"<p>" withString:@""]
                                                   stringByReplacingOccurrencesOfString:@"</p>" withString:@""];
//here i get the full url 
NSURL * url = [NSURL URLWithString:queryContent];

Adding a NSLogs:

 NSLog(@"query:%@", queryContent);
 NSLog(@"website:%@", url);

and the result is this:

query:http://mywebsite.com
website:(null)

Whats wrong?

thanks

Upvotes: 1

Views: 174

Answers (1)

BlackSheep
BlackSheep

Reputation: 1087

//SOLVED//

here the correct code if any of you use my same system JSON API for WordPress:

NSRange r;
NSString *queryUrl = [(webLinks)[@"content"] stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSString *website = queryUrl;
while ((r = [website rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
    website = [website stringByReplacingCharactersInRange:r withString:@""];

NSURL * url = [NSURL URLWithString:website];
[webView loadRequest:[NSURLRequest requestWithURL:url]];

Upvotes: 1

Related Questions