본문 바로가기

프로그래밍/iOS-Swift29

[SWIFT] AVPlayer Background 재생 핸드폰이 슬립모드 가거나, 전원을 끄거나, 앱을 잠시 숨겨놓거나 등... 앱이 background로 이동하면 모든 활동이 멈춘다. 이때, 오디오 재생을 background에서도 재생하고 싶다면?! 한 줄만 코딩하면 된다. 백그라운드 재생 설정 do { try AVAudioSession.sharedInstance().setCategory(.playback) } catch { } 2021. 8. 3.
[SWIFT] Wifi 연결하기 코드 상에서 wifi를 연결할 수 있다. 하지만... 신호가 안 좋은 와이파이일 경우 "연결할 수 없습니다"라는 팝업이 종종 뜨긴 한다 라이브러리 import NetworkExtension Wifi 연결하기 func connectToWifi(wifiName: String, wifiPassword: String, wep: Bool, completion: @escaping ((_ error: Bool) -> Void) ) { let hotspotConfig = NEHotspotConfiguration(ssid: wifiName, passphrase: wifiPassword, isWEP: wep) hotspotConfig.joinOnce = true NEHotspotConfigurationManager.sha.. 2021. 8. 3.
[SWIFT] 퍼센트 인코딩을 character로 변환 후.. 개삽질했다. url을 player로 재생하려고 했는데 자꾸 재생이 안돼서.... 2시간 버린 것 같다 ㅜ 알고보니 url이 퍼센트 인코딩이 되어있어 ios, mac에서는 재생이 안됐다. 예) let stringUrl = "http://www.test.com/mp3file/test%28testfile%29.mp3" 퍼센트 인코딩 ▶ character if let url = stringUrl.removingPercentEncoding { print("url : \(url)") } 다음과 같이 하면 퍼센트 인코딩을 없앨 수 있다 ㅜㅜㅜㅜㅜ 2021. 7. 29.
[SWIFT] 오늘 날짜 & 시간, swift string to date, swift date to string 이번 포스팅에서는 date 관련해서 다뤄보려고 한다. 현재 날짜 & 시간 구하기 func getNowTime() -> String { let now = Date() let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" dateFormatter.timeZone = NSTimeZone(name: "ko_KR") as TimeZone? return dateFormatter.string(from: now) } String to Date func getStringToDate(strDate:String, format:String) -> Date { let dateFormatter = DateFormatte.. 2021. 7. 12.
[SWIFT] Web Socket (Stomp) 오늘은 웹소켓, 그중에서도 stomp를 다뤄보려고 한다. 이유는.. stomp client 쪽을 개발하면서 진짜 삽질도 많이 하고 게다가 swift stomp web socket에 대한 정보가 많이 없기 때문이었다.. 그래서 내가 경험하고 부족한 부분들은 어떻게 처리했는지 포스팅 해보려고 한다. 사용 라이브러리 : StompClientLib Podfile ... target 'Project_Name' do pod 'StompClientLib' ... 다음과 같이 작성하고 pod install ! 기본 예제 import StompClientLib class StompManager { static let shared:StompManager = StompManager() private let url = URL.. 2021. 7. 9.
[SWIFT] iphone 슬립 모드(화면 자동 잠금) 방지 화면 자동 잠금을 설정해 놓은 경우.. 앱에서 소켓 통신 시 소켓이 끊겨버린다. 이 때문에 슬립 모드 방지하는 방법을 찾아냈다. UIApplication.shared.isIdleTimerDisabled = true 짜잔. 위의 한 줄이면 화면 자동 잠금을 설정해놔도 앱이 실행되고 있는 동안에는 화면이 꺼지지 않는다.!!!!! 위의 코드는 SceneDelegate.swift 파일에 scene 함수 안에다 작성해두면 된다. func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let _ = (scene as? UIWindowScen.. 2021. 7. 7.
[SWIFT] Root View 변경하기 앱을 개발하다 보면 뷰가 계속 쌓여서 신경쓰일 때가 있다. 이럴 때, root view를 변경하고 나머지 뷰를 없애고 싶어진다. Root View 변경하기 extension UIWindow { func replaceRootViewController(_ replacementController: UIViewController, animated: Bool, completion: (() -> Void)?) { let snapshotImageView = UIImageView(image: self.snapshot()) self.addSubview(snapshotImageView) let dismissCompletion = { () -> Void in // dismiss all modal view controller.. 2021. 7. 6.
[SWIFT] 최상위 뷰 구하기 우선, 최상위 뷰를 구하기 전에 필요한 코드가 있다. UIApplication.shared.keyWindow? ~~ 하지만 위의 코드를 사용해도 빌드에는 문제가 없지만, iOS 13부터는 deprecated 되었다고 경고문이 뜬다. 그래서 대체할 코드를 전역 변수로 선언해 놓자! let keyWindow = UIApplication.shared.connectedScenes .filter({$0.activationState == .foregroundActive}) .map({$0 as? UIWindowScene}) .compactMap({$0}) .first?.windows .filter({$0.isKeyWindow}).first 최상위 뷰 구하기 extension UIWindow { public var .. 2021. 7. 6.
[SWIFT] URL to UIImage 요즘 블로그 포스팅이 뜸했다.... 3개월 간 맨 땅에 헤딩하면서 iOS 앱 개발을 하느라 정신이 없었다... 이제 여유가 좀 생겼으니 여태 개발하면서 도움이 된 정보들을 포스팅 하려고 한다. URL을 UIImage로 변환하기 func urlToImage(strUrl:String) -> UIImage? { if strUrl.isEmpty || strUrl.count == 0 { return nil } do { let url = URL(string: strUrl) if url != nil { let data = try Data(contentsOf: url!) return UIImage(data: data) } } catch (let error) { print("\(error)") } return nil }.. 2021. 7. 6.
[SWIFT] IBDesignable 에러 대응 방법 medium.com/@esung/ibdesignable-%EC%97%90%EB%9F%AC%EC%97%90-%EB%8C%80%EC%9D%91%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95-254001a648f6 IBDesignable 에러에 대응하는 방법 Originally published at www.notion.so. medium.com 2021. 4. 9.