2014-08-28 71 views
0

我的应用程序是webview。 正常载入页面http://staging.nhomxe.vn。 但是,当服务器发送通知,并附加了一个链接。 Webview将打开此链接。ios上的错误webview

我的申请活动正常。然后,我将导航控制器添加到ViewControler。 当服务器发送通知时,我的webview通知错误[vc.webView loadRequest:urlRequest]; ,和应用程序自动退出。

我的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound)]; 
UILocalNotification *localNotif = 
[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 

if (localNotif) { 
    // launched from notification 
    NSLog(@"Co notify!!!"); 
    NSString *message = [localNotif valueForKey:@"link"]; 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setValue:message forKey:@"LINK"]; 

} else { 
    // from the springboard 
    NSLog(@"Khong co notify!!!"); 

} 
return YES; 
} 



- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 

} 
- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
NSLog(@"My token: %@",deviceToken); 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
[defaults setValue:deviceToken forKey:@"TOKEN"]; 
} 

- (void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 
{ 
NSLog(@"Error: %@",error); 
} 


- (void)applicationWillResignActive:(UIApplication *)application 
{ 
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 

- (void)application:(UIApplication*)application didReceiveRemoteNotification: 
(NSDictionary*)userInfo 
{ 
NSURL *url ; 
NSLog(@"Received notification: %@", userInfo); 
NSDictionary *data = [ userInfo objectForKey:@"aps"]; 
for(NSString *key in data) { 
    NSString *info = [data objectForKey:key]; 
    NSLog(@"thong tin nhan dc: %@ : %@", key, info); 
} 

NSString *message = [userInfo valueForKey:@"link"] ; 
//NSArray *info = [message componentsSeparatedByString:@"&@"]; 
//NSString *body = [info objectAtIndex:0]; 
//NSString *link = [info objectAtIndex:1]; 
NSLog(@"Thong tin Link: %@",message); 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
[defaults setValue:message forKey:@"LINK"]; 
ViewController *vc = (ViewController *)self.window.rootViewController; 
if(message == NULL) 
{ 
    url = [NSURL URLWithString:@"http://staging.nhomxe.vn"]; 
}else 
{ 
    url = [NSURL URLWithString:message]; 
} 

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
[vc.webView loadRequest:urlRequest]; 
[vc.webView3 loadRequest:urlRequest]; 


} 

@end 

我的错误:

2014-08-28 14:51:59.374 NhomXe[30379:907] Thong tin Link: http://staging.nhomxe.vn/org/instance_message/conversation-detail.xhtml?post=13001628&[email protected] 
2014-08-28 14:52:14.950 NhomXe[30379:907] -[UINavigationController webView]: unrecognized selector sent to instance 0x1d5c8350 
2014-08-28 14:52:14.958 NhomXe[30379:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController webView]: unrecognized selector sent to instance 0x1d5c8350' 
*** First throw call stack: 
(0x314e22a3 0x3913e97f 0x314e5e07 0x314e4531 0x3143bf68 0xd687f 0x3353d585 0x3353dfa5 0x33f53305 0x314b7173 0x314b7117 0x314b5f99 0x31428ebd 0x31428d49 0x34fa52eb 0x3333e301 0xd6ad9 0x39575b20) 
libc++abi.dylib: terminate called throwing an exception 

回答

0

你RootViewController的正在返回其被用来管理导航堆栈一个UINavigationController,由视图控制器的阵列表示。

为了让您的视图控制器尝试获取topViewController,例如:

UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController; 

ViewController *vc = (ViewController *)navigationController.topViewController; 

您还可以得到导航管理由所有视图控制器的阵列:

NSLog(@"%@", navigationController.viewControllers); 

更新你的UI上主线程:

dispatch_async(dispatch_get_main_queue(), ^{ 
    // make sure vc is a weak refernace to avoid retain cycle 
    [vc.webView loadRequest:urlRequest]; 
    [vc.webView3 loadRequest:urlRequest]; 
}); 
+0

我使用ViewController * vc =(ViewController *)self.window.rootViewCon troller.modalViewController; 我的应用程序不通知错误。但webView不会从通知重新加载链接。 – Takeshi 2014-08-28 08:51:41

+0

看看网站是否已初始化。 – 2014-08-28 09:02:52

+0

我的webview不活动。它只显示curent页面。 – Takeshi 2014-08-28 09:15:46