My use case is such that I want to launch my app from safari and back to SAME SESSION of safari from my application. Step 1:
- Launch my app from safari browser [Able to achieve it successfully]
Step 2:
- Launch safari by maintaing the same session from where the app was launched [In step 1]
How to achieve Step 2?
the code below is to launch safari
NSString *strurl = @"http://www.google.com"
NSURL *url = [NSURL URLWithtring:strurl];
[[UIApplication sharedApplication] openURL:url];
NOTE: the code above will launch the safari for the specified URL.
- How do开发者_如何转开发 I launch the safari by restoring the previous session?
- Is there any possibility of safari sending me the unique ID of the page when I launch an app from it so that when I try to launch safari back from my app, it would be useful.[Just a vague idea].
I think there is definitely a way to accomplish what you are looking for here. Since you say that you have successfully achieved Step 1, it sounds like you have registered a custom URL scheme for your application, and set up a URL in Safari which the user clicks on to open your application. Let’s pretend your custom URL scheme is myApp://
in which case all you would need to do is embed whatever information your application needs to know about where to send Safari as part of that URL, something like myapp://mysite.com/page/to/display
Then, in your app delegate’s application:openURL:sourceApplication:annotation:
method, you can look at the URL that was used to open your application, and store away the part you need to tell Safari to return to, mysite.com/page/to/display
in this example. (If you are using a version of iOS older than 4.2, then the simpler method application:handleOpenURL:
will be called instead.)
So let’s assume that in this method we store the URL we were given in a property openedURL
as follows (this code goes in your application delegate):
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
self.openedURL = openURL;
}
Then when you want to relaunch Safari, just use the saved information to open Safari to the right place. You need to swap out the myApp:
scheme with http:
so that it gets sent to Safari:
NSURL *safariURL = [[NSURL alloc] initWithScheme:@"http"
host:[openedURL host]
path:[openedURL path]];
[[UIApplication sharedApplication] openURL:[safariURL autorelease]];
If the URL you are opening needs to send other information to your application, which seems likely, then you need to do something a bit more complex, but you can still embed the page you want to go back to in Safari as a query parameter in the URL or something like that: myApp://process?arg1=foo,arg2=bar,safariContext=mysite.com/page/to/display
(with proper URL encoding, of course).
Does that make sense?
精彩评论