2010-12-26 12 views
15

के साथ फेसबुक-आईओएस-एसडीके मैं नए फेसबुक-आईओएस-एसडीके के साथ काम कर रहा हूं और एपीआई को अपने मूल ऐप में सफलतापूर्वक एकीकृत कर चुका हूं। मैं आईओएस-एसडीके कक्षाओं के साथ एक पॉपअप संवाद का उपयोग कर उपयोगकर्ता को प्रमाणीकृत करने और उचित रूप से सेटअप अनुमतियों को सक्षम करने में सक्षम हूं।एम्बेडेड UIWebView

मेरे ऐप के एक हिस्से के लिए मुझे वेबदृश्य के भीतर डेटा को संसाधित करने के लिए जावास्क्रिप्ट और एचटीएमएल का उपयोग करके UIWebView के भीतर फेसबुक कनेक्शन का उपयोग करने की आवश्यकता है। यह देखते हुए कि उपरोक्त दिनचर्या के माध्यम से उपयोगकर्ता पहले ही लॉग इन और प्रमाणीकृत है, मुझे लगता है कि UIWebView उन प्रमाण-पत्रों को साझा करेगा, या कम से कम वेबव्यू को प्रमाण-पत्र पास करने या असाइन करने का कोई तरीका होगा।

दुर्भाग्य से, मुझे यह पिछली पोस्ट मिली जो ऐसा लगता है कि यह योजना काफी काम नहीं करती है (iOS - being logged-in in a webView after logging in with the SDK)। क्या किसी और को इसका सामना करना पड़ा है और/या आसपास एक काम मिला है? ऐसा लगता है कि यह एक काफी सरल उपयोग केस होगा क्योंकि मैं मोबाइल सफारी या ऐसा कुछ लॉन्च करने की कोशिश नहीं कर रहा हूं - यह सब एक ही देशी ऐप के भीतर है।

ऐसा लगता है कि कुछ प्रकार की आसान चाल या सेटिंग है जो मुझे याद आ रही है। शायद किसी भी तरह नए UIWebView में कुकीज़ सेट कर रहे हैं? या इस तरह का कुछ?

+0

द्वारा सुझाए गए वेब व्यू में एक्सेस टोकन का उपयोग करेंगे, यह मेरी समस्या भी है, लेकिन ऐसा लगता है कि उत्तर के लिए कोई उम्मीद नहीं है :( –

+4

लगभग 2 साल बाद, मेरे पास एक ही समस्या। इस बीच कोई समाधान? – Claus

उत्तर

0

एक बार आपके पास FB access टोकन है, तो आप अपने वेब दृश्य में FaceboookJavaScript API का उपयोग करने में सक्षम होना चाहिए। आधिकारिक दस्तावेज़ीकरण के लिए http://developers.facebook.com/docs/reference/javascript/ देखें। यह SO पोस्ट उचित स्पष्टीकरण देता है: Retrieve Access Token Using Javascript API। जैसा कि पोस्ट का वर्णन करता है, आप सीधे AJAX कॉल में पहुंच टोकन का उपयोग कर सकते हैं। मैं कुकीज़ के साथ गड़बड़ नहीं करता - बस पहुंच टोकन के साथ पास।

आपने वेब व्यू में Facebook के साथ बिल्कुल क्या करना चाहते हैं, यह स्पष्ट नहीं किया है, इसलिए मुझे यकीन नहीं है कि यह आपके प्रश्न का उत्तर 100% है। मुझे लगता है कि आप वेब व्यू (UIWebView'sstringByEvaluatingJavaScriptFromString) तक पहुंच टोकन को पारित करने के बारे में जानते हैं।

+2

एम्बेडेड वेबपृष्ठ के लिए, आप fB.getLoginStatus() पर access_token कैसे पास करते हैं क्योंकि यह कॉलबैक को छोड़कर कोई भी param नहीं लेता है? – jAckOdE

+2

क्या कुछ कोड स्निपेट होना संभव है? – Claus

+0

क्या आपके पास है वास्तव में यह किया? सैद्धांतिक रूप से यह समझ में आता है लेकिन @Claus की तरह मैं वास्तव में एक कोड नमूना पसंद करेंगे। – bogardon

1

--BY इस तरह से फेसबुक-ios-sdk

@property (nonatomic, को बनाए रखने) NSString * जच;

@end 

@implementation SettingsViewController 
@synthesize chk,viewFbLogin; 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell; 
AppDelegate *appDelegate; 
UIViewController *tweetComposer; 
ACAccount *twitterAccount; 
ACAccountStore *account; 
ACAccountType *accountType; 
NSArray *arrayOfAccounts ; 
int arr_row_num; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
      } 
    return self; 
} 


- (void)storeAccountWithAccessToken:(NSString *)token secret:(NSString *)secret 
{ 
    // Each account has a credential, which is comprised of a verified token and secret 
    ACAccountCredential *credential = 
    [[ACAccountCredential alloc] initWithOAuthToken:token tokenSecret:secret]; 

    // Obtain the Twitter account type from the store 
    accountType = 
    [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

    // Create a new account of the intended type 
    twitterAccount = [[ACAccount alloc] initWithAccountType:accountType]; 

    // Attach the credential for this user 
    twitterAccount.credential = credential; 

    // Finally, ask the account store instance to save the account 
    // Note: that the completion handler is not guaranteed to be executed 
    // on any thread, so care should be taken if you wish to update the UI, etc. 
    [account saveAccount:twitterAccount withCompletionHandler:^(BOOL success, NSError *error) { 
     if (success) { 
      // we've stored the account! 
      NSLog(@"the account was saved!"); 
     } 
     else { 
      //something went wrong, check value of error 
      NSLog(@"the account was NOT saved"); 

      // see the note below regarding errors... 
      // this is only for demonstration purposes 
      if ([[error domain] isEqualToString:ACErrorDomain]) { 

       // The following error codes and descriptions are found in ACError.h 
       switch ([error code]) { 
        case ACErrorAccountMissingRequiredProperty: 
         NSLog(@"Account wasn't saved because " 
           "it is missing a required property."); 
         break; 
        case ACErrorAccountAuthenticationFailed: 
         NSLog(@"Account wasn't saved because " 
           "authentication of the supplied " 
           "credential failed."); 
         break; 
        case ACErrorAccountTypeInvalid: 
         NSLog(@"Account wasn't saved because " 
           "the account type is invalid."); 
         break; 
        case ACErrorAccountAlreadyExists: 
         NSLog(@"Account wasn't added because " 
           "it already exists."); 
         break; 
        case ACErrorAccountNotFound: 
         NSLog(@"Account wasn't deleted because" 
           "it could not be found."); 
         break; 
        case ACErrorPermissionDenied: 
         NSLog(@"Permission Denied"); 
         break; 
        case ACErrorUnknown: 
        default: // fall through for any unknown errors... 
         NSLog(@"An unknown error occurred."); 
         break; 
       } 
      } else { 
       // handle other error domains and their associated response codes... 
       NSLog(@"%@", [error localizedDescription]); 
      } 
     } 
    }]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    account = [[ACAccountStore alloc] init]; 
    accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 
    arrayOfAccounts = [account accountsWithAccountType:accountType]; 
    // [self storeAccountWithAccessToken:@"119745010-g6YqvIdpyvBvYuaweR5oI1V1h9ugIqlj0toetlqg" secret:@"ysBO2E6dgOWj2vKEFoC3PCEypMaPrpycy5WIeodUNro"]; 



    appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    chk=appDelegate.chk_login; 

    if (!appDelegate.session.isOpen) { 
     // create a fresh session object 
     appDelegate.session = [[FBSession alloc] init]; 
     if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) { 
      // even though we had a cached token, we need to login to make the session usable 
      [appDelegate.session openWithCompletionHandler:^(FBSession *session, 
                  FBSessionState status, 
                  NSError *error) { 
       // we recurse here, in order to update buttons and labels 

      }]; 
     } 
    } 


    if ([chk isEqualToString:@"YES"] && (appDelegate.setting_flag==FALSE)) 
    { 

     appDelegate.arr=[NSMutableArray arrayWithObjects:@"Change Password",@"Facebook Login",@"Twitter Login",nil]; 

    } 

    else 

    { 
     if(appDelegate.setting_flag==FALSE) 
     appDelegate.arr=[NSMutableArray arrayWithObjects:@"Facebook Login",@"Twitter Login",nil]; 
    } 



} 


-(void) updateView 
{ 



} 
- (void)viewWillAppear:(BOOL)animated 
{ 



} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [appDelegate.arr count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 



    cell = [self.SettingsTable dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(!cell) 
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 




    if([[appDelegate.arr objectAtIndex:indexPath.row] isEqualToString:@"Twitter Login"]) 
    { 




     twitterAccount = [arrayOfAccounts objectAtIndex:0]; 
     NSLog(@"array=%@",[arrayOfAccounts objectAtIndex:0]); 
     NSDictionary *tempDict = [[NSDictionary alloc] initWithDictionary: 
            [twitterAccount dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]]]; 
     NSString *tempUserID = [[tempDict objectForKey:@"properties"] objectForKey:@"user_id"]; 
     NSLog(@"temp=%@",tempUserID); 






     if (!IsEmpty(tempUserID)) 
     { 
      [appDelegate.arr replaceObjectAtIndex:indexPath.row withObject:@"Twitter Logout"]; 
      [email protected]"Twitter Logout"; 
     } 
     else 
     { 
      cell.textLabel.text=[appDelegate.arr objectAtIndex:indexPath.row]; 
     } 



    } 

    else if([[appDelegate.arr objectAtIndex:indexPath.row] isEqualToString:@"Facebook Login"]) 
    { 
     if (appDelegate.session.isOpen) { 
      [appDelegate.arr replaceObjectAtIndex:indexPath.row withObject:@"Facebook Logout"]; 
      cell.textLabel.text=[appDelegate.arr objectAtIndex:indexPath.row]; 
     } 
     else 
     { 
      cell.textLabel.text=[appDelegate.arr objectAtIndex:indexPath.row]; 
     } 


    } 

    else 
    { 

     cell.textLabel.text=[appDelegate.arr objectAtIndex:indexPath.row]; 
    } 




    return cell; 


} 

- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 



    if([[appDelegate.arr objectAtIndex:indexPath.row]isEqualToString:@"Change Password"] && [chk isEqualToString:@"YES"]) 
    { [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
     [UIView setAnimationDuration:0.75]; 



     ForgetPasswordViewController *forgetPassView=[[ ForgetPasswordViewController alloc] initWithNibName:@"ForgetPasswordViewController" bundle:[NSBundle mainBundle]]; 
     NSLog(@"Working table"); 


     [self.navigationController pushViewController:forgetPassView animated:YES]; 
     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO]; 
     [UIView commitAnimations]; 



    } 

    else if([[appDelegate.arr objectAtIndex:indexPath.row]isEqualToString:@"Facebook Login"]) 
    { 
     if (appDelegate.session.state != FBSessionStateCreated) { 
      // Create a new, logged out session. 
      appDelegate.session = [[FBSession alloc] init]; 
     } 
     [appDelegate.session openWithCompletionHandler:^(FBSession *session, 
                 FBSessionState status, 
                 NSError *error) { 
      // and here we make sure to update our UX according to the new session state 
      if (appDelegate.session.isOpen) { 

      [appDelegate.arr replaceObjectAtIndex:indexPath.row withObject:@"Facebook Logout"]; 
      [self.SettingsTable reloadData]; 
      } 

     }]; 
1

मैं शायद इस विधि

func loginViewFetchedUserInfo(loginView: FBLoginView, user:FBGraphUser) { 
    println(FBSession.activeSession().accessTokenData.accessToken) 
} 

इस कोड स्विफ्ट में है, लेकिन विधि के रूप में अच्छी तरह से Obj सी के लिए मौजूद है में पहुँच टोकन मिलेगा। फिर आप सैंडी और स्टीव