问题
I'm not talking about a UIWebView
, I want to open the mobile safari app using an NSURLRequest
.
Why? In my app, the user has already logged into our web server. I want to give the user the option of launching the webpage without having to reenter their credentials. However, I can't use a GET
because that would put their credentials in the URL.
So, can I open the safari app (not a UIWebView
) from my app with an NSURLRequest
or a POST
in some other form?
回答1:
I'm going to put my neck on the line and say no — there's no way to pass an NSURLRequest
directly to Safari. The (primary) inter-application communication medium on iOS is to access URLs; there's no general mechanism for passing objects from one application to another.
I think the best you're going to be able to do is to fetch a one-time key from your web server in the app, then pop into Safari with that in the URL. And, if you can, do that via HTTPS so that the URL path and query components are only visible once TLS negotiation has succeeded. That should stop anybody else from seeing the relevant credentials and stop them from being useful even if they're obtained by some other means (such as somebody next to you copying the URL from your screen).
回答2:
It would be simpler to use an NSURL, or just an NSString, or just a char* for that matter. I'm not sure if that works on the iPhone, it should, since the iPhone runs parallel with Darwin just like OSX, but there are some restrictions to that on the iPhone. This definitely works on a mac though.
NSString *url = @"http://www.apple.com";
NSURL *myURL = [NSURL URLWithString:url];
system([ [NSString
stringWithFormat:@"Open -a Safari %@",
[NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:NULL] ]
cStringUsingEncoding:NSUTF8StringEncoding]);
Also, I might suggest that that's a bad idea (if it might be avoided). As an iPhone user, I'm sure I'm not alone in finding it very irritating when developers take me away from the app I'm currently in.
来源:https://stackoverflow.com/questions/9880922/ios-is-it-possible-to-open-nsurlrequest-directly-in-safari