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

Iphone 在另一个视图中运行WebView请求,同时显示另一个视图?,iphone,ios,objective-c,uiviewcontroller,uiwebview,Iphone,Ios,Objective C,Uiviewcontroller,Uiwebview,我目前有一个主视图和一个登录视图。我在登录视图中处理身份验证(javascript注入和web抓取),然后用正确的内容加载主视图。问题是我想在主视图上显示一个注销按钮。我希望它做的是向loginview(注销url)中的uiwebview发送一个加载url请求。我想在不显示loginview的情况下执行此操作。这可能吗 谢谢 这就是我在LoginView中所做的: - (void)webViewDidFinishLoad:(UIWebView *) webView { if ( [

我目前有一个主视图和一个登录视图。我在登录视图中处理身份验证(javascript注入和web抓取),然后用正确的内容加载主视图。问题是我想在主视图上显示一个注销按钮。我希望它做的是向loginview(注销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];

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

当然可以,您可以使用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

  }
 //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];
2)发布通知

 //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

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


希望对您有所帮助。

谢谢您回复卡马沙德!因此,对于步骤1,您会说在我的loginView或我的webViewDidStartLoad的viewDidLoad中输入第一行吗?@user1886754是的,您应该这样做,但请让我知道
loginView
是否存在于内存中?每个都是一个视图控制器。LoginViewController是一个模式视图。我很困惑。因此,发布通知应该进入我的登录视图还是我的主视图?
postNotification
将进入
mainView
中,从那里单击
logOut按钮
addObserver
将进入
loginview