Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set my web view loaded with already login user -iPhone

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.

like image 840
iOS dev Avatar asked Jan 24 '12 12:01

iOS dev


People also ask

How to Add WebView?

To add a WebView to your app, you can either include the <WebView> element in your activity layout, or set the entire Activity window as a WebView in onCreate() .

What is a WebView and how do you Add one to your layout?

The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.

How to use JavaScript in WebView Android?

You have to first register the JavaScriptInterface on your webview. JavaScriptInterFace can be a inner class as shown below. This class will have a function that you can call from html page( via javaScript ) and inside this function you can write code to change activity.


1 Answers

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];
    }
}
like image 129
Jorge Cohen Avatar answered Sep 27 '22 21:09

Jorge Cohen