【问题标题】:best way to implement twitter实现推特的最佳方式
【发布时间】:2013-09-12 18:22:30
【问题描述】:

1) 我从这个链接Twitter with OAuth 找到了 Twitter rush 示例代码 用于集成。

我添加了消费者密钥和密钥。但它从未授权该应用程序。

2) 如果我使用共享工具包,它会给出错误,即从 twitter 请求访问时出现问题。
3)如果我使用社交框架,那么如果用户没有在设置中添加他/她的帐户,它会发出警报。

我需要该用户不应该在应用程序之外进行 Twitter 登录。

有人知道什么是与 twitter 集成的最佳方式吗?

请帮忙。

【问题讨论】:

    标签: ios objective-c twitter


    【解决方案1】:

    我希望这会对你有所帮助。 . .

    1.将以下类添加到您的项目中 GTMOAuthAuthentication.h/m GTMOAuthSignIn.h/m GTMHTTPFetcher.h/m GTMOAuthViewControllerTouch.h/m GTMOAuthViewTouch.xib

    2 。添加以下框架 Security.framework 和 SystemConfiguration.framework。

    3 .set -ObjC 为应用程序目标的“其他链接器标志”构建选项。

    4 。然后是时候进行一些编码了。

    导入 GTMOAuthAuthentication.h 和 GTMOAuthViewControllerTouch.h

    - (void)signInWithTwitter
    {
    NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"];
    NSURL *accessURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];
    NSURL *authorizeURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/authorize"];
    NSString *scope = @"http://api.twitter.com/";
    
    GTMOAuthAuthentication *auth = [self authForTwitter];
    
    [auth setCallback:@"http://www.noop.com/OAuthCallback"];
    
    GTMOAuthViewControllerTouch *viewController;
    
    viewController = [[GTMOAuthViewControllerTouch alloc] initWithScope:scope
                                                                 language:nil
                                                          requestTokenURL:requestURL
                                                        authorizeTokenURL:authorizeURL
                                                           accessTokenURL:accessURL
                                                           authentication:auth
                                                           appServiceName:@"AppName : Twitter"
                                                                 delegate:self
    finishedSelector:@selector(viewController:finishedWithAuth:error:)];
    
    [appDelegate.navigationController pushViewController:viewController animated:YES];
    }
    
    - (GTMOAuthAuthentication *)authForTwitter {
    GTMOAuthAuthentication *auth = [[GTMOAuthAuthentication alloc] initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1
                consumerKey:TWITTER_CONSUMER_KEY
                 privateKey:TWITTER_CONSUMER_SECRET];
    
    [auth setServiceProvider:@"Twitter"];
    
    return auth;
    }
    
    - (void)viewController:(GTMOAuthViewControllerTouch *)viewController finishedWithAuth:(GTMOAuthAuthentication *)auth error:(NSError *)error {
    
        if(error)
        {
             //handle error
        }
        else
        {
            // do stuff as per your app.
        }
    }
    

    注意:如果您收到“无法验证 oauth 签名和令牌”之类的错误消息,请检查您的系统时间是否正确。

    【讨论】:

    • 我已经通过推特认证成功登录。真的非常感谢。但是发布图片和链接到 twitter 的方法是什么。
    【解决方案2】:

    如果您不希望该用户外出,您可以在 iOS 6 上使用 ACAccountStore Account.FrameworkSocial.framework

     NSUrl *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"];
      NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:twittername,@"screen_name",nil];
    
         account = [[ACAccountStore alloc] init];
        ACAccountType *twitterAccountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
        NSArray *twitterAccounts = [account accountsWithAccountType:twitterAccountType];
    
        // Runing on iOS 6
        if (NSClassFromString(@"SLComposeViewController") && [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
        {
            [account requestAccessToAccountsWithType:twitterAccountType options:NULL completion:^(BOOL granted, NSError *error)
             {
                 if (granted)
                 {
    
                     SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url                                      parameters:params];
    
                     [request setAccount:[twitterAccounts lastObject]];
    
                     dispatch_async(dispatch_get_main_queue(), ^
                                    {
    
                                        [NSURLConnection sendAsynchronousRequest:request.preparedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)
                                         {
                                             dispatch_async(dispatch_get_main_queue(), ^
                                                            {
                                                                if (data)
                                                                {
    //                                                                [self loadData:data];
    
                                                                    NSString* newStr = [[NSString alloc] initWithData:data
                                                                                                              encoding:NSUTF8StringEncoding];
                                                                    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
    
    
                                                                    NSLog(@"data:%@",newStr);
                                                                }
                                                            });
                                         }];
                                    });
                 }
             }];
        }
        else if (NSClassFromString(@"TWTweetComposeViewController") && [TWTweetComposeViewController canSendTweet]) // Runing on iOS 5
        {
            [account requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error)
             {
                 if (granted)
                 {
                     TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
                     [request setAccount:[twitterAccounts lastObject]];
    
                     dispatch_async(dispatch_get_main_queue(), ^
                                    {
                                        [NSURLConnection sendAsynchronousRequest:request.signedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)
                                         {
                                             dispatch_async(dispatch_get_main_queue(), ^
                                                            {                             
                                                                if (data)                                 
                                                                {                                 
                                                                    NSString* newStr = [[NSString alloc] initWithData:data
                                                                                                             encoding:NSUTF8StringEncoding];
    
    
                                                                    NSLog(@"data:%@",newStr);                                                           }
                                                            });
                                         }];
    
    
                                    });
                 }
             }];
        }
    }
    

    你必须保留 ACAccountStore: in .h

    @property (nonatomic, strong) ACAccountStore *account;
    

    【讨论】:

      猜你喜欢
      • 2012-12-26
      • 2016-04-12
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      相关资源
      最近更新 更多