• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

例えば、String <-> NSAttributedString

ruby>>
@objc(S2ASTransformer) class
S2ASTransformer : NSValueTransformer {

override static	func
transformedValueClass() -> AnyClass {
	return NSAttributedString.self
}
override static	func
allowsReverseTransformation() -> Bool {
	return true
}
override func
transformedValue( p: AnyObject? ) -> AnyObject {
	if let w = p as? String { return NSAttributedString( string: w ) } else { return NSAttributedString() }
}
override func
reverseTransformedValue( p: AnyObject? ) -> AnyObject {
	if let w = p as? NSAttributedString { return w.string } else { return "" }
}

}

<<--

こんなの作ってこうやって登録

ruby>>
func
applicationDidFinishLaunching( p: NSNotification ) {
NSValueTransformer.setValueTransformer( S2ASTransformer(), forName: "S2ASTransformer" )
}

<<--

posted by Saturn Saturn on Sat 26 Mar 2016 at 15:56 with 0 comments

ruby>>
// To use those functions below
// You need to import CommonCrypto.h in your bridging header.
// #import <CommonCrypto/CommonCrypto.h>

func
DataCryptedByAES( operation: CCOperation, p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
var wKeyLength = 0
if key.length < kCCKeySizeAES128 { assert( false ) }
else if key.length < kCCKeySizeAES192 { wKeyLength = kCCKeySizeAES128 }
else if key.length < kCCKeySizeAES256 { wKeyLength = kCCKeySizeAES192 }
else { wKeyLength = kCCKeySizeAES256 }

if iv != nil { assert( iv!.length == kCCBlockSizeAES128 ) }

var	wLength = UInt( ( ( p.length + kCCBlockSizeAES128 - 1 ) / kCCBlockSizeAES128 ) * kCCBlockSizeAES128 )
let	v = NSMutableData( length: Int( wLength ) )!
let	s: CCCryptorStatus = CCCrypt(
	operation
,	CCAlgorithm( kCCAlgorithmAES )
,	options
,	key.bytes
,	UInt( wKeyLength )
,	iv != nil ? iv!.bytes : nil
,	p.bytes
,	UInt( p.length )
,	v.mutableBytes
,	wLength
,	&wLength
)
v.length = Int( wLength )
return ( s, v )

}

func
DataEncryptedByAES( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
return DataCryptedByAES( CCOperation( kCCEncrypt ), p, key, options, iv )
}

func
DataDecryptedByAES( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
return DataCryptedByAES( CCOperation( kCCDecrypt ), p, key, options, iv )
}

func
DataCryptedByBlowfish( operation: CCOperation, p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
if iv != nil { assert( iv!.length == kCCBlockSizeBlowfish ) }

var	wLength = UInt( ( ( p.length + kCCBlockSizeBlowfish - 1 ) / kCCBlockSizeBlowfish ) * kCCBlockSizeBlowfish )
let	v = NSMutableData( length: Int( wLength ) )!
let	s: CCCryptorStatus = CCCrypt(
	operation
,	CCAlgorithm( kCCAlgorithmBlowfish )
,	options
,	key.bytes
,	UInt( key.length )
,	iv != nil ? iv!.bytes : nil
,	p.bytes
,	UInt( p.length )
,	v.mutableBytes
,	wLength
,	&wLength
)
v.length = Int( wLength )
return ( s, v )

}

func
DataEncryptedByBlowfish( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
return DataCryptedByBlowfish( CCOperation( kCCEncrypt ), p, key, options, iv )
}

func
DataDecryptedByBlowfish( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
return DataCryptedByBlowfish( CCOperation( kCCDecrypt ), p, key, options, iv )
}

//USAGE

func
UTF8Data( p: String ) -> NSData? {
return p.dataUsingEncoding( NSUTF8StringEncoding )
}

func
Usage() {
let wKey = UTF8Data( "teststring" )!
let wIV = UTF8Data( "12345678" )!
var ( s, data ) = DataEncryptedByBlowfish( UTF8Data( "testmessage" )!, wKey, CCOptions( kCCOptionPKCS7Padding ), wIV )
assert( Int( s ) == kCCSuccess )
( s, data ) = DataDecryptedByBlowfish( data, wKey, CCOptions( kCCOptionPKCS7Padding ), wIV )
assert( Int( s ) == kCCSuccess )
}

<<--

posted by Saturn Saturn on Thu 26 Mar 2015 at 08:46 with 0 comments

キーチェーンアクセス -> 証明書アシスタント -> 証明書を作成

OreOre.tiff

できた証明書を p12 で書き出し。

openssl pkcs12 -in OreOre.p12 -nodes -out OreOre.pem

でパスワードがついていない pem を作成。
この中に証明書も鍵も入っているので、tls.LoadX509KeyPair
の引数は両方とも "OreOre.pem" で大丈夫。

posted by Saturn Saturn on Wed 24 Dec 2014 at 22:17 with 0 comments

ruby>>
package main

import (
"net"
"sync"
"crypto/rand"
"crypto/tls"
"log"
"encoding/json"
)

func
main() {

cert, err := tls.LoadX509KeyPair( "OreOre.pem", "OreOre.pem" )
if err != nil { log.Fatalf( "LoadX509KeyPair:%v", err ) }
config := &tls.Config{
	Certificates: []tls.Certificate{ cert }, 
	ClientAuth: tls.VerifyClientCertIfGiven, 
	ServerName: "example.com",
	Rand:	rand.Reader,
}

connections := map[ net.Conn ] interface{} {}
mutex := sync.RWMutex{}

broadcaster := make( chan interface{} )
go func() {
	for {
		data, _ := json.Marshal( <- broadcaster )
		mutex.RLock()
		conns := connections
		mutex.RUnlock()
		for conn, _ := range conns { conn.Write( data ) }
	}
}()

listener, _ := tls.Listen( "tcp", ":6666", config )
defer listener.Close()
for {
	conn, _ := listener.Accept()
	mutex.Lock()
	connections[ conn ] = ""
	mutex.Unlock()
	go func() {
		decoder := json.NewDecoder( conn )
		for {
			var	w interface{}
			err := decoder.Decode( &w )
			if err != nil { break }
			broadcaster <- w 
		}
		mutex.Lock()
		delete( connections, conn )
		mutex.Unlock()
		conn.Close()
	}()
}

}
<<--

証明書がオレオレなので openssl で検証。

openssl s_client -tls1 -connect localhost:6666

posted by Saturn Saturn on Wed 24 Dec 2014 at 22:09 with 0 comments

ruby>>
package main

import (
"net"
"sync"
"encoding/json"
)

func
main() {

connections := map[ net.Conn ] interface{} {}
mutex := sync.RWMutex{}

broadcaster := make( chan interface{} )
go func() {
	for {
		data, _ := json.Marshal( <- broadcaster )
		mutex.RLock()
		conns := connections
		mutex.RUnlock()
		for conn, _ := range conns { conn.Write( data ) }
	}
}()

listener, _ := net.Listen( "tcp", ":6666" )
defer listener.Close()
for {
	conn, _ := listener.Accept()
	mutex.Lock()
	connections[ conn ] = ""
	mutex.Unlock()
	go func() {
		decoder := json.NewDecoder( conn )
		for {
			var	w interface{}
			err := decoder.Decode( &w )
			if err != nil { break }
			broadcaster <- w
		}
		mutex.Lock()
		delete( connections, conn )
		mutex.Unlock()
		conn.Close()
	}()
}

}
<<--

posted by Saturn Saturn on Wed 24 Dec 2014 at 22:07 with 0 comments

UITextField の場合

ruby>>
  → UITextFieldTextDidBeginEditingNotification
    UIKeyboardWillChangeFrameNotification
    UIKeyboardWillShowNotification
    UIKeyboardDidChangeFrameNotification
    UIKeyboardDidShowNotification

<<--

UITextView の場合

 
ruby>>
   UIKeyboardWillChangeFrameNotification
    UIKeyboardWillShowNotification
  → UITextViewTextDidBeginEditingNotification
    UIKeyboardDidChangeFrameNotification
    UIKeyboardDidShowNotification

<<--

iOS7, iOS8 両方

posted by Saturn Saturn on Thu 25 Sep 2014 at 18:06 with 0 comments

The code below is example which toggles view which covers status bar.

ruby>>
import UIKit

class
ViewController: UIViewController {

var	uWindow:	UIWindow!

@IBAction	func
Do( AnyObject! ) {
	if uWindow {
		uWindow = nil
	} else {
		uWindow = UIWindow( frame: UIApplication.sharedApplication().statusBarFrame )
		uWindow.backgroundColor = UIColor( white:0, alpha:0.5 )
		uWindow.windowLevel = UIWindowLevelStatusBar + 1
		uWindow.hidden = false;
	}
}

}

<<--

posted by Saturn Saturn on Mon 4 Aug 2014 at 18:25 with 0 comments

上部のバーのサイズ:69

〜 編集領域最上部:38

〜 編集領域最下部:320

〜 下部のバー上部:427-38-320

添付の画像を frame.origin.y == 69 で cameraOverlayView にセットすればフィットする。

Circle@2x.png

posted by Saturn Saturn on Wed 7 May 2014 at 19:53 with 0 comments
UIView のインスタンスの tag に値を設定した後、removeFromSuperview すると値は 0 にリセットされる(;;)
posted by Saturn Saturn on Mon 28 Apr 2014 at 20:23 with 0 comments

ユーザーが設定でロケーションの設定をいぢると、

didFailWithError

が呼ばれる。Grant したときでさえも code = 0 で呼ばれる!

ユーザーが設定でロケーションの許可を与えなかったとき、だからといってstopUptatingLocation を呼んでしまうと、その後許可を与えたことがわからなくなる。ネットの例はこれが多いので注意。

ruby>>
- (void)
locationManager:(CLLocationManager*)pLM
didFailWithError:(NSError*)pError
{ assert( [ pError.domain isEqual:kCLErrorDomain ] );
switch ( pError.code )
{
case 0: // User grant permission to access Location.
break;
case 1: // User change permittion to access Location to disabled, needs to uLocation be nil.
default: // But don't stop update location, you can't come back when user granted permission to access Location.
uLocation = nil;
break;
}
}
<<--

posted by Saturn Saturn on Mon 14 Apr 2014 at 17:44 with 0 comments