-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainViewController.m
More file actions
126 lines (115 loc) · 4.23 KB
/
MainViewController.m
File metadata and controls
126 lines (115 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// MainViewController.m
// JavaScript
//
// Created by Ansel on 12-11-27.
// Copyright (c) 2012年 Ansel. All rights reserved.
//
/*
Added by hubing 20121202
*/
#import "MainViewController.h"
#include <sys/sysctl.h>
#include <mach/mach.h>
@interface MainViewController ()
@end
@implementation MainViewController
@synthesize JSwebView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"test String :%@",test);
JSwebView.backgroundColor = [UIColor clearColor];
JSwebView.scalesPageToFit = YES;
JSwebView. delegate = self;
NSURL * url = [[NSURL alloc] initWithString: @"http://www.google.com.hk/m?gl=CN&hl=zh_CN&source=ihp" ];
NSURLRequest * request = [[NSURLRequest alloc] initWithURL:url];
[JSwebView loadRequest:request];
[url release];
[request release];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3
target:self
selector:@selector(onTimer:)
userInfo:nil
repeats:YES];
// Do any additional setup after loading the view from its nib.
}
#pragma mark - ----------UIWebView代理方法------------
- ( void )webViewDidFinishLoad:(UIWebView * )webView {
NSString * currentURL = [webView stringByEvaluatingJavaScriptFromString: @"document.location.href" ];
NSString * title = [webView stringByEvaluatingJavaScriptFromString: @"document.title" ];
NSLog(@"\ncurrentURL is:%@,\nTitle is:%@",currentURL,title);
NSString * js_result = [webView stringByEvaluatingJavaScriptFromString: @"document.getElementsByName('q')[0].value='赵发凯'; " ];
NSLog(@"js_result is:%@",js_result);
// NSString * js_result2 = [webView stringByEvaluatingJavaScriptFromString: @" document.forms[0].submit(); "];
// NSLog(@"js_result2 is:%@",js_result2);
// [webView stringByEvaluatingJavaScriptFromString: @" var script = document.createElement('script'); "
// " script.type = 'text/javascript'; "
// " script.text = \"function myFunction() { "
// " var field = document.getElementsByName('q')[0]; "
// " field.value='赵发凯'; "
// " document.forms[0].submit(); "
// " }\"; "
// " document.getElementsByTagName('head')[0].appendChild(script); " ];
//
// [webView stringByEvaluatingJavaScriptFromString: @" myFunction(); " ];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//MARK: 可用内存
- (double)availableMemory
{
vm_statistics_data_t vmStats;
mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
kern_return_t kernReturn = host_statistics(mach_host_self(),HOST_VM_INFO,(host_info_t)&vmStats,&infoCount);
if(kernReturn != KERN_SUCCESS)
{
return NSNotFound;
}
return ((vm_page_size * vmStats.free_count) / 1024.0) / 1024.0;
}
//MARK: 已使用内存
- (double)usedMemory
{
task_basic_info_data_t taskInfo;
mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
kern_return_t kernReturn = task_info(mach_task_self(),
TASK_BASIC_INFO, (task_info_t)&taskInfo, &infoCount);
if(kernReturn != KERN_SUCCESS) {
return NSNotFound;
}
return taskInfo.resident_size / 1024.0 / 1024.0;
}
//MARK: 总的内存为
-(double)totalMemory{
task_basic_info_data_t taskInfo;
mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
kern_return_t kernReturn = task_info(mach_task_self(),
TASK_BASIC_INFO, (task_info_t)&taskInfo, &infoCount);
if(kernReturn != KERN_SUCCESS) {
return NSNotFound;
}
return taskInfo.resident_size / 1024.0 / 1024.0;
}
- (void)onTimer:(id)sender
{
NSLog(@" 使用内存 %f 剩余内存 %f",[self usedMemory],[self availableMemory]);
}
-(void)setTest:(NSString *) newtest{
if(test == newtest){
[test release];
}
test = [newtest retain];
}
@end