【问题标题】:Can an iOS App Switch to Safari Without Opening a Page?iOS 应用可以在不打开页面的情况下切换到 Safari 吗?
【发布时间】:2011-07-25 19:26:48
【问题描述】:

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

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

但是,有什么方法可以让我的应用在不打开 URL 的情况下切换到 Safari?

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

【问题讨论】:

    标签: ios uiapplication openurl


    【解决方案1】:

    很遗憾,没有,除非您能弄清楚如何在非越狱环境中通过捆绑 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 图标相同。这只会在应用程序中启动,恢复之前处于活动状态的任何网络会话。

    【讨论】:

    • 我总是得到空类,我认为有一些链接问题,你能留下关于如何与 SpringBoard 链接的提示吗?!我的 HeaderSearchPath 中有来自github.com/rpetrich/iphoneheaders 的所有标题。将不胜感激。
    • 更具体?空类?确保导入上述代码中使用的每个类。
    • 其实这段代码:objc_getClass("SBUIController");返回零。我正在尝试做的是从另一个应用程序进程(控制台守护进程)访问跳板,经过更多阅读后,我认为您的代码只有在作为基板 dylib 运行时才能工作?这是正确的还是有什么建议?谢谢。
    • 是的,这是正确的,该过程必须从 SpringBoard 内部运行。但是,您可以使用 CFNotifications 从您的守护进程调用 dylib。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多