[objective-c]UIWebViewでリンククリックで処理

UIWebViewでリンククリックで処理

読み込むhtml

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<a href="js-call://camera">camera</a><br/><br/>
<a href="js-call://album">album</a><br/><br/>
<a href="js-call://alert">alert</a><br/><br/>
</body>
</html>

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"shouldStartLoadWithRequest");
    NSString *requestString = [[request URL] absoluteString];
    NSString *protocol = @"js-call://";
    if ([requestString hasPrefix:protocol]) {
        NSString *requestContent = [requestString substringFromIndex:[protocol length]];
        NSArray *vals = [requestContent componentsSeparatedByString:@"/"];
        if ([vals[0] isEqualToString:@"camera"]){
            [self startCamera];//カメラ起動
        }else if ([vals[0] isEqualToString:@"album"]){
            [self startAlbum];//カメラロール起動
        }else {
            [webView stringByEvaluatingJavaScriptFromString:@"alert('Not defined');"];//jsのalert実行
        }
        return NO;
    }
    return YES;
}

[XCode]DropboxにGitリモートリポジトリを作る

DropboxにGitリモートリポジトリを作る

1) gitインストール
2) ターミナルで
「cd /Users/ユーザ名/Dropbox/git/プロジェクト名」
「git –bare init」
3) XCodeで対象プロジェクトを開く
4) Source Control -> プロジェクト名 – master -> Configure プロジェクト名
5) Remotesクリック
6) 左下+ -> Add Remote
7) Nameに任意の名前 Addresに「/Users/ユーザ名/Dropbox/git/プロジェクト名」を入力後「Add Remote」
8) Source Control -> Commit CommitMessageを入力して左下「Push to remote」チェック

[objective-c]UIViewをUIImageへ(Retina対応)

UIViewをUIImageへ(Retina対応)

-(UIImage*)getUIImageByUIView:(UIView*)view
{
    UIImage *screenImage;
    //UIGraphicsBeginImageContext(size);
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    screenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenImage;
}

[Ruby]formの値を受け取る

formの値を受け取る

サンプル

#!/usr/bin/ruby-1.9.3
# coding: utf-8
require 'cgi'

puts "Content-type: text/plain\n\n"
cgi = CGI.new
keys=cgi.keys
keys.each{|value|
    puts value+" = "+cgi[value]+"\n"
}

[Ruby]ファイルを読み込む

ファイルを読み込む

サンプル

#!/usr/bin/ruby-1.9.3
# coding: utf-8
require 'json'

puts "Content-type: application/json\n\n"

filename="test.txt"
results=Array.new

if File.exist?(filename)
    f = open(filename)
    f.each{|line|
        results.push(line.force_encoding("utf-8"))
    }
    f.close
else
    results.push("no data")
end

puts JSON.generate({'time'=>Time.now.strftime("%Y-%m-%d %H:%M:%S"), 'data'=>results})

[Ruby]MySQLからデータ取得

MySQLからデータ取得

サンプル

#!/usr/bin/ruby-1.9.3
# coding: utf-8
require 'json'
require 'mysql'

puts "Content-type: application/json\n\n"

connection = Mysql.connect('host','user','password','db name')
connection.query("set character set utf8") #文字化けするとき

columns=["ID","post_title","post_date"]
result = connection.query("SELECT "+columns.join(",")+" FROM wp_posts WHERE post_status='publish' ORDER BY ID DESC LIMIT 5")
connection.close

results=Array.new
result.each {|record|
    w = {}
    record.each_with_index{|val,i|
        key = columns[i]
        w[key.force_encoding("utf-8")]=val.force_encoding("utf-8") #文字化けするときforce_encoding
    }
    results.push(w)
}
puts JSON.generate({'time'=>Time.now.strftime("%Y-%m-%d %H:%M:%S"), 'data'=>results})

[MySQL]MySQLの文字コードの設定を確認する

MySQLの文字コードの設定を確認する

クエリ

SHOW VARIABLES LIKE 'character_set%'

結果

Variable_name Value
character_set_client utf8
character_set_connection utf8
character_set_database utf8
character_set_filesystem binary
character_set_results utf8
character_set_server utf8
character_set_system utf8
character_sets_dir /usr/share/mysql/charsets/

[Ruby]jsonを出力する

jsonを出力する

サンプル

#coding:utf-8
require 'json'
print("Content-type: application/json\n\n")

print JSON.generate({"time"=>Time.now.strftime("%Y-%m-%d %H:%M:%S"),
                    "user"=>[
                                {"id" => 32, "name"=>"jeff"},
                                {"id" => 56, "name"=>"john"}
                            ]
                    })