如何设置我的 Web 视图已加载已登录用户 -iPhone

How to set my web view loaded with already login user -iPhone(如何设置我的 Web 视图已加载已登录用户 -iPhone)

本文介绍了如何设置我的 Web 视图已加载已登录用户 -iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用带有 URL 的 Web 视图:@http://www.gmail.com/".

  • 当我点击主页/主页中的按钮时,该网页视图已加载

    (IBAction)webClick:(id)sender{MailViewController *mail = [[MailViewController alloc]initWithNibName:@"MailViewController" bundle:nil];[self.navigationController pushViewController:邮件动画:YES];}

  • 然后加载 web 视图,我在邮件视图中使用了这样的代码:

    -(void)viewDidLoad{[超级视图DidLoad];NSString *urlAddress = @"http://www.gmail.com/";NSURL *url = [NSURL URLWithString:urlAddress];NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];[webView loadRequest:requestObj];}

这里的 gmail 打开了登录页面.我们需要输入用户名 &密码.

我想要的是,

  • 如果我们已经通过 gmail 应用程序登录到我的帐户..

    加载的视图直接加载我的邮件,而不是登录页面.

  • 如果我还没有登录,请在登录时显示警报.

怎么做?

请帮助我.提前致谢.

解决方案

首先,我认为你应该加载的 URL 是 http://mail.google.com/mailp>

除此之外,由于 UIWebView 不会在应用程序运行之间保存 cookie,因此您没有获得正常的 gmail 行为,您应该尝试这样的方法来保存它们:

- (void)saveCookies{NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];[默认设置对象:cookiesData forKey:@cookies"];[默认同步];}

并使用以下方法将它们加载回来:

- (void)loadCookies{NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @"cookies"]];NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];for (NSHTTPCookie *cookie 中的cookie){[cookieStorage setCookie: cookie];}}

In my app iam using web view with URL:@"http://www.gmail.com/".

  • 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];
    }
    

Here the gmail opened with login page. we need to enter username & password.

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.

How to do it?

Please help me. Thanks in advance.

解决方案

First of all, I think the URL you should be loading is 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:

- (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];
    }
}

这篇关于如何设置我的 Web 视图已加载已登录用户 -iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何设置我的 Web 视图已加载已登录用户 -iPhone

基础教程推荐