Skip to content

Instantly share code, notes, and snippets.

@TakashiNakagawa
Created February 27, 2013 06:07
Show Gist options
  • Save TakashiNakagawa/5045588 to your computer and use it in GitHub Desktop.
Save TakashiNakagawa/5045588 to your computer and use it in GitHub Desktop.
#import "MyWebViewController.h"
#import "AFNetworking.h"
#if false // facebook
#define LOGIN_URL @"http://sample/login.php?command=facebook&app=true"
#define REDIRECT_URL @"command=facebook_cb"
#else
#define LOGIN_URL @"http://sample/login.php?command=twitter&app=true"
#define REDIRECT_URL @"command=twitter_cb"
#endif
@interface MyWebViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation MyWebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSURL *url = [NSURL URLWithString:LOGIN_URL]; // 文字列をURLとして使える形式に変換
NSURLRequest *req = [NSURLRequest requestWithURL:url]; // URLからリクエストを作る
[self.webView loadRequest:req]; // Webサーバにリクエスト実施!!
self.webView.delegate=self;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
}
-(void) webViewDidFinishLoad:(UIWebView *)webView{
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *url = [[request URL] absoluteString];
NSLog(@"%@", url);
NSRange searchResult = [url rangeOfString:REDIRECT_URL];
if(searchResult.location == NSNotFound){
// みつからない場合の処理
return YES;
}else{
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.frame = CGRectMake(100, 100, 50, 50);
[self.webView addSubview:indicator];
[indicator startAnimating];
indicator.color = [UIColor redColor];
__weak MyWebViewController *wself = self;
NSURL *nsurl = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:nsurl];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
NSLog(@"%@", JSON);
[wself dismissModalViewControllerAnimated:YES];
[indicator removeFromSuperview];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
[wself dismissModalViewControllerAnimated:YES];
[indicator removeFromSuperview];
}];
NSOperationQueue *queue = [[NSOperationQueue alloc] init] ;
[queue addOperation:operation];
return NO;
}
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment