未安装应用程序时启动Web应用程序

时间:2015-09-14 08:01:04

标签: ios objective-c swift url-scheme

我希望能够在多个应用上分享内容。我正在使用URL Scheme。但是如果没有安装该应用程序,我想启动相关应用程序的Web版本。

这样的事情:

func share(urlScheme: String)
{
     if (UIApplication.sharedApplication().canOpenURL(NSURL(string:urlScheme)!))
     {
         UIApplication.sharedApplication().openURL(NSURL(string: string:urlScheme)!)
     }
     else
     {
         // open web version somehow
     }
}

1 个答案:

答案 0 :(得分:3)

在这里,我为Uber添加了一些样本来定制您的自我,我们不知道您的urlScheme包含的内容

例如

<强>夫特

func share(urlScheme: String)
{
 if   (UIApplication.sharedApplication().canOpenURL(NSURL(string:"uber://")!))
 {

  // if your app is available it open the app 
     UIApplication.sharedApplication().openURL(NSURL(string: string:"uber://?openapp")!)
 }
 else
 {
     open web version somehow
    // it redirect to safari, safari directly open the web page on uber application, it does not redirect to app store

     UIApplication.sharedApplication().openURL(NSURL(string: string:"http://m.uber.com/?opneapp")!)
 }
}

<强>目标C

 NSString *getUberDetails=[NSString stringWithFormat:@"client_id=vbtCwkGyqvDupQhpCcgCMo4VE-gIRw42&action=setPickup&pickup[latitude]=%f&pickup[longitude]=%f&pickup[nickname]=OuttAPP&dropoff[latitude]=%f&dropoff[longitude]=%f&dropoff[nickname]=%@&product_id=%@",[latitude floatValue],[longitude floatValue],[droplatitude floatValue],[dropLongtitude floatValue],[dropAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[[huberTableArr objectAtIndex:indexPath.row]objectForKey:@"productID"]];


    if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"uber://"]])
    {


        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString  stringWithFormat:@"uber://?%@",getUberDetails]]];

    }
    else
    {
        [[UIApplication sharedApplication] openURL: [NSURL URLWithString:[NSString stringWithFormat:@"http://m.uber.com/?%@",getUberDetails]]];
    }
}