0

現在、メイン ビューとログイン ビューがあります。ログイン ビュー (javascript インジェクションと Web スクレイピング) で認証を処理し、メイン ビューに適切なコンテンツを読み込みます。問題は、メイン ビューにログアウト ボタンを表示したいことです。私がやりたいことは、ログインビュー(ログアウト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 に答える