Twitter 가 iOS 에 Integrate 되었습니다.
"드디어, MGTwitterEngine 과 같은 Library에서 벗어 날수 있겠군요! "
라고 생각하시면 큰일 납니다 ;ㅂ;  ( 위에 Optional 로 Link 되어 있는 이유를 아시겠죠? ^^ )

아직 iOS5 보전보다 iOS4 버전이 많을테니, 하위 호환성을 생각해줘야 하기 때문에 여전히 버릴수가 없습니다.
어쨌든 들어 왔습니다 !
그래서 제가 개발하고 있는 것들에 Twitter Framework 을 붙혀 볼까 합니다. 

TWTweetComposeViewController

MFMailComposeViewController와 비슷한 형태의 ViewController 라고 생각하시면 됩니다. 
한번 띄워 볼까요? ^^ 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    

    TWTweetComposeViewController *viewController = [[TWTweetComposeViewController alloc] init];
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.rootViewController = viewController;
    [viewController release];
    [self.window makeKeyAndVisible];
    return YES;
}

위와 같이 Code 를 작성하고, 결과가 나오기를 바랬는데 ;ㅂ;

*** Terminating app due to uncaught exception 'TWUnsupportedPresentationException', reason: 'TWTweetComposeViewController cannot currently be used within a popover or as a non-modal view controller'

*** First throw call stack:

 
위와 같은 에러가 나옵니다.  
말인 즉슨 modal view controller 가 아니면 사용할수가 없다는 군요 ;ㅂ;

그래서 코드는 아래로 옮겨 주고, present modal 로 보여줬습니다.
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    TWTweetComposeViewController *composeViewController = [[TWTweetComposeViewController alloc] init];
    [self presentModalViewController:composeViewController animated:YES];
    [composeViewController release];
}


위와 같이 수정하고 나니,


 위와 같이 이쁜 디자인으로 잘 나오는군요.

// Sets the initial text to be tweeted. Returns NO if the specified text will
// not fit within the character space currently available, or if the sheet
// has already been presented to the user.
- (BOOL)setInitialText:(NSString *)text;

// Adds an image to the tweet. Returns NO if the additional image will not fit
// within the character space currently available, or if the sheet has already
// been presented to the user.
- (BOOL)addImage:(UIImage *)image;

// Removes all images from the tweet. Returns NO and does not perform an operation
// if the sheet has already been presented to the user. 
- (BOOL)removeAllImages;

// Adds a URL to the tweet. Returns NO if the additional URL will not fit
// within the character space currently available, or if the sheet has already
// been presented to the user.
- (BOOL)addURL:(NSURL *)url;

// Removes all URLs from the tweet. Returns NO and does not perform an operation
// if the sheet has already been presented to the user.
- (BOOL)removeAllURLs;

// Specify a block to be called when the user is finished. This block is not guaranteed
// to be called on any particular thread.
@property (nonatomic, copy) TWTweetComposeViewControllerCompletionHandler completionHandler;

그럼 TWTweetComposeViewController 의 header파일을 살짝 살펴 볼까요?

setInitialText:
addImage: 
addUrl:
등이 보이는군요.

여기서 재밌는게, addImage:/addUrl:등이 BOOL 을 return 한다는것입니다.
Tweet이 140 자 까지만 허용하기 떄문에, img 혹은 url 을 추가할때마다, url character가 추가 되기 때문에,
더이상 추가할 수 없을때 "NO"를 return 할것 같습니다.
 while([composeViewController addImage:[UIImage imageNamed:@"circle"]] == YES) {
        NSLog(@"abcdefg");
    }
  이런식으로 코딩을 했을때, "abcdefg"가 한번만 호출되는것으로 봐서, 중복체크도 해주는 모양입니다.
그리고, image를 넣어준 후에 viewController를 노출 시키면, 이미지 Thumnail 과 남은 글자수가 140이 아닌 119로 변경됨을 알수 있습니다.

 

그럼, Twitter 계정정보는 어떻게 접근할까요?
아니, Twitter 계정이 로그인안되어 있으면, TWTweetComposeViewController 를 띄워봐야 헛일 아닙니까?

우선, 사용자 입장에서 Twitter 계정설정은 "아이폰 설정"에서 할 수 있습니다.

 
트위터 앱이 설치 되어 있지 않아도, Twitter 계정을 로그인 할 수 있습니다.

 
심지어 여러개의 Account를 설정할 수 있으며,
Twitter App이 설치되어 있는지 여부도 알려줍니다. ( 아이폰에서 확인 )
해당 스크린샷들은 모두 iOS5 Simulator 에서 캡쳐 한것으로, Simulator에서도 다 테스트 가능합니다.

다시 본론으로 들어 가서, 로그인이 되어 있지 않다면, ViewController가 어떻게 동작할까요?

 
트위터계정이 없다고, Settings 로 이동할것인지, 취소 할것인지 묻습니다. 취소하게되면, Modal 로 띄워진,
ViewController가  Dismiss 됩니다.


이건 좀 구리지 않나요? ViewController를 띄우기 전에 미리 알수 있으면 좋을것 같다는 생각이 드네요 ^^
 
UIKIT_CLASS_AVAILABLE(5_0) @interface TWTweetComposeViewController : UIViewController {
}

// Returns if Twitter is accessible and at least one account has been setup.
+ (BOOL)canSendTweet;

TWTweetComposeViewController의 + method 로 canSendTweet이 있습니다.
트위터 계정이 없다면, NO, 있다면 YES를 얻을 수 있습니다.

그럼,,현재 로그인된 Twitter 정보는 얻을 수 없을까?
아니, 난 Tweet 작성말고, TimeLine을 얻어 오고 싶은데, 이런것들은 어떻게 할수 있을까?

글이 길어 진것 같아 :)
이 질문들은 다음 블로깅에서 답해보도록 하겠습니다 ^^ 

+ Recent posts