首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有嵌入式UIWebView的Facebook-ios-sdk

带有嵌入式UIWebView的Facebook-ios-sdk
EN

Stack Overflow用户
提问于 2010-12-27 05:22:31
回答 3查看 6.3K关注 0票数 16

我正在使用新的facebook-ios-sdk,并且已经成功地将api集成到我的本机应用程序中。我能够使用ios-sdk类的弹出对话框对用户进行身份验证并正确设置权限。

对于我的应用程序的一部分,我需要在UIWebView中使用facebook连接,使用javascript和html来处理webview中的数据。假设用户已经登录并通过上面的例程进行了身份验证,我假设UIWebView将共享这些凭据,或者至少有某种方法可以将凭据传递或分配给webview。

不幸的是,我发现之前的这篇文章似乎暗示这个方案并不是很有效(iOS - being logged-in in a webView after logging in with the SDK)。还有没有其他人遇到过这种情况和/或找到了解决办法?这似乎是一个相当简单的用例,因为我并没有试图启动移动safari或类似的东西-所有这些都在同一个原生应用程序中。

看起来一定有一些我遗漏的简单的技巧或设置。也许以某种方式在新的UIWebView中设置cookie?或者像这样的东西?

EN

回答 3

Stack Overflow用户

发布于 2013-09-19 20:00:12

--通过这种方式Facebook-ios-sdk

@property (非原子,保留) NSString *chk;

代码语言:javascript
复制
@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"];
            cell.textLabel.text=@"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
EN

Stack Overflow用户

发布于 2014-09-10 06:04:44

我可能会在这个方法中得到访问令牌。

代码语言:javascript
复制
func loginViewFetchedUserInfo(loginView: FBLoginView, user:FBGraphUser ) {
    println(FBSession.activeSession().accessTokenData.accessToken)
}

此代码在Swift中,但该方法也适用于Obj C。然后,您将按照Sandy和Steve的建议在web视图中使用访问令牌

票数 1
EN

Stack Overflow用户

发布于 2011-11-16 02:02:33

一旦您有了API令牌,您就应该能够在您的web视图中使用Faceboook JavaScript FB access。官方文档请参见http://developers.facebook.com/docs/reference/javascript/。这篇文章给出了一个合理的解释:Retrieve Access Token Using Javascript API。正如这篇文章所描述的,您可以直接在AJAX调用中使用访问令牌。我不会弄乱cookies -只要传递访问令牌即可。

你没有在web视图中确切地解释你想要对Facebook做什么,所以我不确定这是否百分之百地回答了你的问题。我假设您知道如何将访问令牌传递给web视图(UIWebView's stringByEvaluatingJavaScriptFromString)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4535479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档