2013-07-18 160 views
0

我閱讀Facebook文檔並找到一些登錄Facebook的方法。如何登錄與嵌入式網頁視圖登錄對話框與FB SDK 3.5.x的在嵌入式WebView登錄對話框中登錄facebook應用

Link facebook docs here

+0

[Rü問任何問題?或提供解決方案如何使用Facebook。 – Romance

+0

我的意思是如何使用嵌入式webview在我的應用中登錄Facebook,而不是使用Safari或Facebook原生應用。 – John

+0

請檢查下面的代碼 – Romance

回答

0

這可能是有用的,以你和Facebook的一些委託方法也顯示下方

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url 
    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{ 
    return [facebook handleOpenURL:url]; 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    // Although the SDK attempts to refresh its access tokens when it makes API calls, 
    // it's a good practice to refresh the access token also when the app becomes active. 
    // This gives apps that seldom make api calls a higher chance of having a non expired 
    // access token. 
    [[self facebook] extendAccessTokenIfNeeded]; 
} 

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 
{ 
    NSLog(@"url->%@",url); 
    return [self.facebook handleOpenURL:url]; 
} 
- (BOOL)isFBLoggedIn 
{ 
    return [facebook isSessionValid]; 
} 

- (void)login 
{ 
    if (![facebook isSessionValid]) 
    { 
     [facebook authorize:[[NSArray alloc] initWithObjects:@"offline_access", nil]]; 

    } else 
    { 
     [facebook logout]; 
     [facebook authorize:[[NSArray alloc] initWithObjects:@"offline_access", nil]]; 
    } 
} 
- (void)fetchFBFriends 
{ 
    [facebook requestWithGraphPath:@"me/friends" andDelegate:self]; 
} 
-(void)fetchFbDetails 
{ 
    [facebook requestWithGraphPath:@"me" andDelegate:self]; 
} 
- (void)storeAuthData:(NSString *)accessToken expiresAt:(NSDate *)expiresAt 
{ 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:accessToken forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:expiresAt forKey:@"FBExpirationDateKey"]; 
    [defaults synchronize]; 
} 

    NSString *strRes = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/me?fields=name,username,email,gender,birthday,hometown,picture&access_token=%@",facebook.accessToken]] usedEncoding:&encoding error:&error]; 

    NSMutableDictionary *dirFbDetails = [[NSMutableDictionary alloc] initWithDictionary:[strRes JSONValue]]; 

    -(void)fbDidExtendToken:(NSString *)accessToken expiresAt:(NSDate *)expiresAt 
    { 
     NSLog(@"token extended"); 
     [self storeAuthData:accessToken expiresAt:expiresAt]; 
    } 
    -(void)doFaceBookLogin 
    { 
     [self.facebook authorize:[NSArray arrayWithObjects:@"friends_birthday",@"user_birthday",@"email",@"publish_stream",nil]]; 

    } 
    #pragma mark - FBRequestDelegate Methods 

    - (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response 
    { 

    } 
    - (void)request:(FBRequest *)request didLoad:(id)result 
    { 
    } 

    - (void)request:(FBRequest *)request didFailWithError:(NSError *)error 
    { 


     NSLog(@"Err message: %@", [[error userInfo] objectForKey:@"error_msg"]); 
     NSLog(@"Err code: %d", [error code]); 
    } 
+0

謝謝,但我想說的是使用FB sdk 3.5.x for iOS,這似乎與您的答案不一致。 – John

+0

@PhamTuyen檢查編輯的代碼 – Romance