1

I have a string as follows (Which actually is a link) http://xxx.xx.xxx.xxx:xxxx/goat/mainMenuNew.png (x is replaced by the ip address)

I want to convert it to a URL. Below is what i did,

NSURL *imageUrl = [NSURL URLWithString:objUserInfo.photoLink];

also tried alloc and initWithString.

But, imageUrl is nil in both the cases.

Please let me know where am i going wrong.

13
  • Are you sure that objUserInfo.photoLink is not nil? Commented Feb 28, 2013 at 5:08
  • Ya it is not nil. I did print it prior passing it as a parameter. Commented Feb 28, 2013 at 5:09
  • What @BenTrengrove said. Also, it's worth adding in an NSLog() or something to make sure it's not a malformed URL; if it is, your NSURL object will be nil. Commented Feb 28, 2013 at 5:10
  • 2
    @bgoers Local variables are strong by default with ARC. Commented Feb 28, 2013 at 5:24
  • 1
    @Raj can you NSLog objUserInfo.photoLink and print the log here. Also, NSLog imageUrl and print the log here. Commented Feb 28, 2013 at 5:31

2 Answers 2

2

You can try followings:

  • Check if objUserInfo.photoLink still contains the string, by logging it before converting to URL
  • If it does try trimming your string with:

    [objUserInfo.photoLink stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
  • Check san's answer for adding percent escapes if problem persist
Sign up to request clarification or add additional context in comments.

Comments

2

Add this, also your link include space or not.

NSString *urlStr =[objUserInfo.photoLink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *imageUrl = [NSURL URLWithString:urlStr];

Paste to Safari this link: http://www.google.com/search?q=I%20Love%20You Enter and you'll understand what the code do.

4 Comments

Ya thank you, its working, but the url is preceded by the following characters %0A%20%20%20%20%20%20%20%20. Is it normal to have it?
@Raj You must have parsed the URL string from some text and you ended up with leading carriage return and spaces. Use the answer by "rptwsthi" to trim the leading whitespace and newline.
Yes, %0A, or %20 replace some special character when working with URL.Don't worry about it. I don't remember,maybe %20 = space. Alway do it with internet URL
@Raj sang say's right %20 need to fill up your URL white-space so you must encoding your URL sting with %20 +1 for good Answer Sang

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.