5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Objective-CからネガポジAPIを叩いてみた

Last updated at Posted at 2014-07-30

概要

ハッカソンする機会があり、
Mashup Awards 8のAPI集で使えそうなAPIを見ていたら
ネガポジAPIが面白そうだったので、Objective-Cから使ってみた。

使い方

apikeyを申請

http://ap.mextractr.net/ma8/signup
メールでkeyがもらえます。

API

http://ap.mextractr.net/ma8/negaposi_analyzer?out=atom&apikey=xxxxx&text=<URLエンコードした文字列>

params

//出力形式
out=atom or json
//取得したkeyをset
apikey=XXX 
//エンコードした文字列
test=文字列

実装内容

Negaposi.h
# import <Foundation/Foundation.h>

@protocol NegaposiDelegate <NSObject>
- (void)negaposiDidFinishLoad;
- (void)negaposiDidFailLoad;
@end

@interface Negaposi : NSObject

@property (nonatomic, strong) NSString *negaposiValue;
@property (nonatomic, weak) id<NegaposiDelegate> delegate;

- (void)fetchNegaposiData:(NSString *)str;

@end
Negaposi.m
# import "Negaposi.h"

NSString * const kNegaposiUrlString = @"http://ap.mextractr.net/ma8/negaposi_analyzer?out=json&apikey=XXXXXXX&text=";

@implementation Negaposi

- (void)fetchNegaposiData:(NSString *)str{
  // 文字列をURLencoding
  NSString *encodeString = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  NSString *urlString = [kNegaposiUrlString stringByAppendingString:encodeString];
  NSURL *url = [NSURL URLWithString:urlString];
  NSURLRequest *request = [NSURLRequest requestWithURL:url];

  NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init];
  [NSURLConnection sendAsynchronousRequest:request
                                     queue:backgroundQueue
                         completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                           // エラー時はnegaposiDidFailLoadを投げる
                           if (error) {
                             if ([self.delegate respondsToSelector:@selector(negaposiDataDidFailLoad)]) {
                               [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                 [self.delegate negaposiDidFailLoad];
                               }];
                             }
                             return;
                           }
                          
                           NSError *jsonSerializationError = nil;
                           NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data                            options:NSJSONReadingAllowFragments                                 error:&jsonSerializationError];
                           // self.negaposiValueにネガポジの値をsetする
                           self.negaposiValue = [dataDictionary[@"negaposi"] stringValue];
                           // 成功時はnegaposiDidFinishLoadを投げる
                           if ([self.delegate respondsToSelector:@selector(negaposiDataDidFinishLoad)]) {
                             [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                               [self.delegate negaposiDidFinishLoad];
                             }];
                           }
 }];
}
@end

あとはNegaposiDelegateをdelegeteされたController等で
- (void)negaposiDidFinishLoad;
- (void)negaposiDidFailLoad;
の処理を書けば、使える。

最後に

※このバージョンは商用にご提供できません。商用のご利用要望は別途お問い合わせください。

残念ながら、無料で商用には使えないらしい。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?