Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

uiapplication - Can an iOS App Switch to Safari Without Opening a Page?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...