0

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

谢谢!!

这就是我在我的 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)。

4

1 回答 1

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 传递给该对象

我希望它对你有帮助。

于 2013-05-08T05:23:17.507 回答