Are cookies in UIWebView accepted?(UIWebView 中的 cookie 是否被接受?)
问题描述
我要问你.
1:我在我的 iPhone 应用程序中使用 UIWebView
s.我不希望用户能够在新闻中添加评论.但是,要发表评论,他们必须登录.
1 : I'm using UIWebView
s in my iPhone App. I wan't the users be able to add comments in the news. But, to comment they have to log-in.
如果没有,我如何在 UIWebView
s 中接受 cookie?
If not, how can I accept cookies in UIWebView
s ?
2:在 UIWebView
上创建的 cookie 在其他 UIWebView
中是否可用?
2 : Are the cookies created in on UIWebView
available in others UIWebView
in an other View ?
例如:我有我的 LoginViewController
,带有嵌入式 UIWebView
,我的用户可以在其中登录/注销.如果他们在此视图中登录,该 cookie 仍将在 CommentViewController
中可用?
Ex : I have my LoginViewController
, with an embedded UIWebView
, where my user can login/logout. If they log-in in this view, the cookie will be still available in the CommentViewController
?
如果没有,我怎样才能做到这一点?
If not, how can I make this possible ?
提前致谢!
推荐答案
UIWebView
会自动将cookies 存储在[NSHTTPCookieStorage sharedHTTPCookieStorage]
集合中,并且应该可用在您的应用内的所有其他 UIWebView
中,在同一个应用启动期间.但是,UIWebView
类不会自动为在应用启动之间加载的页面存储 cookie.您需要在应用移入后台时手动存储 cookie,并在应用移回前台时重新加载这些值.
The UIWebView
will automatically store the cookies in the [NSHTTPCookieStorage sharedHTTPCookieStorage]
collection, and should be available in all other UIWebView
s within your app, during the same app launch. However the UIWebView
class does not automatically store cookies for the pages that are loaded between app launches. You need to manually store cookies when the app is moved into the background and reload the values when the app is brought back into the foreground.
将以下代码放入您的 AppDelegate 类中:
Place the following code in your AppDelegate class:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Other existing code
[self loadHTTPCookies];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//Other existing code
[self saveHTTPCookies];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[self loadHTTPCookies];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
//Other existing code
[self saveHTTPCookies];
}
-(void)loadHTTPCookies
{
NSMutableArray* cookieDictionary = [[NSUserDefaults standardUserDefaults] valueForKey:@"cookieArray"];
for (int i=0; i < cookieDictionary.count; i++)
{
NSMutableDictionary* cookieDictionary1 = [[NSUserDefaults standardUserDefaults] valueForKey:[cookieDictionary objectAtIndex:i]];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieDictionary1];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
}
-(void)saveHTTPCookies
{
NSMutableArray *cookieArray = [[NSMutableArray alloc] init];
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[cookieArray addObject:cookie.name];
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:cookie.name forKey:NSHTTPCookieName];
[cookieProperties setObject:cookie.value forKey:NSHTTPCookieValue];
[cookieProperties setObject:cookie.domain forKey:NSHTTPCookieDomain];
[cookieProperties setObject:cookie.path forKey:NSHTTPCookiePath];
[cookieProperties setObject:[NSNumber numberWithUnsignedInteger:cookie.version] forKey:NSHTTPCookieVersion];
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
[[NSUserDefaults standardUserDefaults] setValue:cookieProperties forKey:cookie.name];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[[NSUserDefaults standardUserDefaults] setValue:cookieArray forKey:@"cookieArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
这篇关于UIWebView 中的 cookie 是否被接受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIWebView 中的 cookie 是否被接受?
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01