



#define URL_SCHEME @"iostestapp"
#define CLIENT_ID @"6ef1c6dc6f134a2daa67cc905e5c1a3d"
#import <UIKit/UIKit.h>
#import "YandexOauthViewController.h"
@interface ViewController : UIViewController <YandexOauthViewControllerDelegate>
@end - (void) viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
YandexOauthViewController * cntrl = [[YandexOauthViewController alloc] init];
cntrl.delegate = self;
[self.navigationController presentModalViewController: cntrl animated: YES];
}
- (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions
{
self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName: @ "ViewController" bundle: nil];
UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController: self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}

- (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType
{
// Get the URL
NSURL * url = [request URL];
// Check for compliance with custom URL scheme
if ([url.scheme isEqualToString: URL_SCHEME])
{
// remove the network activity indicator
UIApplication * app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = NO;
// parse the URL into separate elements
// our token will be in the arr array at index 2
NSArray * arr = [[url description] componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @ "# = &"]];
// tell the delegate about successful authorization and pass the token
if ([delegate respondsToSelector: @selector (yandexOauthViewController: succesfullLoginWithToken :)])
{
[delegate yandexOauthViewController: self succesfullLoginWithToken: [arr objectAtIndex: 2]];
}
// do not allow UIWebView to open URL
return NO;
}
// allow UIWebView to navigate by URL
return YES;
}
- (void) yandexOauthViewController: (YandexOauthViewController *) controller
succesfullLoginWithToken: (NSString *) token
{
[self.navigationController dismissModalViewControllerAnimated: YES];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Authorization succeeded"
message: [NSString stringWithFormat: @ "Token% @", token]
delegate: nil
cancelButtonTitle: @ "Ok"
otherButtonTitles: nil];
[alert show];
}

Source: https://habr.com/ru/post/131773/
All Articles