Can an iOS App Switch to Safari Without Opening a Page?
Asked Answered
H

1

1

I know that my app can open a particular URL in Safari by doing something like this:

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

but, is there any way to have my app switch over to Safari without opening a URL?

I'd like to switch to Safari, but let it keep showing whatever page it had open the last time the user looked at it.

Hemo answered 25/7, 2011 at 19:26 Comment(0)
D
3

Unfortunately no, unless you can figure out how to launch an app by bundle id in a non-jailbroken environment.

Otherwise, if you are in a jailbroken environment, you can use the following to launch an app by its bundle id:

Usage:

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

Code:

#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];
    }
}

Note:

Launching the app this way is basically the same as tapping on the Safari icon in SpringBoard. This will only launch into the app, resuming any web session that was previously active.

Drawers answered 25/7, 2011 at 19:51 Comment(4)
I am always getting null class, I think there is some linking problem, can you please leave a tip on how to link with SpringBoard?! I have all the headers in my HeaderSearchPath from github.com/rpetrich/iphoneheaders. Will highly appreciate.Meier
Be more specific? NULL class? Make sure you import each class used in the above code.Drawers
actually this code: objc_getClass("SBUIController"); returns nil. What I am trying do is to access the springboard from another application process (console daemon process), after a bit more reading I think your code will only work when run as substrate dylib? is it correct or any suggestion? thanks.Meier
Yes, this is correct, the process must be run from inside SpringBoard. You can however use CFNotifications to call from your daemon to a dylib.Drawers

© 2022 - 2024 — McMap. All rights reserved.