我正在尝试使用UIWeb-View登录页面将控制权传递给另一个UIViewController

时间:2015-12-01 07:21:16

标签: ios objective-c cookies uiwebview

我正在使用此代码设置cookie和UIWeb-View进行登录,并且我试图将控制权传递给另一个UIViewcontroller,如果登录成功已满但控制权没有传递到另一个视图

(void)viewDidLoad
{
  [super viewDidLoad];

  AppDelegate *app = [[UIApplication sharedApplication]delegate];

  NSString *urlString = @"http://streamtvbox.com/site/wp-login.php";
  NSURL *url = [NSURL URLWithString:urlString];

  NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];

  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

  [request setHTTPShouldHandleCookies:YES];
  [self addCookies:cookies forRequest:request];
  [self.webView loadRequest:request];
  [self Login];

}


(void)addCookies:(NSArray *)cookies forRequest:(NSMutableURLRequest *)request
{

 if ([cookies count] == 0)
 {
     NSString *cookieHeader = nil;
     AppDelegate *app = [[UIApplication sharedApplication]delegate];
     NSHTTPCookie *cooki;
     app.cookie = cooki;
     for (cooki in cookies)
     { 
        if (!cookieHeader)
        {

            cookieHeader = [NSString stringWithFormat: @"%@=%@",[app.cookie name],[app.cookie value]];

        }
        else
        {

            cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[app.cookie name],[app.cookie value]];
         }

}

if (cookieHeader)
{

     [request setValue:cookieHeader forHTTPHeaderField:@"Cookie"];

}
  } 

}

 -(void)Login

 {

   Service *srv=[[Service alloc]init];

   NSString *str=@"http://streamtvbox.com/site/api/matrix/";
   NSString *method=@"channels";
   NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];

   [srv postToURL:str withMethod:method andParams:dict completion:^(BOOL success, NSDictionary *responseObj)

  {

      if (success) {

               TabBarController *TabBar = [self.storyboard instantiateViewControllerWithIdentifier:@"TabBar"];
               [self.navigationController pushViewController:TabBar animated:YES];

       }
       else
       {
             [self Login1];
       }
}];
}

1 个答案:

答案 0 :(得分:0)

查看相关活动必须在主线程中完成。像这样更改你的代码。

dispatch_async(dispatch_get_main_queue(), ^
{
    TabBarController *TabBar = [self.storyboard instantiateViewControllerWithIdentifier:@"TabBar"];
    [self.navigationController pushViewController:TabBar animated:YES];
});