我正在创建一个应用程序,该应用程序加载页面的Web视图以从Web视图获取数据并将数据保存到iOS应用程序中的本地SQLite数据库中。当前面临的问题是,尽管Web视图警告了数据,但我无法在Web View委托中接收数据,即:我试图在Web View委托区域中的日志中打印,甚至没有显示。
URL : http://35.161.118.210/list/12345.html
服务器页面数据
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
setInterval(function(){
var data = "Notice 1:::Notice 2:::Notice 3:::Notice 4:::Notice 5:::Notice 6";
if(data.length > 5){
window.location = 'ios:'.concat(data);
$('#notice').text("Unread Notice");
alert(data);
}
}, 5000);
</script>
Web视图加载
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-64)];
NSString *htmlFile = [[NSUserDefaults standardUserDefaults] objectForKey:USER_CODE];
NSURL *url = [NSURL URLWithString:htmlFile];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
[self.view addSubview:webView];
}
Web视图委托
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *strResponse = [[request URL] absoluteString];
NSLog(@"Value retured : %@",strResponse);
if ([strResponse hasPrefix:@"ios:"]) {
NSString *newString = [strResponse substringFromIndex:[@"ios:" length]];
newString = [newString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Value retured : %@",newString);
UIAlertView *uiAlert = [[UIAlertView alloc] initWithTitle:@"Message" message:newString delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[uiAlert show];
NSArray *arrItems = [newString componentsSeparatedByString:@":::"];
for (NSString *strItems in arrItems) {
[strItems stringByRemovingPercentEncoding];
NSMutableDictionary *mDic = [[NSMutableDictionary alloc] init];
[mDic setObject:strItems forKey:@"item_name"];
[mDic setObject:@"0" forKey:@"is_deleted"];
[[DataBase connection] insertTableData:mDic AndTableName:@"school_feed"];
}
return NO;
}
return YES;
}
info.plist
最佳答案
在您的Webview加载中添加以下代码webView.delegate = self
例如 :
Web视图加载
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-64)];
NSString *htmlFile = [[NSUserDefaults standardUserDefaults] objectForKey:USER_CODE];
NSURL *url = [NSURL URLWithString:htmlFile];
webView.delegate = self;
[webView loadRequest:[NSURLRequest requestWithURL:url]];
[self.view addSubview:webView];
}