从Safari直接打开ViewController of App

时间:2016-01-20 07:00:14

标签: ios

我有2个ViewController,如下所示:

enter image description here

我想通过使用URL Scheme从safari打开我的应用程序,我已经像这样设置了它。

enter image description here

当我打开Safari并调用它时,它有效:TestSafari://。但我想自动打开ViewController2而不是初始的ViewController,怎么做呢?

更新

在AppDelegate中设置完成后,现在效果很好。

 func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("VC2")
        self.window?.rootViewController!.presentViewController(vc, animated: true, completion: nil)
        return true
    }

3 个答案:

答案 0 :(得分:4)

在AppDelegate中使用自定义网址方法,请参阅此处Custom URL

    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
    UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
    [navController presentModalViewController:controller animated:YES];

    return YES;        
}

答案 1 :(得分:1)

您可以在Appdelegate.m文件中覆盖此方法以处理URL

(BOOL)application:(UIApplication *)application openURL:(NSURL *)url     sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    //jump to view controll 2
    return YES;
}

答案 2 :(得分:0)

调用架构URL时,可以添加一些标签 例如: TestSafari:// firstController TestSafari:// secondController

然后你在appDelegate中处理这个:

- (BOOL) application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
if ([url.absoluteString isEqualToString:@"TestSafari://firstController"]) {
    //PresentYour first Controller;
}
else if ([url.absoluteString isEqualToString:@"TestSafari://secondController"]){
    // present your second contoller
}
return YES;
}

请告诉我这是否有助于您:)