打开特定选项卡,并在收到推送通知时在uiwebview中启动特定网址

时间:2014-01-09 09:26:40

标签: ios iphone objective-c xcode uiwebview

我正在开发一个将接收推送通知的应用程序,当用户点击通知视图时,应用程序启动并且其中有3个uitabs并编入索引为0,1和2.我的所需标签,当收到通知时是第二个当标签1打开时,它会检测到如果此标签打开以响应推送通知,那么UIWebview将启动另一个URL。否则它将作为默认值。我发布了我的所有代码,请帮助我。在此先感谢:)

这是我的didReceiveRemotenotification方法,我正在创建并保存一个整数作为标志来做出决定。   - AppDelegate.m

(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    int flg = 1;
    [[NSUserDefaults standardUserDefaults] setInteger:flg forKey:@"flagvlu"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
}

然后它加载应用程序并打开第一个视图,即Main.m   - Main.m

(void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotificationReceived) name:@"pushNotification" object:nil];
}
-(void)pushNotificationReceived{
    [self.tabBarController setSelectedIndex:1];
}

并将app传递给下一个标签(Required.m),这是我需要的标签。我有一个uiwebview。

Required.m

- (void)viewDidLoad
{
  [self doYourStuff];
  [super viewDidLoad];
}
-(void)doYourStuff{
  int flg = [[NSUserDefaults standardUserDefaults] integerForKey:@"flagvlu"];
   if (flg == 1) {
     [webPage reload];
     [webPage loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
     flg = 0;
     [[NSUserDefaults standardUserDefaults] setInteger:flg forKey:@"flagvlu"];
     [[NSUserDefaults standardUserDefaults] synchronize];
   }
   else if(flg == 0){
     [webPage reload];
     [webPage loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.yahoo.com"]]];
   }
}

*现在的问题是应用会收到通知,当用户点击视图时,它会启动应用并完美打开所需的标签但不会在URL中启动所需的webview。 :(请帮助我在目标c开发中是新的。

2 个答案:

答案 0 :(得分:1)

执行所需操作的一种方法是在doYourStuff而不是viewWillAppear中调用viewDidLoad

在视图创建时调用

viewDidLoad,通常是在将视图作为子视图添加到另一个视图时。

viewWillAppear在视图可见时调用。

因此,填充标签栏时会调用viewDidLoad;当您使该视图可见时(即,通过点击其标签栏图标),将调用viewWillAppear

IMO使用tab bar delegate method的更好方法viewWillAppear

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

   if (<viewController is Required.m>)
       [(Required.m*)viewController doYourStyff];
}

每次点击标签栏项时都会调用一次。

答案 1 :(得分:0)

在不使用NSNotificationCenter的情况下尝试此操作。

- (void)application:(UIApplication *)application  didReceiveRemoteNotification:(NSDictionary *)userInfo
{


        int flg = 1;
    [[NSUserDefaults standardUserDefaults] setInteger:flg forKey:@"flagvlu"];
    [[NSUserDefaults standardUserDefaults] synchronize];

   UITabBarController *tabb = (UITabBarController *)self.window.rootViewController;
   tabb.selectedIndex = 1;

}

现在在viewwillappear方法中正确 doYourStuff

相关问题