Can an iPhone xcode application read cookies previ

2019-01-08 18:31发布

Can an iPhone application read cookies previously stored by Safari Mobile?

8条回答
SAY GOODBYE
2楼-- · 2019-01-08 19:15

Here's my utils get/set cookie methods.



+(void)setCookie:(NSString *)key withValue:(NSString *)value {
    NSArray *keys = [NSArray arrayWithObjects:
                     NSHTTPCookieDomain,
                     NSHTTPCookieExpires,
                     NSHTTPCookieName,
                     NSHTTPCookiePath,
                     NSHTTPCookieValue, nil];
    NSArray *objects = [NSArray arrayWithObjects:
                        @"YOURDOMAIN",
                        [NSDate distantFuture],
                        key,
                        @"/",
                        value, nil];    
    NSDictionary *dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:dict];
    NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    [sharedHTTPCookieStorage setCookie:cookie];
}

+(NSString *)getCookie:(NSString *)key {
    NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookies = [sharedHTTPCookieStorage cookiesForURL:[NSURL URLWithString:@"YOURDOMAIN"]];
    NSEnumerator *enumerator = [cookies objectEnumerator];
    NSHTTPCookie *cookie;
    while (cookie = [enumerator nextObject])
    {
        if ([[cookie name] isEqualToString:key]) 
        {
            return [cookie value];
        }
    }
    return nil;
}
查看更多
再贱就再见
3楼-- · 2019-01-08 19:16

There is actually an interesting way if you have access to a server url.

  1. In your app launch the server url with mobile safari.
  2. The target server url reads the cookie and redirects back to an app specific url (myapp://cookie=123)
  3. The app is then switched back and you can read that value from the url handler

It's a little hacky as the app would switch mobile safari and then immediately switch back to the app. But, it is possible.

查看更多
淡お忘
4楼-- · 2019-01-08 19:19

Note that on iOS 8, you're probably better using Safari Password Sharing to solve some of the use cases that give rise to this problem.

This is not directly possible, but with the cooperation of the web site it is possible.

To clarify, the user case is that an Objective C application wants to read the value of a cookie that has been set by a website in mobile safari. (ie. in particular, a UIWebView was not involved in setting the cookie)

Your app should do this:

  1. Launch mobile safari, using [[UIApplication sharedApplication] openURL:url];
  2. The URL should be a special one, eg. http://yourwebsite.com/give-ios-app-the-cookie
  3. On your website, when that url is launched, issue a redirect to your-app-url-scheme:cookievalue= (eg. angrybirds:cookievalue=hh4523523sapdfa )
  4. when your app delegate receives - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation process the url to get the cookie value

Note that you should not do this automatically when the application starts - the user will see the transfer to Mobile Safari and back, which is not a good user experience and Apple will reject your app (Apple also consider this to be "uploading user's personal data to server without their prior consent"). It would be better to do it in response to the user, paying attention to the user experience - eg. wait for the user to hit a "login" button, then do it, and if the user is not logged into your website, http://yourwebsite.com/give-ios-app-the-cookie should show the user the login screen within safari. If the user is logged in you could briefly show a "Automatically logging you in..." screen for a second or two in Safari before redirecting the user back.

There's no way to get this to work with hotmail/gmail/etc of course - it needs to be your own website.

Credit goes to Unique Identifier for both mobile safari and in app in iOS for suggesting this kind of approach.

查看更多
家丑人穷心不美
5楼-- · 2019-01-08 19:25

Because of sandboxing on the iPhone you don't have access to Safari's cookies. You can only access cookies created within your application - by an UIWebView for example.

查看更多
ら.Afraid
6楼-- · 2019-01-08 19:34

Although you have asked the same question twice before, here's one approach not yet mentioned...

This may be a little convoluted, but you can do Greasemonkey-esque things with a UIWebView. Something like this:

I've used this technique to enhance 3rd party pages in an iPhone app, but I'm not sure if it will read cookies from the same place as Safari mobile.

Worth a shot though?

查看更多
可以哭但决不认输i
7楼-- · 2019-01-08 19:34

You might want to check

if ([[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieAcceptPolicy] != NSHTTPCookieAcceptPolicyAlways) {
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];       
}

But apparently NSHTTPCookieStorage does not even hold cookies from the last request in the current application on iOS (rdar://8190706)

查看更多
登录 后发表回答