iOS App可以在不打开页面的情况下切换到Safari吗?

时间:2011-07-25 19:26:48

标签: ios uiapplication openurl

我知道我的应用可以通过执行以下操作在Safari中打开特定的网址:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.example.com/"]];

但是,有没有办法让我的应用切换到Safari 而不用打开网址?

我想切换到Safari,但让它继续显示上次用户查看时打开的任何页面。

1 个答案:

答案 0 :(得分:3)

不幸的是,除非你能弄清楚如何在非越狱环境中通过捆绑包ID启动应用程序。

否则,如果您处于越狱环境中,则可以使用以下内容通过其包ID启动应用程序:

用法:

[self launch:(@"com.apple.mobilesafari")];

代码:

#pragma mark - Launch Application

-(void)launch:(NSString *)bundle {
    Class SBApplicationController = objc_getClass("SBApplicationController");
    id appController = [SBApplicationController sharedInstance];
    NSArray *apps = [appController applicationsWithBundleIdentifier: bundle];
    if ([apps count] > 0) {
        //Wait .5 seconds.. then launch.
        [self performSelector:@selector(launchTheApp:) withObject:[apps objectAtIndex:0] afterDelay: 0.5]; 
    } else {
        id app = [appController applicationWithDisplayIdentifier: bundle];
        if (app) {
            //Wait .5 seconds.. then launch.
            [self performSelector:@selector(launchTheApp:) withObject:app afterDelay: 0.5];
        }
    }
}
-(void)launchTheApp:(id)app {
    Class SBUIController = objc_getClass("SBUIController");
    id uiController = [SBUIController sharedInstance];
    if([uiController respondsToSelector:@selector(animateLaunchApplication:)]) {
        [uiController animateLaunchApplication:app animateDefaultImage:YES];
    } else {
        [uiController activateApplicationAnimated:app];
    }
}

注意:

以这种方式启动应用程序与点击SpringBoard中的Safari图标基本相同。这将仅启动到应用程序,恢复以前处于活动状态的任何Web会话。