2014년 1월 21일 화요일

sns 공유 (Twitter, Facebook, Line)

+ (void)postToTwitter:(UIViewController *)originController  withData:(NSDictionary *)dic {
    SLComposeViewController *tweetSheet = [SLComposeViewController
                                           composeViewControllerForServiceType:
                                           SLServiceTypeTwitter];
    tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
        switch(result) {
                //  This means the user cancelled without sending the Tweet
            case SLComposeViewControllerResultCancelled:
                break;
                //  This means the user hit 'Send'
            case SLComposeViewControllerResultDone:
                break;
        }
    };
    
    //  Set the initial body of the Tweet
    [tweetSheet setInitialText:[dic valueForKey:@"text"]];
    NSString *imageUrl = [dic valueForKey:@"imageurl"];
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if(![tweetSheet addImage:[UIImage imageWithData:data]]) {
            NSLog(@"Unable to add the image!");
        } else {
            if (![tweetSheet addURL:[NSURL URLWithString:[dic valueForKey:@"referenceurl"]]]){
                NSLog(@"Unable to add the URL!");
            }
            [originController presentViewController:tweetSheet animated:YES completion:^{
                NSLog(@"Tweet sheet has been presented.");
            }];
        }
    }];
}

+ (void)postToFacebook:(UIViewController *)originController withData:(NSDictionary *)dic {
    SLComposeViewController *facebookSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    
    [facebookSheet setInitialText:[dic valueForKey:@"text"]];
    [facebookSheet addURL:[NSURL URLWithString:[dic valueForKey:@"referenceurl"]]];
    NSString *imageUrl = [dic valueForKey:@"imageurl"];
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        [facebookSheet addImage:[UIImage imageWithData:data]];
    }];
    
    [originController presentViewController:facebookSheet animated:YES completion:Nil];
}

+ (void)postToLine:(UIViewController *)originController withData:(NSDictionary *)dic {
    //텍스트 전송 링크 전송
    NSString *_resultString = [[NSString stringWithFormat:@"%@%@",[dic valueForKey:@"text"],[dic valueForKey:@"imageurl"]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *LineUrlString = [NSString stringWithFormat:@"line://msg/text/%@",_resultString];
    BOOL r =[[UIApplication sharedApplication] openURL:[NSURL URLWithString:LineUrlString]];
    if (!r) {
        [[[UIAlertView alloc] initWithTitle:@"" message:@"LINEがインストールされていません。" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil,nil] show];
    }

}

댓글 없음:

댓글 쓰기