【问题标题】:Running a WebView Request In Another View, While Displaying a Different View?在另一个视图中运行 WebView 请求,同时显示不同的视图?
【发布时间】:2013-05-02 05:45:44
【问题描述】:

我目前有一个主视图和一个登录视图。我在我的登录视图中处理身份验证(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)。

【问题讨论】:

    标签: iphone ios objective-c uiviewcontroller uiwebview


    【解决方案1】:

    是的,当然你可以使用 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 传递给该对象

    希望对你有帮助。

    【讨论】:

    • 感谢您回复 Kamarshad!那么对于第 1 步,您会说在我的 loginView 或我的 webViewDidStartLoad 的 viewDidLoad 中输入第一行吗?
    • @user1886754 是的,你应该知道,但是告诉我loginview 是否存在于内存中?
    • 每个都是一个视图控制器。 LoginViewController 是一个模态视图。我糊涂了。所以发布通知应该放在我的登录视图还是我的主视图中?
    • postNotification 将进入mainView 内部,您点击logOut buttonaddObserver 将进入LoginView
    猜你喜欢
    • 1970-01-01
    • 2010-12-21
    • 2019-11-10
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多