當您在用戶輸入其憑據時調用Fitbit認證時,fitbit將調用您的回調URL。所以 你需要像這樣處理你的應用程序委託中的回調。
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
NSString *strUrl =[NSString stringWithFormat:@"%@",url];
if ([strUrl containsString:@"#_=_"]) {
[self saveFitbitCode:url];
return YES;
}
}
現在saveFitbitCode方法,我檢索AUTH_CODE然後發送本地通知,以便它會調用另一功能的訪問令牌。
-(void)saveFitbitCode:(NSURL *)url
{
NSString *strUrl =[NSString stringWithFormat:@"%@",url];
NSRange range1 = [strUrl rangeOfString:@"code="];
NSRange range2 = [strUrl rangeOfString:@"#_=_"];
NSRange rSub = NSMakeRange(range1.location + range1.length, range2.location - range1.location - range1.length);
NSString *sub = [strUrl substringWithRange:rSub];
[[NSUserDefaults standardUserDefaults] setObject:sub forKey:@"auth_code"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:@"callRequest" object:nil userInfo:nil];
}
我希望這會幫助你。