在另一个视图中运行WebView请求,同时显示不同的视图?

时间:2013-05-08 05:17:02

标签: iphone ios objective-c uiviewcontroller uiwebview

我目前有一个主视图和一个登录视图。我在登录视图(javascript注入和Web抓取)中处理身份验证,然后使用正确的内容加载我的主视图。问题是我想在主视图上显示一个注销按钮。我想要它做的是在loginview(logout url)中向uiwebview发送一个加载URL请求。我想在不显示loginview的情况下这样做。这可能吗?

谢谢!

这就是我在LoginView中所做的事情:

- (void)webViewDidFinishLoad:(UIWebView *) webView {


     if ( [[NSURL URLWithString:@"https://arandomservice.com/signin"] isEqual:[NSURL URLWithString:[WebView stringByEvaluatingJavaScriptFromString: @"document.URL"]]] ) {

         NSLog(@"Authentication Successful");

         //Save Username
         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
         [defaults setObject:usernameField.text forKey:@"username"];
         [defaults synchronize];

         //Save Password
         [defaults setObject:passwordField.text forKey:@"password"];
         [defaults synchronize];

         //Close Login Screen
         [self dismissViewControllerAnimated:YES completion:NULL];
     }

     else {

         NSLog(@"Authentication Failed.");
     }


}

现在稍后,当我希望用户退出时,我希望他们在另一个视图中点击一个uibutton,它将执行以下操作:

NSURL *signInUrl = [NSURL URLWithString:@"https://arandomservice.com/logout"];
    [self loadURL:nil withURL:signInUrl];

我想将其发送到登录视图。实现这一目标的最佳方法是什么? (PS请记住,稍后我将使用钥匙串而不是NSuserdefaults。)

1 个答案:

答案 0 :(得分:0)

是的,可以通过使用NSNotification来实现 看看如何

1)设置通知

 //write this line of code at where you created `LoginView(loginview)`
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logOut) name:@"kLogoutButtonClicked" object:nil];

 - (void)logOut:(NSNotification*)notification{

   NSURL * signInUrl = (NSURL*)[notification object];
   //now use this URL further
   //here write you javaScript and inject it to the Webview

  }

2)发布通知

 //as you clicked logOut button in otherView just  write below line of code this line of code broadcast notification to each observer  and don't forget to remove this observer as doen with it in any right place like `dealloc`.

 [[NSNotificationCenter defaultCenter] postNotificationName:@"kLogoutButtonClicked" object:herePassWhatYouwant];

//在您的情况下将URL传递给该对象

我希望对你有所帮助。