在浏览器中启动应用 post 身份验证
Launching app post authentication in browser
所以我在我的 iOS 应用程序中启动浏览器 (UIWebView
) 以进行身份验证。用户键入用户名和密码,身份验证请求将发布到身份提供程序服务器。它在同一个浏览器 window 中发回响应。现在,在身份验证成功后,我想显示应用程序的第一个屏幕。
这就是我在我的浏览器 UIWebview
代码中尝试的
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSURL *url = webView.request.URL;
if([url.absoluteString containsString:@"DONEWITHLOGIN"]){
// Store cookies
}
// ?? How do I call app delegate from here to launch first screen
// load custom URL scheme
}
这就是我 AppDelegate
现在
正在做的事情
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
#if __has_feature(objc_arc)
self.window = [[UIWindow alloc] initWithFrame:screenBounds];
#else
self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease];
#endif
self.window.autoresizesSubviews = YES;
#if __has_feature(objc_arc)
self.viewController = [[MainViewController alloc] init];
#else
self.viewController = [[[MainViewController alloc] init] autorelease];
#endif
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// Call Authentication Method - this will launch a webview with URL for authentication
return YES;
}
- (BOOL)(UIApplication *)application openURL:(NSURL *)url sourceApplication annotation:(id) annotation
{
// I return here once the custom URL scheme is loaded.
// Other code
self.viewController = [[[MainViewController alloc] init] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
我收到错误提示,试图将 NavigationController 呈现给 ViewController,其视图不在层次结构中。错误来自行 self.window.rootViewController = self.viewController
.
我正在为此应用程序使用 Cordova。
我会通过相反的方式来解决这个问题。您的 MainViewController
是应用程序的主视图。如果用户未登录,则它会显示一个带有登录名 window 的模态视图。登录完成后,模态视图将自行关闭并以适当的方式存储凭据以允许应用程序的其余部分正常运行。
您似乎对 iOS 非常陌生,所以我会在这里为您提供一些帮助。首先,你永远不应该跳回 App Delegate 来重置视图。你想像你目前所做的那样在你的应用程序委托中加载 MainViewController 。其余代码如下!
MainViewController.m
#import "MainViewController.h"
#import "WebViewController.h"
@interface MainViewController () <WebViewControllerDelegate>
@end
@implementation MainViewController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if(...userHasAlreadyFilledOutProfileScreen...){
[self showLoginScreen:NO];
} else {
... show profile screen ...
}
}
- (void)showLoginScreen:(BOOL)animated
{
WebViewController *webViewController = [[WebViewController alloc] init];
webViewController.delegate = self;
[self presentViewController:webViewController animated:animated completion:^{
NSLog(@"Web View Loaded!");
}];
}
- (void)showFirstScreenOfApp
{
... code to show first screen of app ...
}
- (void)whenUserFinishesProfileScreen
{
...
[self showLoginScreen:YES];
...
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
WebViewController.h
#import <UIKit/UIKit.h>
@class WebViewController;
@protocol WebViewControllerDelegate <NSObject>
@optional
- (void)showFirstScreenOfApp;
@end
@interface WebViewController : UIViewController
@property (nonatomic, assign) id<WebViewControllerDelegate> delegate;
@property (nonatomic, strong) UIWebView* webView;
@end
WebViewController.m
@implementation WebViewController
- (void)viewDidLoad
{
}
- (void)viewDidAppear:(BOOL)animated
{
}
- (void)didLoginCorrectly
{
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"Closed Login WebView");
}];
}
- (void)viewWillDisappear:(BOOL)animated
{
if(...didYouLoginCorrectlySoYouShouldShowFirstScreen... && [self.delegate respondsToSelector:@selector(showFirstScreenOfApp)]){
[self.delegate showFirstScreenOfApp];
}
}
@end
这是我们正在做的事情
- 在 AppDelegate 中加载 MainViewContoller
- 检查他们是否填写了个人资料。如果是这样,请先显示登录屏幕而不显示动画,这样它看起来像是第一件事
- 如果他们没有填写个人资料页面...设置
- 当他们完成个人资料页面时,我们将代理设置为 MainViewController 并显示登录页面
- 当用户正确登录时,它会关闭 Login WebViewController。
- 在视图消失之前,它会检查您是否正确登录,如果是,则告诉代理显示登录用户可以访问的应用程序的第一个屏幕
“...”之间的所有代码都是您需要填写的内容,因为我无法帮助您:)
希望这对您有所帮助...尽情享受吧!
所以我在我的 iOS 应用程序中启动浏览器 (UIWebView
) 以进行身份验证。用户键入用户名和密码,身份验证请求将发布到身份提供程序服务器。它在同一个浏览器 window 中发回响应。现在,在身份验证成功后,我想显示应用程序的第一个屏幕。
这就是我在我的浏览器 UIWebview
代码中尝试的
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSURL *url = webView.request.URL;
if([url.absoluteString containsString:@"DONEWITHLOGIN"]){
// Store cookies
}
// ?? How do I call app delegate from here to launch first screen
// load custom URL scheme
}
这就是我 AppDelegate
现在
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
#if __has_feature(objc_arc)
self.window = [[UIWindow alloc] initWithFrame:screenBounds];
#else
self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease];
#endif
self.window.autoresizesSubviews = YES;
#if __has_feature(objc_arc)
self.viewController = [[MainViewController alloc] init];
#else
self.viewController = [[[MainViewController alloc] init] autorelease];
#endif
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// Call Authentication Method - this will launch a webview with URL for authentication
return YES;
}
- (BOOL)(UIApplication *)application openURL:(NSURL *)url sourceApplication annotation:(id) annotation
{
// I return here once the custom URL scheme is loaded.
// Other code
self.viewController = [[[MainViewController alloc] init] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
我收到错误提示,试图将 NavigationController 呈现给 ViewController,其视图不在层次结构中。错误来自行 self.window.rootViewController = self.viewController
.
我正在为此应用程序使用 Cordova。
我会通过相反的方式来解决这个问题。您的 MainViewController
是应用程序的主视图。如果用户未登录,则它会显示一个带有登录名 window 的模态视图。登录完成后,模态视图将自行关闭并以适当的方式存储凭据以允许应用程序的其余部分正常运行。
您似乎对 iOS 非常陌生,所以我会在这里为您提供一些帮助。首先,你永远不应该跳回 App Delegate 来重置视图。你想像你目前所做的那样在你的应用程序委托中加载 MainViewController 。其余代码如下!
MainViewController.m
#import "MainViewController.h"
#import "WebViewController.h"
@interface MainViewController () <WebViewControllerDelegate>
@end
@implementation MainViewController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if(...userHasAlreadyFilledOutProfileScreen...){
[self showLoginScreen:NO];
} else {
... show profile screen ...
}
}
- (void)showLoginScreen:(BOOL)animated
{
WebViewController *webViewController = [[WebViewController alloc] init];
webViewController.delegate = self;
[self presentViewController:webViewController animated:animated completion:^{
NSLog(@"Web View Loaded!");
}];
}
- (void)showFirstScreenOfApp
{
... code to show first screen of app ...
}
- (void)whenUserFinishesProfileScreen
{
...
[self showLoginScreen:YES];
...
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
WebViewController.h
#import <UIKit/UIKit.h>
@class WebViewController;
@protocol WebViewControllerDelegate <NSObject>
@optional
- (void)showFirstScreenOfApp;
@end
@interface WebViewController : UIViewController
@property (nonatomic, assign) id<WebViewControllerDelegate> delegate;
@property (nonatomic, strong) UIWebView* webView;
@end
WebViewController.m
@implementation WebViewController
- (void)viewDidLoad
{
}
- (void)viewDidAppear:(BOOL)animated
{
}
- (void)didLoginCorrectly
{
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"Closed Login WebView");
}];
}
- (void)viewWillDisappear:(BOOL)animated
{
if(...didYouLoginCorrectlySoYouShouldShowFirstScreen... && [self.delegate respondsToSelector:@selector(showFirstScreenOfApp)]){
[self.delegate showFirstScreenOfApp];
}
}
@end
这是我们正在做的事情
- 在 AppDelegate 中加载 MainViewContoller
- 检查他们是否填写了个人资料。如果是这样,请先显示登录屏幕而不显示动画,这样它看起来像是第一件事
- 如果他们没有填写个人资料页面...设置
- 当他们完成个人资料页面时,我们将代理设置为 MainViewController 并显示登录页面
- 当用户正确登录时,它会关闭 Login WebViewController。
- 在视图消失之前,它会检查您是否正确登录,如果是,则告诉代理显示登录用户可以访问的应用程序的第一个屏幕
“...”之间的所有代码都是您需要填写的内容,因为我无法帮助您:)
希望这对您有所帮助...尽情享受吧!