문제

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];
}}
도움이 되었습니까?

해결책 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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top