我正在为网站开发本机应用程序。该应用程序基本上是网站的包装器,它实现了推送通知等功能。单击推送通知时,将使用以下代码转到相应的页面:
- (NSString *)handlePush:(NSDictionary *)notification
{
if ([[notification objectForKey:@"aps"] objectForKey:@"badge"]) {
int badgeNum = [[[notification objectForKey:@"aps"] objectForKey:@"badge"] integerValue];
NSLog(@"Badge: %i, got from %@", badgeNum, [[notification objectForKey:@"aps"] objectForKey:@"badge"]);
[UIApplication sharedApplication].applicationIconBadgeNumber = badgeNum;
}
if (!active) {
NSLog(@"Got noti while not active, going to that chat!");
NSString *hash;
if ((hash = [notification objectForKey:@"h"])) {
NSLog(@"Hash: %@", [hash stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
return [NSString stringWithFormat:@"#%@", hash];
}
return @"";
}
return nil;
}
Active 在应用程序进入后台和恢复后更改,以确保在用户使用应用程序时推送通知到达时不会触发它。该 URL 已正确解析,因为如果我在浏览器中手动粘贴完全相同的 URL,我会转到正确的页面。
我100%确定委托已设置,因为该UIWebView: ShouldStartLoadWithRequest
方法被调用:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *url = [[request URL] absoluteString];
NSLog(@"shouldStartLoadWithRequest called with url %@", url);
if ([url hasPrefix:BASE_URL]) {
NSLog(@"Yea, i'll load this one for ya");
// remove any get-params / hash suffix
NSRange r = [url rangeOfString:@"?"];
if (r.location == NSNotFound)
r = [url rangeOfString:@"#"];
if (r.location != NSNotFound)
url = [url substringToIndex:r.location];
if (![url isEqualToString:[defaults valueForKey:@"baseUrl"]]) {
NSLog(@"setting new baseUrl %@", url);
[defaults setValue:url forKey:@"baseUrl"];
[defaults synchronize];
}
NSLog(@"Should go and load it now...");
return YES;
}
}
那里有一些缓存网页版本的逻辑。我通过断点逐步完成,它到达了该return YES
部分,并调用了那里的日志。然而,在同一个委托中,didStartLoad
并didFailLoadWithError
没有被调用,它包含纯粹的 NSLogs。
在最初的应用程序启动时,这确实有效,并且它也有一次我使用断点单步执行了很长时间,所以这似乎是一些时间问题。希望不必使用任何像计时器这样的hacky解决方案,我希望这里的任何人都有类似问题的经验,或者有任何其他有价值的输入。