Post

Replies

Boosts

Views

Activity

how to extract the hostname from a https/tls request in NEFilterSocketFlow
Hi guys, I try to create a content filter app by using network extension api. When it comes to a https/tls remote endpoint, the remoteEndpoint.hostname will always be "" instead of the actual hostname. How can I extract the actual hostname? private func filterTraffic(flow: NEFilterSocketFlow) -> NEFilterNewFlowVerdict { // Default action from settings will be used if no rules match logger.error("filter traffic...") guard let remoteEndpoint = flow.remoteEndpoint as? NWHostEndpoint else { logger.error("not a NWHostEndpoint)") return .allow() } logger.error("host name: \(remoteEndpoint.hostname)") if remoteEndpoint.hostname.hasSuffix("google.com"){ logger.error("google.com") return .drop() } return .allow() } code-block
1
0
323
Jun ’25
How to create a socket connection and handle data stream in swift ?
Hi, I try to create a socket client to connect to a server, and get/send data as well. My code as below, it connected successfully to the server, but I don't how to receive /send data. Can somebody help me out of this? import Foundation import CoreFoundation class SocketClient {          private(set) var host: String     private(set) var port: Int     private var socket: CFSocket?          init(_ host:String, _ port: Int){         self.host = host         self.port = port     }     func connect(){         //create socket         self.socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, CFSocketCallBackType.dataCallBack.rawValue, {             socket, callBackType, address, data, info in             if CFSocketCallBackType.dataCallBack == callBackType && data != nil {                 debugPrint("incomming。。。")                              }         }, nil)                  guard self.socket != nil else{             debugPrint("create socket failed")             return         }         //binding ip         var address: sockaddr_in = sockaddr_in()         address.sin_family = sa_family_t(PF_INET)         address.sin_port = in_port_t(self.port)         var inAddr = in_addr()         inAddr.s_addr = inet_addr(self.host)         address.sin_addr = inAddr         let addrData = CFDataCreate(kCFAllocatorDefault, &address, MemoryLayout.size(ofValue: address))         let result = CFSocketConnectToAddress(self.socket, addrData, 5)         if result == .success {             debugPrint("connect succeed")         } else {             debugPrint("connect failed")         }         let source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, self.socket, 1)         CFRunLoopAddSource(CFRunLoopGetCurrent(), source, .commonModes)     } }
0
0
461
Oct ’22
how can i get two wheel picker in one row display correctly
How can I get two wheel picker in one row display correctly?I tried to use HStack, but it did not work. see my code below: import SwiftUI @available(iOS 15.0, *) struct ContentView: View {       @State var min:Int = 0   @State var sec:Int = 0       var body: some View {     Form{       Section{         HStack{           Picker("min", selection: $min){             Text("0")               .tag(0)             Text("1")               .tag(1)             Text("2")               .tag(2)             Text("3")               .tag(3)                         }           .pickerStyle(.wheel)           Picker("sec", selection: $sec){             Text("0")               .tag(0)             Text("1")               .tag(1)             Text("2")               .tag(2)             Text("3")               .tag(3)                         }           .pickerStyle(.wheel)                     }         .frame(height:60)       }     }   } } @available(iOS 15.0, *) struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }
1
0
2.2k
Mar ’22
DispatchSource compile error: Cannot assign value of type '()' to type 'DispatchSourceTimer?'
After I installed xcode13 and updated pods of my swiftui project by executing pod update, it occurred an unexpected compile error when I tried to run my project. Cannot assign value of type '()' to type 'DispatchSourceTimer?' Is this a new bug? or I did something wrong? See my code below: //use case for counting down time self.timer = DispatchSource.dispatchTimer(timeInterval: 1, handler: { dispatchTimer in       self.time -= 1       if self.time < 0 {         dispatchTimer?.cancel()         self.next()       }     }, needRepeat: true) //DispatchSource extension function public extension DispatchSource {   class func dispatchTimer(timeInterval: Double, handler: @escaping (DispatchSourceTimer?) -> Void, needRepeat: Bool) {           let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)     timer.schedule(deadline: .now(), repeating: timeInterval)     timer.setEventHandler {       DispatchQueue.main.async {         if needRepeat {           handler(timer)         } else {           timer.cancel()           handler(nil)         }       }     }     timer.resume()   } } Please help!
1
0
688
Oct ’21
How to add a search bar to a list?
Hi guys, I try to use .searchable to add search bar to a list, see my code as below, but it doesn't work. How can I make it work? Somebody, please help me!!! import SwiftUI @available(iOS 15.0, *) struct ContentView: View {       var body: some View {     NavigationView {       TabView{         VStack{           List{             Text("aaa")             Text("bbb")             Text("ccc")           }           .searchable(text: .constant(""))         }         .tabItem({           Text("tab1")                     })         VStack{           Text("tab2")         }         .tabItem({           Text("tab2")                     })                 }             }         } } @available(iOS 15.0, *) struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }
1
0
1.7k
Oct ’21
how to extract the hostname from a https/tls request in NEFilterSocketFlow
Hi guys, I try to create a content filter app by using network extension api. When it comes to a https/tls remote endpoint, the remoteEndpoint.hostname will always be "" instead of the actual hostname. How can I extract the actual hostname? private func filterTraffic(flow: NEFilterSocketFlow) -> NEFilterNewFlowVerdict { // Default action from settings will be used if no rules match logger.error("filter traffic...") guard let remoteEndpoint = flow.remoteEndpoint as? NWHostEndpoint else { logger.error("not a NWHostEndpoint)") return .allow() } logger.error("host name: \(remoteEndpoint.hostname)") if remoteEndpoint.hostname.hasSuffix("google.com"){ logger.error("google.com") return .drop() } return .allow() } code-block
Replies
1
Boosts
0
Views
323
Activity
Jun ’25
How to create a socket connection and handle data stream in swift ?
Hi, I try to create a socket client to connect to a server, and get/send data as well. My code as below, it connected successfully to the server, but I don't how to receive /send data. Can somebody help me out of this? import Foundation import CoreFoundation class SocketClient {          private(set) var host: String     private(set) var port: Int     private var socket: CFSocket?          init(_ host:String, _ port: Int){         self.host = host         self.port = port     }     func connect(){         //create socket         self.socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, CFSocketCallBackType.dataCallBack.rawValue, {             socket, callBackType, address, data, info in             if CFSocketCallBackType.dataCallBack == callBackType && data != nil {                 debugPrint("incomming。。。")                              }         }, nil)                  guard self.socket != nil else{             debugPrint("create socket failed")             return         }         //binding ip         var address: sockaddr_in = sockaddr_in()         address.sin_family = sa_family_t(PF_INET)         address.sin_port = in_port_t(self.port)         var inAddr = in_addr()         inAddr.s_addr = inet_addr(self.host)         address.sin_addr = inAddr         let addrData = CFDataCreate(kCFAllocatorDefault, &address, MemoryLayout.size(ofValue: address))         let result = CFSocketConnectToAddress(self.socket, addrData, 5)         if result == .success {             debugPrint("connect succeed")         } else {             debugPrint("connect failed")         }         let source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, self.socket, 1)         CFRunLoopAddSource(CFRunLoopGetCurrent(), source, .commonModes)     } }
Replies
0
Boosts
0
Views
461
Activity
Oct ’22
How to get iPhone's IP address in swift
How to get iPhone's IP address in swift?
Replies
1
Boosts
0
Views
470
Activity
Oct ’22
SwiftUI launch screen storyboard
why I put a circle clipped image in the launch screen story board but I got a rectangle one when I run it ?
Replies
2
Boosts
0
Views
638
Activity
Oct ’22
how to get user's weight by using HealthKit in swiftui
Hey, guys. I try to get user's weight by using HealthKit in SwiftUI. can anyone give a example to show me how to do it?
Replies
1
Boosts
0
Views
1.4k
Activity
Oct ’22
Can coredata entity's attribute store enum data type?
Can CoreData entity's attribute store enum ? How to do that?
Replies
1
Boosts
0
Views
1.6k
Activity
Sep ’22
how can i get two wheel picker in one row display correctly
How can I get two wheel picker in one row display correctly?I tried to use HStack, but it did not work. see my code below: import SwiftUI @available(iOS 15.0, *) struct ContentView: View {       @State var min:Int = 0   @State var sec:Int = 0       var body: some View {     Form{       Section{         HStack{           Picker("min", selection: $min){             Text("0")               .tag(0)             Text("1")               .tag(1)             Text("2")               .tag(2)             Text("3")               .tag(3)                         }           .pickerStyle(.wheel)           Picker("sec", selection: $sec){             Text("0")               .tag(0)             Text("1")               .tag(1)             Text("2")               .tag(2)             Text("3")               .tag(3)                         }           .pickerStyle(.wheel)                     }         .frame(height:60)       }     }   } } @available(iOS 15.0, *) struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }
Replies
1
Boosts
0
Views
2.2k
Activity
Mar ’22
DispatchSource compile error: Cannot assign value of type '()' to type 'DispatchSourceTimer?'
After I installed xcode13 and updated pods of my swiftui project by executing pod update, it occurred an unexpected compile error when I tried to run my project. Cannot assign value of type '()' to type 'DispatchSourceTimer?' Is this a new bug? or I did something wrong? See my code below: //use case for counting down time self.timer = DispatchSource.dispatchTimer(timeInterval: 1, handler: { dispatchTimer in       self.time -= 1       if self.time < 0 {         dispatchTimer?.cancel()         self.next()       }     }, needRepeat: true) //DispatchSource extension function public extension DispatchSource {   class func dispatchTimer(timeInterval: Double, handler: @escaping (DispatchSourceTimer?) -> Void, needRepeat: Bool) {           let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)     timer.schedule(deadline: .now(), repeating: timeInterval)     timer.setEventHandler {       DispatchQueue.main.async {         if needRepeat {           handler(timer)         } else {           timer.cancel()           handler(nil)         }       }     }     timer.resume()   } } Please help!
Replies
1
Boosts
0
Views
688
Activity
Oct ’21
How to add a search bar to a list?
Hi guys, I try to use .searchable to add search bar to a list, see my code as below, but it doesn't work. How can I make it work? Somebody, please help me!!! import SwiftUI @available(iOS 15.0, *) struct ContentView: View {       var body: some View {     NavigationView {       TabView{         VStack{           List{             Text("aaa")             Text("bbb")             Text("ccc")           }           .searchable(text: .constant(""))         }         .tabItem({           Text("tab1")                     })         VStack{           Text("tab2")         }         .tabItem({           Text("tab2")                     })                 }             }         } } @available(iOS 15.0, *) struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }
Replies
1
Boosts
0
Views
1.7k
Activity
Oct ’21