gpt4 book ai didi

ios - 无法使用 'ios:' .concat(data) 在 iOS 的 Web View 中从服务器调用数据

转载 作者:行者123 更新时间:2023-11-29 11:49:34 25 4
gpt4 key购买 nike

我正在创建一个应用程序,它加载页面的 WebView 以从 WebView 获取数据并将数据保存到 iOS 应用程序中的本地 SQLite 数据库。目前面临的问题是,虽然 WebView 提醒数据,但我无法在 Web View Delegate 中接收数据,即:我尝试在 Web View Delegate 区域的日志中打印甚至它都没有显示。

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>

网页 View 加载

- (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 View 委托(delegate)

- (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;
}

信息.plist enter image description here

最佳答案

在您的 Webview Load 中添加以下代码

webView.delegate = self

例如:

网页 View 加载

- (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];
}

关于ios - 无法使用 'ios:' .concat(data) 在 iOS 的 Web View 中从服务器调用数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41758183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com