我创建了一个应用程序,它登录到一个简单的UIWebview登录页面,然后在加载的网站上进行身份验证。问题是,我希望我的应用程序在网站上进行验证,并且一旦通过身份验证,我希望它将我的应用程序重定向到我在Xcode中创建的视图。我对iphone编程很新。 任何帮助,将不胜感激。 我到目前为止所编写的代码也包含cookie,它看起来如下:
为我的APP代表:
- (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
---------------");
}
为我的登录视图提供COOKIES:
-(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
答案 0 :(得分:1)
如果身份验证成功,请让服务器重定向到某个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;
}
并且,在app delegate中:
- (void)authenticationSuccessful {
[[[self.window subviews] objectAtIndex:0] removeFromSuperview];
[self.window addSubview:navigationController.view];
}