{"id":1238,"date":"2016-11-14T19:17:49","date_gmt":"2016-11-14T10:17:49","guid":{"rendered":"http:\/\/androiphone.uvs.jp\/?p=1238"},"modified":"2016-11-14T19:17:49","modified_gmt":"2016-11-14T10:17:49","slug":"ios-nsurlconnection%e3%81%aedeprecated%e3%81%ab%e3%82%88%e3%82%8bnsurlsession%e3%81%ae%e5%88%a9%e7%94%a8","status":"publish","type":"post","link":"https:\/\/androiphone.uvs.jp\/?p=1238","title":{"rendered":"[iOS] NSURLConnection\u306edeprecated\u306b\u3088\u308bNSURLSession\u306e\u5229\u7528"},"content":{"rendered":"<p>\u30b5\u30a4\u30c8\u306e\u904b\u7528\u3060\u3051\u3067\u306f\u304f\u3001\u30b2\u30fc\u30e0\u306e\u30e9\u30f3\u30ad\u30f3\u30b0\u3084\u3089\u3001\u30c7\u30fc\u30bf\u306e\u53ce\u96c6\u3084\u3089\u3092\u30ec\u30f3\u30bf\u30eb\u30b5\u30fc\u30d0\u30fc\u3067\u8cc4\u3063\u3066\u3044\u308b\u306e\u3067\u3001Android\u3084iOS\u306e\u30a2\u30d7\u30ea\u3092\u958b\u767a\u3059\u308b\u3068\u304d\u306b\u306f\u3001POST\u3067HTTP\u63a5\u7d9a\u3001\u30b5\u30fc\u30d0\u30fc\u51e6\u7406\u3001JSON\u8fd4\u4fe1\u3068\u3044\u3046\u4f5c\u696d\u304c\u5e38\u306b\u5fc5\u8981\u3060\u3063\u305f\u3002<\/p>\n<p>\u3044\u3061\u3044\u3061\u8a18\u8ff0\u3057\u306a\u304f\u3066\u3082\u3044\u3044\u69d8\u306b\u3001\u72ec\u81ea\u306e\u30af\u30e9\u30b9\u3067\u5b9f\u88c5\u3057\u3066\u3044\u305f\u306e\u3060\u304c\u3001NSURLConnection\u304cdeprecated\u306b\u306a\u3063\u3066NSURLSession\u306b\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u51fa\u3066\u3001\u3044\u308d\u3044\u308d\u3068\u63a2\u3063\u3066\u898b\u305f\u3089\u3001\u3053\u3093\u306a\u611f\u3058\u306b\u306a\u3063\u305f\u3002<\/p>\n<p>param\u306fPOST\u306b\u6e21\u3059Dictionarty\u30c7\u30fc\u30bf\u3002value\u306f\u30c7\u30ea\u30b2\u30fc\u30c9\u3067\u623b\u308b\u969b\u306b\u6301\u305f\u305b\u305f\u3044\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b(\u4e0d\u8981\u306a\u3089nil)\u3067\u3082\u3044\u3044\u3002flag\u306f\u8907\u6570\u306eNetWorkManeger\u3092\u751f\u6210\u3057\u305f\u3068\u304d\u306b\u30c7\u30ea\u30b2\u30fc\u30c9\u3067\u623b\u3063\u3066\u304f\u308b\u3068\u304d\u306b\u533a\u5225\u304c\u3064\u304f\u69d8\u306b\u56fa\u6709\u306eint\u5024\u3092\u6e21\u305b\u308b\u69d8\u306b\u3057\u305f\u3002<\/p>\n<p>POST\u3067\u3082GET\u3067\u3082\u6e21\u305b\u308b\u69d8\u306b\u3001URL\u30a8\u30f3\u30b3\u30fc\u30c9\u3067\u5f15\u6570\u3092\u751f\u6210\u3057\u3066\u3042\u308b(GET\u306f\u4f7f\u308f\u306a\u3044\u305f\u3081\u672a\u5b9f\u88c5\u3067\u3059\u304c\u30fb\u30fb\u30fbw)<\/p>\n<p>\u3053\u308c\u3067NSSession\u306f\u3044\u3044\u3068\u3057\u3066\u3082\u30fb\u30fb\u30fb\u30fbATS\u306e\u554f\u984c\u304c\u30fb\u30fb\u30fb\u6b21\u306b\u66f8\u304d\u307e\u3059\u30fb\u30fb\u30fb<\/p>\n<p>\u4e0b\u304c\u30b3\u30fc\u30c9\u3002\u66f8\u304d\u306a\u3050\u3063\u305f\u306e\u3067\u7518\u3044\u304b\u3082<\/p>\n<p>[obj-c]<br \/>\n#import &quot;NetworkManeger.h&quot;<\/p>\n<p>@implementation NetworkManeger<\/p>\n<p>-(void)sendserver_url_withPost:(NSString*)URL_st param:(NSDictionary*)param value:(NSDictionary*)_value flag:(int)_flag{<\/p>\n<p>    \/\/\u30c7\u30ea\u30b2\u30fc\u30c8\u306b\u623b\u3059\u3068\u304d\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc<br \/>\n    value = _value;<br \/>\n    flag = _flag;<\/p>\n<p>    \/\/ \u30b5\u30fc\u30d0\u30fc\u3068\u901a\u4fe1\uff08URL\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u4f5c\u6210:POST\uff09<br \/>\n    NSURL *serviceURL = [NSURL URLWithString:URL_st];<br \/>\n    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:serviceURL];<br \/>\n    [req setHTTPMethod:@&quot;POST&quot;];<\/p>\n<p>    NSMutableString *ms = [NSMutableString string];<br \/>\n    for (id key in [param keyEnumerator]) {<br \/>\n        \/\/NSLog(@&quot;Key:%@ Value:%@&quot;, key, [dic valueForKey:key]);<br \/>\n        [ms appendString:key];<br \/>\n        [ms appendString:@&quot;=&quot;];<br \/>\n        [ms appendString:[[param valueForKey:key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]]];<br \/>\n        [ms appendString:@&quot;&amp;&quot;];<br \/>\n    }<br \/>\n    NSString *buf = [ms substringToIndex:([ms length]-1)];<\/p>\n<p>    \/\/NSLog(@&quot;%@&quot;,buf);<\/p>\n<p>    [req setHTTPBody:[buf dataUsingEncoding:NSUTF8StringEncoding]];<\/p>\n<p>    \/\/ \u30bb\u30c3\u30b7\u30e7\u30f3\u4f5c\u6210<br \/>\n    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];<\/p>\n<p>    \/\/ \u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u306e\u8a2d\u5b9a<br \/>\n    sessionConfig.timeoutIntervalForRequest  =  10;<br \/>\n    sessionConfig.timeoutIntervalForResource =  10;<\/p>\n<p>    \/\/ \u901a\u4fe1\u8a2d\u5b9a<br \/>\n    NSURLSession* session = [NSURLSession sessionWithConfiguration: sessionConfig delegate:self delegateQueue: [NSOperationQueue mainQueue]];<\/p>\n<p>    \/\/\u30c7\u30fc\u30bf\u30d0\u30c3\u30d5\u30a1\u306e\u521d\u671f\u5316<br \/>\n    recevedData = [[NSMutableData alloc]init];<\/p>\n<p>    \/\/ \u901a\u4fe1\u958b\u59cb<br \/>\n    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:req];<br \/>\n    [dataTask resume];<\/p>\n<p>}<\/p>\n<p>\/\/ \u30ec\u30b9\u30dd\u30f3\u30b9\u51e6\u7406<br \/>\n&#8211; (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask<br \/>\ndidReceiveResponse:(NSURLResponse *)response<br \/>\n completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {<\/p>\n<p>    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;<\/p>\n<p>    \/\/ Header\u306e\u60c5\u5831\u306e\u78ba\u8a8d<br \/>\n    if (httpResponse.statusCode == 200) {<\/p>\n<p>        completionHandler(NSURLSessionResponseAllow);   \/\/ \u7d9a\u3051\u308b<\/p>\n<p>    } else {<br \/>\n        \/\/ error.code = -999\u3067\u7d42\u4e86\u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3070\u308c\u308b<br \/>\n        completionHandler(NSURLSessionResponseCancel);  \/\/ \u6b62\u3081\u308b<br \/>\n    }<\/p>\n<p>}<\/p>\n<p>\/\/ \u53d7\u4fe1\u30c7\u30fc\u30bf\u51e6\u7406<br \/>\n&#8211; (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {<br \/>\n    \/\/ \u53d7\u4fe1\u30c7\u30fc\u30bf\u683c\u7d0d<br \/>\n    [recevedData appendData:data];<br \/>\n     \/\/    printf(&quot;\/*%s*\/\\n&quot;,[[NSString stringWithFormat:@&quot;%zd&quot;,[data length]] UTF8String]);<br \/>\n}<\/p>\n<p>&#8211; (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {<\/p>\n<p>    [session invalidateAndCancel];<\/p>\n<p>    if (!error) {<br \/>\n        \/\/ \u6b63\u5e38\u7d42\u4e86<br \/>\n        NSString *str= [[NSString alloc] initWithData:recevedData encoding:NSUTF8StringEncoding];<br \/>\n        \/\/NSLog(@&quot;%@&quot;,str);<br \/>\n        \/\/NSLog(@&quot;\u6b63\u5e38\u7d42\u4e86&quot;);<br \/>\n        if(self.delegate==nil) return;<br \/>\n        if ([self.delegate respondsToSelector:@selector(getFinished:value:flag:)]) {<br \/>\n            \/\/\u6b63\u5e38\u7d42\u4e86\u3092\u547c\u3073\u51fa\u3059<br \/>\n            [self.delegate getFinished:str value:value flag:flag];<br \/>\n        }<\/p>\n<p>    } else {<br \/>\n        \/\/ \u30a8\u30e9\u30fc\u7d42\u4e86<br \/>\n        \/*  code\u5024<br \/>\n          -999 : statusCode\u304c200\u4ee5\u5916(401\u306a\u3069)\u3001\u30e6\u30fc\u30b6\u30fc\u30ad\u30e3\u30f3\u30bb\u30eb?<br \/>\n         -1200 : ATS\u95a2\u9023(\u30ed\u30b0\u53c2\u8003)<br \/>\n         -1009 : \u6a5f\u5185\u30e2\u30fc\u30c9\u3001\u30e2\u30d0\u30a4\u30eb\u30c7\u30fc\u30bf\u901a\u4fe1\u30fbwifi off<br \/>\n         *\/<\/p>\n<p>        \/\/NSLog(@&quot;\u7570\u5e38\u7d42\u4e86 errorcode=%ld&quot;,error.code);<\/p>\n<p>        if(self.delegate==nil) return;<br \/>\n        if ([self.delegate respondsToSelector:@selector(getError:value:flag:)]) {<br \/>\n            \/\/\u7570\u5e38\u7d42\u4e86\u3092\u547c\u3073\u51fa\u3059<br \/>\n            [self.delegate getError:error.code value:value flag:flag];<br \/>\n        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>\/\/\u623b\u308aJson\u6587\u5b57\u5217\u306e\u30ed\u30b0\u51fa\u529b<br \/>\n+(void)LogJsonFromSt:(NSString*)r_line{<\/p>\n<p>    NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:[r_line dataUsingEncoding:NSUnicodeStringEncoding] options:NSJSONReadingAllowFragments error:nil];<\/p>\n<p>    NSLog(@&quot;%@&quot;,jsonResponse);<\/p>\n<p>    NSLog(@&quot;&#8211;\u53d7\u4fe1\u30e9\u30a4\u30f3&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&quot;);<br \/>\n    NSLog(@&quot;%@&quot;,r_line);<br \/>\n    NSLog(@&quot;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&quot;);<br \/>\n    NSLog(@&quot;&#8211;JSON&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&quot;);<br \/>\n    for (id key in [jsonResponse keyEnumerator]) {<br \/>\n        NSLog(@&quot;Key:%@ Value:%@&quot;, key, [jsonResponse valueForKey:key]);<br \/>\n    }<br \/>\n    NSLog(@&quot;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&quot;);<\/p>\n<p>}<\/p>\n<p>@end<br \/>\n[\/obj-c]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30b5\u30a4\u30c8\u306e\u904b\u7528\u3060\u3051\u3067\u306f\u304f\u3001\u30b2\u30fc\u30e0\u306e\u30e9\u30f3\u30ad\u30f3\u30b0\u3084\u3089\u3001\u30c7\u30fc\u30bf\u306e\u53ce\u96c6\u3084\u3089\u3092\u30ec\u30f3\u30bf\u30eb\u30b5\u30fc\u30d0\u30fc\u3067\u8cc4\u3063\u3066\u3044\u308b\u306e\u3067\u3001Android\u3084iOS\u306e\u30a2\u30d7\u30ea\u3092\u958b\u767a\u3059\u308b\u3068\u304d\u306b\u306f\u3001POST\u3067HTTP\u63a5\u7d9a\u3001\u30b5\u30fc\u30d0\u30fc\u51e6\u7406\u3001JSON\u8fd4\u4fe1\u3068\u3044\u3046\u4f5c\u696d\u304c\u5e38\u306b\u5fc5\u8981 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"","_original_post":"","footnotes":""},"categories":[7,8],"tags":[],"class_list":["post-1238","post","type-post","status-publish","format-standard","hentry","category-ios","category--ios","ja"],"_links":{"self":[{"href":"https:\/\/androiphone.uvs.jp\/index.php?rest_route=\/wp\/v2\/posts\/1238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/androiphone.uvs.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/androiphone.uvs.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/androiphone.uvs.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/androiphone.uvs.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1238"}],"version-history":[{"count":2,"href":"https:\/\/androiphone.uvs.jp\/index.php?rest_route=\/wp\/v2\/posts\/1238\/revisions"}],"predecessor-version":[{"id":1240,"href":"https:\/\/androiphone.uvs.jp\/index.php?rest_route=\/wp\/v2\/posts\/1238\/revisions\/1240"}],"wp:attachment":[{"href":"https:\/\/androiphone.uvs.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/androiphone.uvs.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/androiphone.uvs.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}