• 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

   1  @objc(S2ASTransformer) class
   2  S2ASTransformer	:	NSValueTransformer {
   3  
   4  	override static	func
   5  	transformedValueClass() -> AnyClass {
   6  		return NSAttributedString.self
   7  	}
   8  	override static	func
   9  	allowsReverseTransformation() -> Bool {
  10  		return true
  11  	}
  12  	override func
  13  	transformedValue( p: AnyObject? ) -> AnyObject {
  14  		if let w = p as? String { return NSAttributedString( string: w ) } else { return NSAttributedString() }
  15  	}
  16  	override func
  17  	reverseTransformedValue( p: AnyObject? ) -> AnyObject {
  18  		if let w = p as? NSAttributedString { return w.string } else { return "" }
  19  	}
  20  
  21  }
  22  

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

   1  	func
   2  	applicationDidFinishLaunching( p: NSNotification ) {
   3  		NSValueTransformer.setValueTransformer( S2ASTransformer(), forName: "S2ASTransformer" )
   4  	}
   5  	
   6  

posted by Face Saturn on Sat 26 Mar 2016 at 15:56

   1  //	To use those functions below
   2  //	You need to import CommonCrypto.h in your bridging header.
   3  //		#import <CommonCrypto/CommonCrypto.h>
   4  
   5  func
   6  DataCryptedByAES( operation: CCOperation, p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
   7  	var	wKeyLength = 0
   8  	if		key.length < kCCKeySizeAES128	{ assert( false ) }
   9  	else if	key.length < kCCKeySizeAES192	{ wKeyLength = kCCKeySizeAES128 }
  10  	else if	key.length < kCCKeySizeAES256	{ wKeyLength = kCCKeySizeAES192 }
  11  	else									{ wKeyLength = kCCKeySizeAES256 }
  12  
  13  	if iv != nil { assert( iv!.length == kCCBlockSizeAES128 ) }
  14  
  15  	var	wLength = UInt( ( ( p.length + kCCBlockSizeAES128 - 1 ) / kCCBlockSizeAES128 ) * kCCBlockSizeAES128 )
  16  	let	v = NSMutableData( length: Int( wLength ) )!
  17  	let	s: CCCryptorStatus = CCCrypt(
  18  		operation
  19  	,	CCAlgorithm( kCCAlgorithmAES )
  20  	,	options
  21  	,	key.bytes
  22  	,	UInt( wKeyLength )
  23  	,	iv != nil ? iv!.bytes : nil
  24  	,	p.bytes
  25  	,	UInt( p.length )
  26  	,	v.mutableBytes
  27  	,	wLength
  28  	,	&wLength
  29  	)
  30  	v.length = Int( wLength )
  31  	return ( s, v )
  32  }
  33  
  34  func
  35  DataEncryptedByAES( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
  36  	return DataCryptedByAES( CCOperation( kCCEncrypt ), p, key, options, iv )
  37  }
  38  
  39  func
  40  DataDecryptedByAES( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
  41  	return DataCryptedByAES( CCOperation( kCCDecrypt ), p, key, options, iv )
  42  }
  43  
  44  func
  45  DataCryptedByBlowfish( operation: CCOperation, p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
  46  	if iv != nil { assert( iv!.length == kCCBlockSizeBlowfish ) }
  47  
  48  	var	wLength = UInt( ( ( p.length + kCCBlockSizeBlowfish - 1 ) / kCCBlockSizeBlowfish ) * kCCBlockSizeBlowfish )
  49  	let	v = NSMutableData( length: Int( wLength ) )!
  50  	let	s: CCCryptorStatus = CCCrypt(
  51  		operation
  52  	,	CCAlgorithm( kCCAlgorithmBlowfish )
  53  	,	options
  54  	,	key.bytes
  55  	,	UInt( key.length )
  56  	,	iv != nil ? iv!.bytes : nil
  57  	,	p.bytes
  58  	,	UInt( p.length )
  59  	,	v.mutableBytes
  60  	,	wLength
  61  	,	&wLength
  62  	)
  63  	v.length = Int( wLength )
  64  	return ( s, v )
  65  }
  66  
  67  func
  68  DataEncryptedByBlowfish( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
  69  	return DataCryptedByBlowfish( CCOperation( kCCEncrypt ), p, key, options, iv )
  70  }
  71  
  72  func
  73  DataDecryptedByBlowfish( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
  74  	return DataCryptedByBlowfish( CCOperation( kCCDecrypt ), p, key, options, iv )
  75  }
  76  
  77  
  78  //USAGE
  79  
  80  func
  81  UTF8Data( p: String ) -> NSData? {
  82  	return p.dataUsingEncoding( NSUTF8StringEncoding )
  83  }
  84  
  85  func
  86  Usage() {
  87  	let	wKey = UTF8Data( "teststring" )!
  88  	let	wIV = UTF8Data( "12345678" )!
  89  	var	( s, data ) = DataEncryptedByBlowfish( UTF8Data( "testmessage" )!, wKey, CCOptions( kCCOptionPKCS7Padding ), wIV )
  90  	assert( Int( s ) == kCCSuccess )
  91  	( s, data ) = DataDecryptedByBlowfish( data, wKey, CCOptions( kCCOptionPKCS7Padding ), wIV )
  92  	assert( Int( s ) == kCCSuccess )
  93  }
  94  

posted by Face Saturn on Thu 26 Mar 2015 at 08:45

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

OreOre.tiff

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

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

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

posted by Face Saturn on Wed 24 Dec 2014 at 22:14

   1  package main
   2   
   3  import (
   4  	"net"
   5  	"sync"
   6  	"crypto/rand"
   7  	"crypto/tls"
   8  	"log"
   9  	"encoding/json"
  10  )
  11   
  12  
  13  func
  14  main() {
  15  
  16  	cert, err := tls.LoadX509KeyPair( "OreOre.pem", "OreOre.pem" )
  17  	if err != nil { log.Fatalf( "LoadX509KeyPair:%v", err ) }
  18  	config := &tls.Config{
  19  		Certificates: []tls.Certificate{ cert }, 
  20  		ClientAuth: tls.VerifyClientCertIfGiven, 
  21  		ServerName: "example.com",
  22  		Rand:	rand.Reader,
  23  	}
  24  
  25  	connections := map[ net.Conn ] interface{} {}
  26  	mutex := sync.RWMutex{}
  27  
  28  	broadcaster := make( chan interface{} )
  29  	go func() {
  30  		for {
  31  			data, _ := json.Marshal( <- broadcaster )
  32  			mutex.RLock()
  33  			conns := connections
  34  			mutex.RUnlock()
  35  			for conn, _ := range conns { conn.Write( data ) }
  36  		}
  37  	}()
  38  
  39  	listener, _ := tls.Listen( "tcp", ":6666", config )
  40  	defer listener.Close()
  41  	for {
  42  		conn, _ := listener.Accept()
  43  		mutex.Lock()
  44  		connections[ conn ] = ""
  45  		mutex.Unlock()
  46  		go func() {
  47  			decoder := json.NewDecoder( conn )
  48  			for {
  49  				var	w interface{}
  50  				err := decoder.Decode( &w )
  51  				if err != nil { break }
  52  				broadcaster <- w 
  53  			}
  54  			mutex.Lock()
  55  			delete( connections, conn )
  56  			mutex.Unlock()
  57  			conn.Close()
  58  		}()
  59  	}
  60  }

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

openssl s_client -tls1 -connect localhost:6666

posted by Face Saturn on Wed 24 Dec 2014 at 22:09

   1  package main
   2   
   3  import (
   4  	"net"
   5  	"sync"
   6  	"encoding/json"
   7  )
   8   
   9  func
  10  main() {
  11  
  12  	connections := map[ net.Conn ] interface{} {}
  13  	mutex := sync.RWMutex{}
  14  
  15  	broadcaster := make( chan interface{} )
  16  	go func() {
  17  		for {
  18  			data, _ := json.Marshal( <- broadcaster )
  19  			mutex.RLock()
  20  			conns := connections
  21  			mutex.RUnlock()
  22  			for conn, _ := range conns { conn.Write( data ) }
  23  		}
  24  	}()
  25  
  26  	listener, _ := net.Listen( "tcp", ":6666" )
  27  	defer listener.Close()
  28  	for {
  29  		conn, _ := listener.Accept()
  30  		mutex.Lock()
  31  		connections[ conn ] = ""
  32  		mutex.Unlock()
  33  		go func() {
  34  			decoder := json.NewDecoder( conn )
  35  			for {
  36  				var	w interface{}
  37  				err := decoder.Decode( &w )
  38  				if err != nil { break }
  39  				broadcaster <- w
  40  			}
  41  			mutex.Lock()
  42  			delete( connections, conn )
  43  			mutex.Unlock()
  44  			conn.Close()
  45  		}()
  46  	}
  47  }

posted by Face Saturn on Wed 24 Dec 2014 at 22:07

UITextField の場合

   1    → UITextFieldTextDidBeginEditingNotification
   2      UIKeyboardWillChangeFrameNotification
   3      UIKeyboardWillShowNotification
   4      UIKeyboardDidChangeFrameNotification
   5      UIKeyboardDidShowNotification
   6  

UITextView の場合

 

   1     UIKeyboardWillChangeFrameNotification
   2      UIKeyboardWillShowNotification
   3    → UITextViewTextDidBeginEditingNotification
   4      UIKeyboardDidChangeFrameNotification
   5      UIKeyboardDidShowNotification
   6  

iOS7, iOS8 両方

posted by Face Saturn on Thu 25 Sep 2014 at 18:06

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

   1  import UIKit
   2  
   3  class
   4  ViewController: UIViewController {
   5  
   6  	var	uWindow:	UIWindow!
   7  	
   8  	@IBAction	func
   9  	Do( AnyObject! ) {
  10  		if uWindow {
  11  			uWindow = nil
  12  		} else {
  13  			uWindow = UIWindow( frame: UIApplication.sharedApplication().statusBarFrame )
  14  			uWindow.backgroundColor = UIColor( white:0, alpha:0.5 )
  15  			uWindow.windowLevel = UIWindowLevelStatusBar + 1
  16  			uWindow.hidden = false;
  17  		}
  18  	}
  19  }
  20  

posted by Face Saturn on Mon 4 Aug 2014 at 18:25

上部のバーのサイズ:69

〜 編集領域最上部:38

〜 編集領域最下部:320

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

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

Circle@2x.png

posted by Face Saturn on Wed 7 May 2014 at 19:52

UIView のインスタンスの tag に値を設定した後、removeFromSuperview すると値は 0 にリセットされる(;;)

posted by Face Saturn on Mon 28 Apr 2014 at 20:23

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

didFailWithError

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

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

   1  -	(void)
   2  locationManager:(CLLocationManager*)pLM
   3  didFailWithError:(NSError*)pError
   4  {	assert( [ pError.domain isEqual:kCLErrorDomain ] );
   5  	switch ( pError.code )
   6  	{
   7  	case  0:	//	User grant permission to access Location.
   8  		break;
   9  	case  1:	//	User change permittion to access Location to disabled, needs to uLocation be nil.
  10  	default:	//	But don't stop update location, you can't come back when user granted permission to access Location.
  11  		uLocation = nil;
  12  		break;
  13  	}
  14  }

posted by Face Saturn on Mon 14 Apr 2014 at 17:44
Contents
Value Transformer
AES & Blowfish in swift (CBC)
OreOre.pem の作り方
JSON Chat Server Skelton in GoLang TLS Ver.
JSON Chat Server Skelton in GoLang
UITextView と、UITextField でキーボードが出た時の Notification の順番が違う
Making window which covers iPhone's Status Bar
UIImagePickerController の編集領域
UIView の tag property
UILocationManager
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ