Antoine Vandenheste
Antoine Vandenheste

Reputation: 131

How to add "http://" to a string (if missing)

I have a string [currentTweet pic] that is supposed to be the URL to a picture (parsed from an XML file). The problem is some of the links are missing the "http://" in the URL so some pictures don't appear. How can I add the "http://" where it's missing?

Any help is strongly appreciated! thx

Upvotes: 3

Views: 736

Answers (1)

Joris Kluivers
Joris Kluivers

Reputation: 12081

I assume you mean a NSString here, as opposed to a NSURL.

NSString *picURL = [currentTweet pic];
if (![picURL hasPrefix:@"http://"]) {
    picURL = [@"http://" stringByAppendingString:picURL];
}

Not sure if twitter ever uses https for pic urls, but you should possibly also check for https:// just to be sure.

Upvotes: 13

Related Questions