2

This method opens up the url in Safari when the website string is not null and it is atleast of length 3. But when I have supplierWebsite=@"www.heritage.com", nothing happens. I know that heritage.com is not valid website and so it is not activating in UIApplication. I would like to display atleast a pop up that would tell user that website is not available. Is there any way i can show Alertview telling that website is not available.

- (IBAction)doWebOpen:(UIButton *)sender {

if (self.provider.supplierWebSite && [self.provider.supplierWebSite length] > 3) {
    NSString *urlString = [self.provider supplierWebSite];
    NSURL *url = [NSURL URLWithString:urlString];
    [[UIApplication sharedApplication] openURL:url];

}else {

    NSError *err = [NSError errorWithDomain:@"com.cantopenweb" code:509 andDescription:@"This supplier does not have a website."];
    [self showErrorAlert:err];
}}
Shaunak
  • 707
  • 1
  • 9
  • 29

2 Answers2

3

You could use canOpenURL method,

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"your website"]];

The method returns a BOOL, so check that for YES or NO.

If YES, it CAN else NO.

Charan
  • 4,940
  • 3
  • 26
  • 43
2

Just use canOpenURL of UIApplication class, like:

if([[UIApplication sharedApplication] canOpenURL:url])
 {
    [[UIApplication sharedApplication] openURL:url];
 }
 else
 {
   //show alert
 }

canOpenURL:

Returns whether an application can open a given URL resource.

- (BOOL)canOpenURL:(NSURL *)url

Parameters

url

A URL object that identifies a given resource. The URL’s scheme—possibly a custom scheme—identifies which application can

handle the URL.

Return Value

NO if no application is available that will accept the URL; otherwise, returns YES. Discussion

This method guarantees that that if openURL: is called, another application will be launched to handle it. It does not guarantee that the full URL is valid. Availability

Available in iOS 3.0 and later.

Declared In UIApplication.h

Midhun MP
  • 103,496
  • 31
  • 153
  • 200