1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
#import "ThirdPartyLoginController.h" #import "UserCenter.h"
@interface ThirdPartyLoginController ()
@end
@implementation ThirdPartyLoginController
@synthesize weiboLoginWeb; @synthesize indicatorView; @synthesize authParams; @synthesize appRedirectURI; @synthesize sinaWeiboRequest;
@synthesize userID; @synthesize accessToken; @synthesize expirationDate; @synthesize refreshToken;
@synthesize previousOrientation;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"微博登录"; } return self; }
- (void)viewDidLoad { [super viewDidLoad]; weiboLoginWeb.delegate = self; indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray]; indicatorView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; [self.view addSubview:indicatorView]; }
-(void) viewWillAppear:(BOOL)animated{ self.authParams = [NSMutableDictionary dictionaryWithObjectsAndKeys: kAppKey, @"client_id", @"code", @"response_type", kAppRedirectURI, @"redirect_uri", @"mobile", @"display", nil]; self.appRedirectURI = kAppRedirectURI; self.sinaWeiboRequest = [[SinaWeiboRequest alloc] init]; sinaWeiboRequest.delegate = self; [self show]; }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
#pragma mark - SinaWeiboRequestDelegate Methods - (void)request:(SinaWeiboRequest *)request didReceiveResponse:(NSURLResponse *)response{ NSLog(@"response = %@",response);
} - (void)request:(SinaWeiboRequest *)request didReceiveRawData:(NSData *)data{ NSLog(@"data = %@",data);
} - (void)request:(SinaWeiboRequest *)request didFailWithError:(NSError *)error{ NSLog(@"error = %@",error); }
- (void)request:(SinaWeiboRequest *)request didFinishLoadingWithResult:(id)result{ NSLog(@"result = %@",result); if([UserCenter saveWeiboAuth:result]){ [self.navigationController popViewControllerAnimated:YES]; } }
#pragma mark - Activity Indicator - (void)showIndicator { [indicatorView sizeToFit]; [indicatorView startAnimating]; indicatorView.center = weiboLoginWeb.center; }
- (void)hideIndicator { [indicatorView stopAnimating]; }
#pragma mark - Show / Hide - (void)load{ NSString *authPagePath = [SinaWeiboRequest serializeURL:kSinaWeiboWebAuthURL params:authParams httpMethod:@"GET"]; [weiboLoginWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:authPagePath]]]; }
- (void)show { [self load]; [self showIndicator]; [self addObservers]; }
- (void)hide { [self removeObservers]; [weiboLoginWeb stopLoading]; }
- (void)cancel { [self hide]; }
#pragma mark - UIDeviceOrientationDidChangeNotification Methods - (void)deviceOrientationDidChange:(id)object { }
#pragma mark Obeservers - (void)addObservers { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; }
- (void)removeObservers{ [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIDeviceOrientationDidChangeNotification" object:nil]; }
#pragma mark - UIWebView Delegate
- (void)webViewDidFinishLoad:(UIWebView *)aWebView{ [self hideIndicator]; }
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ [self hideIndicator]; }
- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ NSString *url = request.URL.absoluteString; NSString *siteRedirectURI = [NSString stringWithFormat:@"%@%@", kSinaWeiboSDKOAuth2APIDomain, appRedirectURI]; if ([url hasPrefix:appRedirectURI] || [url hasPrefix:siteRedirectURI]){ NSString *error_code = [SinaWeiboRequest getParamValueFromUrl:url paramName:@"error_code"]; if (error_code){ NSString *error = [SinaWeiboRequest getParamValueFromUrl:url paramName:@"error"]; NSString *error_uri = [SinaWeiboRequest getParamValueFromUrl:url paramName:@"error_uri"]; NSString *error_description = [SinaWeiboRequest getParamValueFromUrl:url paramName:@"error_description"]; NSDictionary *errorInfo = [NSDictionary dictionaryWithObjectsAndKeys: error, @"error", error_uri, @"error_uri", error_code, @"error_code", error_description, @"error_description", nil]; NSLog(@"errorInfo = %@",errorInfo); [self hide]; }else{ NSString *code = [SinaWeiboRequest getParamValueFromUrl:url paramName:@"code"]; if (code){ [self hide]; NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: kAppKey, @"client_id", kAppSecret, @"client_secret", @"authorization_code", @"grant_type", self.appRedirectURI, @"redirect_uri", code, @"code", nil]; [sinaWeiboRequest disconnect]; sinaWeiboRequest = nil; self.sinaWeiboRequest = [SinaWeiboRequest requestWithURL:kSinaWeiboWebAccessTokenURL httpMethod:@"POST" params:params delegate:self]; [sinaWeiboRequest connect]; } } return NO; } return YES; }
- (void)removeAuthData{
}
- (BOOL)isLoggedIn { return userID && accessToken && expirationDate; }
- (BOOL)isAuthorizeExpired { NSDate *now = [NSDate date]; return ([now compare:expirationDate] == NSOrderedDescending); }
- (BOOL)isAuthValid { return ([self isLoggedIn] && ![self isAuthorizeExpired]); } @end
|