3

I created my application. I need to open URL in iPhone, safari view directly.

My code as follows:

        NSString *email = [NSString stringWithFormat:@"http://itunes.apple.com/us/album/music/id186152317?i=186153149&uo=4"];
         email = [email stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
         NSURL *url = [NSURL URLWithString:email];
         if (![[UIApplication sharedApplication] openURL:url])
         NSLog(@"%@%@",@"Failed to open url:",[url description]);
         [super viewDidLoad];

But my problem is, the Safari view is opened. But the URL is not opened. It is showing error. The error is

Cannot Open Page safari cannot open the page because the address is invalid

But the URL is opened in browser.

Does anyone know the solution, to open this URL in Safari in iPhone using Objective C?

halfer
  • 19,824
  • 17
  • 99
  • 186
Velmurugan
  • 2,303
  • 6
  • 30
  • 45

5 Answers5

1

Your problem is that you should not be URL encoding the entire URL. You should only be encoding the keys and the values of each pair in the query string. In other words, URL encode: i, 186153149, uo, and 4. Nothing else. (And technically, you don't even need to encode those because they're all in the ASCII set of alphanumeric characters, which don't need to be encoded)

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
1

You can use http://itunes.apple.com/linkmaker/ to get exact link.Hope it helps

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Swastik
  • 2,415
  • 2
  • 31
  • 61
0

Change the encoding to "NSUTF8StringEncoding"

Satya
  • 3,320
  • 1
  • 20
  • 19
0

Also check out that after encoding is done then your link

http://itunes.apple.com/us/album/music/id186152317?i=186153149&uo=4

changes to

http://itunes.apple.com/us/album/music/id186152317?i=186153149&ign-mpt=uo%3D4

hAPPy iCODING...

Suresh Varma
  • 9,750
  • 1
  • 60
  • 91
0

Strange because Safari on the Mac doesn't complain...

But if you replace your url by http://itunes.apple.com/us/album/music/id186152317 it seems to work on the iPhone too.

Jilouc
  • 12,684
  • 4
  • 46
  • 43