我想知道如何在我的iOS应用程序中将变量(cookie)传递到本地HTML文件中,并将cookie所说的内容传递给UILabel。这是HTML代码。我需要将此消息传递给UILabel
<script language="javascript">setCookie('Test','You Sir have succeeded. Congratulations.', 300);
</script>
这是我更新的iOS应用代码
标题文件:
@interface ViewController:UIViewController {
IBOutlet UIWebView *webView;
IBOutlet UILabel *mylabel;
}
@end
主文件
#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
提前谢谢你!
答案 0 :(得分:1)
假设你已经有一个javascript函数来获取cookie,比如that,你只需用webview的方法stringByEvaluatingJavaScriptFromString
执行javascript,得到它的返回字符串并在UILabel中设置它。
会是这样的:
NSString *myCookie = [self.webView stringByEvaluatingJavaScriptFromString:@"getCookie('Test')"];
self.mylabel.text = myCookie;