2013-05-08 86 views
0

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

谢谢!

这是我在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]; 

我想然后发送到登录视图。什么是完成这个最好的方法? (请记住,稍后我将使用钥匙串代替NSuserdefaults)。

回答

0

是offcourse这是可能的,你可以通过使用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到该对象

我希望它对你有帮助。

+0

感谢您回复Kamarshad!所以对于第1步,你会说我的loginView或我的webViewDidStartLoad的viewDidLoad中输入第一行? – KingPolygon 2013-05-08 05:30:14

+0

@ user1886754是的,你应该,但让我知道'loginview'存在于内存中吗? – Kamarshad 2013-05-08 05:35:22

+0

每个都是视图控制器。 LoginViewController是一个模态视图。我很困惑。所以邮政通知,应该在我的loginview或我的主视图? – KingPolygon 2013-05-08 05:51:15

相关问题