Iphone 通过webview进行身份验证后,如何重定向应用程序以显示应用程序视图

Iphone 通过webview进行身份验证后,如何重定向应用程序以显示应用程序视图,iphone,cocoa-touch,uiwebview,Iphone,Cocoa Touch,Uiwebview,我制作了一个应用程序,它登录到一个简单的UIWebview登录页面,然后在加载的网站上进行身份验证。问题是,我希望我的应用程序在网站上进行身份验证,一旦它通过身份验证,我希望它将我的应用程序重定向到我在Xcode中创建的视图。我对iphone编程相当陌生。 任何帮助都将不胜感激。 到目前为止,我编写的代码还包含cookies,如下所示: 对于我的应用程序代理: - (BOOL)application:(UIApplication *)application didFinishLaunchingW

我制作了一个应用程序,它登录到一个简单的UIWebview登录页面,然后在加载的网站上进行身份验证。问题是,我希望我的应用程序在网站上进行身份验证,一旦它通过身份验证,我希望它将我的应用程序重定向到我在Xcode中创建的视图。我对iphone编程相当陌生。 任何帮助都将不胜感激。 到目前为止,我编写的代码还包含cookies,如下所示:

对于我的应用程序代理:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    [self initPreferences];


  // Override point for customization after application launch.
 NSArray *siteArray = [[NSArray alloc] initWithObjects:@"http://...com",
        @"http://....com",
        @"http://...com",
        @"http://...com",
        @"http://.com",
        nil];
    SignonWebView *webView = [[SignonWebView alloc] initWithURL:[siteArray objectAtIndex:2]];

 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
 [nc addObserver:self selector:@selector(cookieChange:) name:@"NSHTTPCookieManagerCookiesChangedNotification" object:nil];

 if ([Constants useAuthenticator])
 {
  [[self window] addSubview:[webView view]];
 }
 else
 {
  [self.window addSubview:navigationController.view];  
 }
   // [self.window makeKeyAndVisible];



    // Check for device

 if (![self deviceConnected])
 {
  [Constants setConnectedToDevice:NO];
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ALERT!" message:@"Device Not Detected" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  [alert show];
  [alert autorelease];
 }
 else
 {
  if ([Constants isLineaPro])
  {
   NSLog(@"Init LineaPro device");
  }
  else if ([Constants isVerifone])
  {
   NSLog(@"Init Verifone device");
   DeviceControl *dc = [DeviceControl sharedInstance]; // Singleton initializes device
   [[dc payControl] hostPowerEnabled:YES];
   NSLog(@"---------------------- Sending message to MSR");
   [[dc pinPad] sendStringCommand:@"Q40" calcLRC:YES];

  }

 }


 //AdminViewController = [[AdminViewController alloc] init];


    [self.window makeKeyAndVisible];
 //[self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:0.933 green:0.114 blue:0.149 alpha:1]];

 //[[UIApplication sharedApplication] setupScreenMirroringOfMainWindow:navigationController framesPerSecond:20];

 //[[UIApplication sharedApplication] setupScreenMirroringWithFramesPerSecond:20];

    return YES;

 //[self.parentViewController.view setHidden:YES];

}

-(void)cookieChange:(NSHTTPCookieStorage *)somethin
{
 NSLog(@"----------- Cookies have changed sucessfully
---------------");

}
对于包含cookie的“我的登录”视图:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
 NSLog(@"shouldStartLoadWithRequest");
 return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
 NSLog(@"webViewDidStartLoad");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
 NSLog(@"***************** webViewDidFinishLoad --- getting all cookies");
 NSMutableArray *cookieList = [[NSMutableArray alloc] init];
 NSHTTPCookie *cookie;
 for (cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
  [cookieList addObject:cookie];
//  NSLog(@"%@ expire: %@\n%@", [cookie name],[cookie expiresDate],cookie);
 }
 NSLog(@"Number of cookies is %d",[cookieList count]);
 if (initialLoad)
 {
  NSLog(@"---- removing existing cookies -----");
  for (NSHTTPCookie *currentCookie in cookieList)
  {
   NSLog(@"Removing cookie : %@",[currentCookie name]);
   //[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:currentCookie]; 
  }
  initialLoad = NO;
 }
 else
 {
  for (NSHTTPCookie *currentCookie in cookieList)
  {
   NSLog(@"='%@'",currentCookie);
   if ([[currentCookie name]isEqual:@"UserID"]) {
    // we have a user id returned from services, so save it
    [self setUserIDCookie:currentCookie];
    NSLog(@"  -- userIDCookie : %@",[[self userIDCookie] value]);
    [self setUserID:([[self userIDCookie] value])];
   }
   if ([[currentCookie name]isEqual:@"UserName"]) {
    // we have a user id returned from services, so save it
    [self setUserNameCookie:currentCookie];
    NSLog(@"  -- userNameCookie : %@",[[self userNameCookie] value]);
    [self setUserName:([[self userNameCookie] value])];
   }
  }
 }
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
 NSLog(@"didFailLoadWithError : %@",error);
}
#pragma mark -
#pragma mark Actions
-(void)loadUrl:(NSString*)URL
{
 [myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:URL]]];
}
@end

如果身份验证成功,请将服务器重定向到某个URL。然后,在webView:shouldStartLoadWithRequest:navigationType:中,检查该URL,并在加载时移动到其他视图

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"shouldStartLoadWithRequest");
    if([[[request URL] absoluteString] isEqualToString:@"http://authSuccess.website.com"]) {
        [[[UIApplication sharedApplication] delegate] authenticationSuccessful];
        return NO;
    }
    return YES;
}
并且,在应用程序代理中:

- (void)authenticationSuccessful {
    [[[self.window subviews] objectAtIndex:0] removeFromSuperview];
    [self.window addSubview:navigationController.view];
}

如果身份验证成功,请将服务器重定向到某个URL。然后,在webView:shouldStartLoadWithRequest:navigationType:中,检查该URL,并在加载时移动到其他视图

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"shouldStartLoadWithRequest");
    if([[[request URL] absoluteString] isEqualToString:@"http://authSuccess.website.com"]) {
        [[[UIApplication sharedApplication] delegate] authenticationSuccessful];
        return NO;
    }
    return YES;
}
并且,在应用程序代理中:

- (void)authenticationSuccessful {
    [[[self.window subviews] objectAtIndex:0] removeFromSuperview];
    [self.window addSubview:navigationController.view];
}

请尝试格式化您的帖子,使其更具可读性。请尝试格式化您的帖子,使其更具可读性。