xcode 如何设置我的 web 视图加载了已经登录的用户 -iPhone
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8986963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set my web view loaded with already login user -iPhone
提问by iOS dev
In my app iam using web view with URL:@"http://www.gmail.com/".
在我的应用程序中,我使用带有 URL:@"http://www.gmail.com/" 的 web 视图。
This web view was loaded when i clicked a button in the main page / home page
(IBAction)webClick:(id)sender { MailViewController *mail = [[MailViewController alloc]initWithNibName:@"MailViewController" bundle:nil]; [self.navigationController pushViewController:mail animated:YES]; }
Then the web view was loaded, i used code like thin this in mail view:
-(void)viewDidLoad { [super viewDidLoad]; NSString *urlAddress = @"http://www.gmail.com/"; NSURL *url = [NSURL URLWithString:urlAddress]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestObj]; }
当我单击主页/主页中的按钮时加载了此 Web 视图
(IBAction)webClick:(id)sender { MailViewController *mail = [[MailViewController alloc]initWithNibName:@"MailViewController" bundle:nil]; [self.navigationController pushViewController:mail animated:YES]; }
然后加载了 web 视图,我在邮件视图中使用了像这样的代码:
-(void)viewDidLoad { [super viewDidLoad]; NSString *urlAddress = @"http://www.gmail.com/"; NSURL *url = [NSURL URLWithString:urlAddress]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestObj]; }
Here the gmail opened with login page. we need to enter username & password.
在这里,gmail 打开了登录页面。我们需要输入用户名和密码。
What i want is,
我想要的是,
if we already login into my account through gmail application..
The loaded view directly loades my mail, instead of login page.
if i didn't already loged in, then show an alert as please login.
如果我们已经通过 gmail 应用程序登录到我的帐户..
加载的视图直接加载我的邮件,而不是登录页面。
如果我还没有登录,那么显示一个警告,请登录。
How to do it?
怎么做?
Please help me. Thanks in advance.
请帮我。提前致谢。
回答by Jorge Cohen
First of all, I think the URL you should be loading is http://mail.google.com/mail
首先,我认为你应该加载的 URL 是 http://mail.google.com/mail
Other than that, you're not getting normal gmail behavior because UIWebView does not save cookies between app runs, you should try something like this to save them:
除此之外,由于 UIWebView 不会在应用程序运行之间保存 cookie,因此您无法获得正常的 gmail 行为,您应该尝试使用以下方法来保存它们:
- (void)saveCookies
{
NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: cookiesData forKey: @"cookies"];
[defaults synchronize];
}
and load them back using this:
并使用此加载它们:
- (void)loadCookies
{
NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @"cookies"]];
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in cookies)
{
[cookieStorage setCookie: cookie];
}
}