2013-05-08 88 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

相關問題