1

I wanted to know how I could go about passing a Variable (cookie) in a local HTML file in my iOS App, and pass what the cookie says onto a UILabel. Here is what the HTML code is. I need to pass this message into a UILabel

<script language="javascript">setCookie('Test','You Sir have succeeded. Congratulations.', 300);
 </script>

And here is my updated code for iOS app

Header File:

import

@interface ViewController : UIViewController {

IBOutlet UIWebView *webView;
IBOutlet UILabel *mylabel;


}

  @end

Main File

#import "ViewController.h"

@interface ViewController ()

@end

 @implementation ViewController

- (void)viewDidLoad
{

    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"DTPContent/cookietest" ofType:@"html"]isDirectory:NO]]];

    [super viewDidLoad];

    NSString *myCookie = [self->webView stringByEvaluatingJavaScriptFromString:@"getCookie('Test')"]; self->mylabel.text = myCookie;

    }

    @end

Thank You in Advance!

Mina Dawoud
  • 408
  • 8
  • 22

1 Answers1

1

Assuming you already have a javascript function to get the cookies, like that, you can simply execute the javascript with webview's method stringByEvaluatingJavaScriptFromString , get it's returned string and set it in a UILabel.

It will be something like this:

NSString *myCookie = [self.webView stringByEvaluatingJavaScriptFromString:@"getCookie('Test')"];
self.mylabel.text = myCookie;
Community
  • 1
  • 1
Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
  • I updated my code that i put in the app, After connecting outlets, The Uilabel is just blank – Mina Dawoud Aug 20 '13 at 03:05
  • 1
    Have you pasted the javascript function `getCookie()` inside your HTML? If it's blank, probably the javascript method its not been correctly called. I suggest you to first test it with a javascript console in a desktop web browser. When you succeed in get the cookies with javascript inside the console, just post the same code in the ios method `stringByEvaluatingJavaScriptFromString`. – Lucas Eduardo Aug 20 '13 at 03:26