Last active
August 29, 2015 14:09
-
-
Save kmakita13714/97fd12dc92d6781fc791 to your computer and use it in GitHub Desktop.
[iOS] App ExtensionsからNetwork経由でLogを送る
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var server = http.createServer(function (req, res) { | |
var body_str = ""; | |
req.on('data', function(chunk) { body_str += chunk }); | |
req.on('error', function(e) { console.log('problem with request: ' + e.message) }); | |
req.on('end', function() { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end(); | |
console.log(body_str); | |
}); | |
}); | |
var host = '192.168.64.28'; | |
var port = 80; | |
server.listen(port, host); | |
console.log('Server running at http://%s:%d/', host, port); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (void)sendNetLoggerWithSession:(NSURLSession *)urlSession message:(NSString *)message | |
{ | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss (A)"; | |
NSString *sendMessage = [NSString stringWithFormat:@"%@ %@", [dateFormatter stringFromDate:[NSDate date]], message]; | |
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.64.28/"]]; | |
request.HTTPMethod = @"POST"; | |
request.HTTPBody = [sendMessage dataUsingEncoding:NSUTF8StringEncoding]; | |
NSURLSessionDataTask *sessionTask = [urlSession dataTaskWithRequest:request]; | |
[sessionTask resume]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 便利なモノを定義 | |
#define CALLED_METHOD [NSString stringWithFormat:@"%@<%d> %s", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, __PRETTY_FUNCTION__] | |
// プロパティの定義 | |
@property (nonatomic, strong) NSURLSession *urlSession; | |
// NSURLSessionの生成方法は以下の通り | |
NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"jp.example.backgroundsession"]; | |
config.sharedContainerIdentifier = @"group.jp.example"; | |
_urlSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; | |
// 呼び出し方は以下の通り | |
[SimpleNetLogger sendNetLoggerWithSession:_urlSession message:CALLED_METHOD]; | |
/* | |
などとやると、以下のように出力されます。 | |
2014-11-09 13:01:37 (46897681) TodayViewController.m<89> -[TodayViewController loadView] | |
2014-11-09 13:01:37 (46897835) TodayViewController.m<116> -[TodayViewController viewWillAppear:] | |
2014-11-09 13:02:33 (46953043) TodayViewController.m<148> -[TodayViewController viewDidAppear:] | |
2014-11-09 13:02:33 (46953034) TodayViewController.m<116> -[TodayViewController viewWillAppear:] | |
2014-11-09 13:02:38 (46958592) TodayViewController.m<106> -[TodayViewController viewDidLoad] | |
2014-11-09 13:02:38 (46958644) TodayViewController.m<157> -[TodayViewController widgetPerformUpdateWithCompletionHandler:] | |
2014-11-09 13:02:38 (46958678) TodayViewController.m<116> -[TodayViewController viewWillAppear:] | |
2014-11-09 13:02:38 (46958699) TodayViewController.m<148> -[TodayViewController viewDidAppear:] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
App ExtensionsからNSURLSessionクラスを利用して、バックグラウンドでアップロードやダウンロードが出来ます。
これを利用して、 Node.jsで作った簡易HTTPサーバーへ送り、コンソールに出力しています。
意外と便利!!