본문 바로가기

IT/iOS

[iOS] ios7에서 트위터랑 페이스북 포스팅하기

필요 프레임워크

Twitter.framework, social.framework, Accounts.framework, AdSupport.framework(이건.. 왜 필요한거지.) 


개발환경

OS X : 10.9.4 메버릭스

Xcode : 5.1.1

iOS SDK : 7.1

ARC


트위 터 


#import <twitter/twitter.h>

후에

    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
        ACAccountStore *accountStore = [[ACAccountStore alloc] init];
        ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
        [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
           if (granted) {
               NSArray *accountArray = [accountStore accountsWithAccountType:accountType];
               if (accountArray.count > 0) {
                   NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1/statuses/update.json"];
                   NSDictionary *params = [NSDictionary dictionaryWithObject:@"SLRequest post test." forKey:@"status"];
                   
                   SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:url parameters:params];
                   [request setAccount:[accountArray objectAtIndex:0]];
                   [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                       NSLog(@"responseData=%@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
                   }];
               }
           }
       }];
    }


다음 페이스북

페이스북은 앱을 만들어야한다.


페북 개발자 버전으로 앱을 만든후


    
#import <Accounts/Accounts.h>
#import <Social/Social.h>

추가후

    
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    
    ACAccountType *accountTypeFacebook =
    [accountStore accountTypeWithAccountTypeIdentifier:
     ACAccountTypeIdentifierFacebook];
    
    NSDictionary *options = @{ACFacebookAppIdKey: @"You Facebook App Key", ACFacebookPermissionsKey: @[@"publish_actions"], ACFacebookAudienceKey: ACFacebookAudienceFriends };
    
    [accountStore requestAccessToAccountsWithType:accountTypeFacebook options:options completion:^(BOOL granted, NSError *error) {
       if(granted) {
           NSArray *accounts = [accountStore accountsWithAccountType:accountTypeFacebook];
           _facebookAccount = [accounts lastObject];
           
           NSDictionary *parameters = @{@"access_token":_facebookAccount.credential.oauthToken, @"message": @"My first iOS 7 Facebook posting"};
           
           NSURL *feedURL = [NSURLURLWithString:@"https://graph.facebook.com/me/feed"];
           
           SLRequest *feedRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:feedURL parameters:parameters];
           
           [feedRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
            {
                NSLog(@"Request failed, %@", [urlResponse description]);
            }];
       } else {
           NSLog(@"Access Denied");
           NSLog(@"[%@]",[error localizedDescription]);
       }
   }];

그럼 끝