夢見るポテト

色々きになったことを書いてこうと思う

ニコニコ生放送の開始お知らせアプリを作りたくて

※注意これは書きかけのプログラムです動かないです※

久しぶりに少しニコニコ生放送を見てたら、そういや放送開始が通知センターに出てくれたら
ありがたいなぁと、ふと思い(なぜか)作りたいとポチポチコードを打っていたのだが
一応メモ代わりに残しとこうと思います。

ログインまではできるようになってチケットや接続URLとポートとスレッドを取り出すことは
できたんだけど、それらをどのように送信するかはXMLSocketでやらなくちゃいけないそうなので
まだわからないが頑張ってみよう

//
//  main.m
//  niconama
//
//  Created by heart on 2013/12/10.
//  Copyright (c) 2013年 heart. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    NSString* username;
    NSString* password;
    
    username = @"メールアドレス";
    password = @"パスワード";
    
    
    //NSURLConnection* connection;
    NSURL* url = [NSURL URLWithString:@"https://secure.nicovideo.jp/secure/login"];
    NSMutableURLRequest* req = [[NSMutableURLRequest alloc] initWithURL:url];
    
    NSString* data = [[NSString alloc] initWithFormat:@"site=nicolive_antenna&mail=%@&password=%@",username,password];
    
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSHTTPURLResponse* httpresponse;
    NSError *err;
    
    NSData *contents = [NSURLConnection sendSynchronousRequest:req returningResponse:&httpresponse error:&err];
    
    //connection = [[NSURLConnection alloc] initWithRequest:req delegate:0];
    
    if (contents)
    {
       // NSLog(@"Login...OK!");
        
        //NSString *responseXML =[[NSString alloc] initWithData:contents encoding:NSUTF8StringEncoding];
        
        
        NSXMLDocument *doc;
        doc = [[NSXMLDocument alloc]initWithData:contents options:0 error:NULL];
        
        
        NSArray *nodes;
        //nodes = [doc nodesForXPath:@"nicovideo_user_response" error:NULL];
        
        
        
            if ((nodes = [doc nodesForXPath:@"nicovideo_user_response/ticket" error:NULL])){
            
            
                NSXMLNode* snode;
                snode = [nodes objectAtIndex:0];
                
                NSString* ticket;
                ticket = [snode stringValue];
                
                //NSLog(@"%@",ticket);
                
                
                NSURL* url2 = [NSURL URLWithString:@"http://live.nicovideo.jp/api/getalertstatus"];
                NSMutableURLRequest* req2 = [[NSMutableURLRequest alloc] initWithURL:url2];
                
                NSString* data2 = [[NSString alloc] initWithFormat:@"ticket=%@",ticket];
                
                [req2 setHTTPMethod:@"POST"];
                [req2 setHTTPBody:[data2 dataUsingEncoding:NSUTF8StringEncoding]];
                
                NSHTTPURLResponse* httpresponse2;
                NSError *err2;
                
                NSData *contents2 = [NSURLConnection sendSynchronousRequest:req2 returningResponse:&httpresponse2 error:&err2];
                
                
                if (contents2) {
                    
                    //NSString *str = [[NSString alloc] initWithData:contents2 encoding:NSUTF8StringEncoding];
                    
                    //NSLog(@"%@",str);
                    
                    NSArray* node3;
                    
                    NSXMLDocument* doc3 = [[NSXMLDocument alloc] initWithData:contents2 options:0 error:NULL];
                    
                    if ((node3 = [doc3 nodesForXPath:@"getalertstatus/ms/addr" error:NULL])){
                        
                        NSXMLNode* snode3;
                        snode3 = [node3 objectAtIndex:0];
                        NSString* addr;
                        addr = [snode3 stringValue];
                        
                        if ((node3 = [doc3 nodesForXPath:@"getalertstatus/ms/port" error:NULL])){
                            
                        
                        
                            NSXMLNode* snode4;
                            snode4 = [node3 objectAtIndex:0];
                            NSString* port;
                            port = [snode4 stringValue];
                            
                            
                        
                        
                            
                        
                            if ((node3 = [doc3 nodesForXPath:@"getalertstatus/ms/thread" error:NULL])) {
                                
                                NSXMLNode* snode5;
                                snode5 = [node3 objectAtIndex:0];
                                NSString* thread;
                                thread = [snode5 stringValue];
                            
                               //NSLog(@"%@",addr);
                                //NSLog(@"%@",port);
                                //NSLog(@"%@",thread);
                                
                                
                                NSString *str = [NSString stringWithFormat:@"<thread thread=\"%@\" version=\"20061206\" res_from=\"-1000\" />",thread];
                                
                                NSHost* host = [NSHost hostWithName:addr];
                                
                                NSInputStream* indata;
                                NSOutputStream* outdata;
                                indata = [[NSInputStream alloc]
                                
                                [NSInputStream retain];
                                [NSOutputStream retain];
                                
                                
                                [NSInputStream setDelegate:self];
                                [NSOutputStream setDelegate:self];
                                [NSInputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
                                [NSOutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
                                
                                [NSInputStream open];
                                [NSOutputStream open];
                                
                            }
                        }
                    
                    
                    
                    
                    }else{
                        NSLog(@"失敗2");
                    }
                    
                    
                } else {
                    NSLog(@"失敗");
                }
                
                
            
            }else{
                NSLog(@"not find");
            }
        
    
        

    
        
        
        //NSLog(@"%@",responseXML);
        
        
    
    }
    else
    {
        NSLog(@"connection is error");
        
    }
}

簡単なXMLを表示させるプログラム

簡単そうな構造のXMLを参考にしたかったので
こちらで紹介されているhttp://zapanet.info/api/first.html
お天気Webサービス仕様 - Weather Hacks - livedoor 天気情報
http://weather.livedoor.com/weather_hacks/webservice.html
を参考にして作りました。

f:id:heart02:20121107115628p:plain

ソースコードはこちらにアップしました
https://github.com/heart02/otenki

スワイプで戻る・進むが出来るブラウザを作ったけど…

iPhoneでWebブラウジングしていて前のページに戻るのに
戻るボタンではなくスワイプで
戻る・進むが出来ないかなと思い、iOSdevCenterにある
ジェスチャーサンプルを組み込んで作ってみたけど
作りこみが甘いのか使いづらくスワイプを認識してくれないときも
あり、しかもスワイプを
認識してくれないと地味にイラッとしまうので微妙かなと思いました。


私が知らないだけで、あるかもしれないけど
このような戻る・進むって動作をスワイプで処理するブラウザがないのは
戻ったり・進んだりする処理は正確に認識する必要があるから
スワイプではなくボタンでしてるのかなぁと思ったり

ただもしかしたらスワイプで戻る・進むを行うにはスワイプを正確にキチキチっと認識するようにコードをを書けば
出来るんじゃないかなぁ

ちなみにコードはこちらにUPしました。
https://github.com/heart02/Browser

iCounter(カウンター)(ほぼ)完成

素材は自分で作れないのでiPhone SDKの教科書のサイトから
使わさせていただきました。
f:id:heart02:20120804124707j:plain

これで完成したんですが、
ふと思って今の状態で9999回カウント以上するとどうなるんだろうと
思い9999回カウントした状態にしたら桁が増えてとんでもないことになったので
ソースコードを直しました。
(9999回ボタンを押すのが面倒だったので9999カウントを足しました)
f:id:heart02:20120804125326p:plain


ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

IBOutlet UILabel *display;
    int count;
    
}
-(IBAction)add;
-(IBAction)subtract;
-(IBAction)clear;

@end


ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(void)updataDisplay
{
    display.text =[NSString stringWithFormat:@"%04d",count];
    
}


-(IBAction)add{             //+カウントする
    count = count + 1;
    if(count >= 9999)
        count = 9999;
    [self updataDisplay];
   // display.text = [NSString stringWithFormat:@"%d",count];
    
}

-(IBAction)subtract{
    count = count - 1;
    if(count < 0)
            count =0;
    [self updataDisplay];
    //display.text = [NSString stringWithFormat:@"%d",count];
}

-(IBAction)clear{
    
    count = 0;
    [self updataDisplay];
    //display.text = [NSString stringWithFormat:@"%d",count];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end


UIImageViewで背景画像の設置の仕方がわからなった

 以前に購入したiPhoneSDKの教科書を見ながらアプリを作っていたのだけど
背景画像配置で2時間くらい悩んでしまった。
 教科書には「Layoutメニューの「Send to Back」を選ぶ」と
書いてあるんだけどXcode4.2以降ではLayoutメニューが見つからず
検索してたら2chで解決方法が書いてあった。
 Editor -> Arrange -> Send to Front (最前面)、Send Forward (一つ前)
Layoutメニューは無いらしく上記のようにすれば良いらしいです。
f:id:heart02:20120804084102p:plain

Twitterトレンド(日本の)を表示するRubyプログラム

こちらをRubyからTwitter APIを呼び出してツイッターのトレンド情報を取得するプログラム - halcv's blog
ほぼ丸々コピーしてやっと出来た(T_T)
基礎からやっていかないと無理かなぁ

一応コードを載せとこう

#-*-codeing: utf-8-*-

require 'open-uri'
require 'rubygems'
require 'json'
require 'timeout'

json = nil

begin
	timeout(10){
url = "https://api.twitter.com/1/trends/1118370.json"
puts json  = URI(url).read
}

else
hash = JSON.parse(json)
	
	hash[0]["trends"].each{|trends|
		name = trends["name"]
		print(name,"\n")
}
end