기본 콘텐츠로 건너뛰기

라벨이 swift인 게시물 표시

JavascriptCore in Swift 3 연습

//: Playground - noun: a place where people can play import UIKit import JavaScriptCore /* swift to javascript */ let str = "var double=function(x) { return x*2 };" let context = JSContext () context ?. evaluateScript ( str ) let getDouble = context ?. objectForKeyedSubscript ( "double" ) getDouble ?. call (withArguments: [ 1 ]). toString () *2를 하는 double이라는 function을 js에서 만들고 swift에서 인지 1을 넣어 2를 반환하는 예. 스위프트에서 자바스크립트 함수를 호출할 때는  objectForKeyedSubscript를 사용하여 함수명을 지정하고 call(withArguments: [, ... ]) 를 사용하여 인자를 지정한다. 결과값은 JSValue 형이므로 적당히 변환해서 사용한다. /* javascript to swfit */ let str2 = "postMessage('갔다가 다시 올꺼야')" let postMessageBlock : @convention(block) (AnyObject?) -> (AnyObject) = { (data) in     print(data)     return data! } context?.globalObject.setValue(unsafeBitCast(postMessageBlock, to: AnyObject.self), forProperty: "postMessage") context?.evaluateScript(str2) 반대의 경...

iOS/swift - segue 연결 후 중간에 가로채기 하고자 할때 : shouldPerformSegueWithIdentifier

segue로 storyboard를 연결해 놓고 특정 조건일 때 해당 segue로 연결을 막고 싶은 경우가 있다. 가령 특정 메뉴를 들어가고자 할땐 로그인이 되있지 않다면 로그인 화면으로 유도하려면 segue를 중간에 가로채야 한다. UIViewController 안에서 shouldPerformSegueWithIdentifier 를 사용하여 구현할 수 있다. NeedLoginSegue 로 이동할 경우 사용자 정보(  checkUserInformation ( userDefaults ) )가 false면 LoginSegue 를 열게 하는 코드는 다음과 같다.     override func shouldPerformSegueWithIdentifier(identifier: String , sender: AnyObject ?) -> Bool {         if identifier == "NeedLoginSegue" {              if checkUserInformation ( userDefaults )== false {                 self . performSegueWithIdentifier ( "LoginSegue" , sender: nil )             }             return false         }         return true     } unwind와 함께 빈번하게 쓰는 패턴이니...

iOS Swift 학습 로그

오래간만에 의뢰로 iOS app을 만들고 있다. 정말 많이 편해졌다. 놀랍다. swift만해도 벌써 몇번이나 업데이트 하고; 일단 pod 파일. cocoapod은 정말 너무 좋다. 먼저 프로젝트 생성 하고 Meteor DDP를 쓰기 위해 touch Podfile 하고 platform :ios, '8.0' use_frameworks! pod 'Meteor' 이렇게 쓰면 끝 pod install 한 뒤 *. xcworkspace 를 열면 끝. * 주의: install중 중간에 정지하면 기존 pod 가 망가질 수 있음 사용은 AppDelegate.swift를 열고 전역 변수로  let Meteor = METCoreDataDDPClient (serverURL: NSURL (string: "ws://www.YOURMETEORAPP.com/websocket" )!) 지정하고      func application(application: UIApplication , didFinishLaunchingWithOptions launchOptions: [ NSObject : AnyObject ]?) -> Bool {         Meteor . connect ()                  return true     } 이런 식으로 Application 기동 시 Meteor 접속. * swiftDDP 를 내장한 RealmMeteor가 더 좋아보인다. Storyboard 의 segue (세그웨이라고 읽음) 상당히 좋았다. 일단 첨에 진입점이 되는 Controller 에 Show Attributes Inspector를 열고  Is Initial View C...