Error when integrate Sinch third-party library in Swift? - swift3

I want to build the video call function in my app and i decided to choose Sinch framework. But when i press the call, seem the localView can not track. Please help me how to fix this. Thank a lot. Here is my code get error:
func videoController() -> SINVideoController {
let appDel = UIApplication.shared.delegate as! AppDelegate
return (appDel.client?.videoController())!
}
I got this error in return line:
Thread 1: EXC_BREAKPOINT (code=1, subcode=0x102bcc740)
Here is my code:
override func viewDidLoad() {
super.viewDidLoad()
sinCall?.delegate = self
playSound()
if sinCall?.direction == SINCallDirection.incoming {
lbReceiverName.text = "Đang gọi"
_ = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateLabelForReceiver), userInfo: nil, repeats: true)
if let id = senderName {
databaseRefenrence.child("Users").child("\(id)").observeSingleEvent(of: .value, with: { (snapshot) in
if let dict = snapshot.value as? [String : Any] {
self.lbDangGoi.text = dict["Name"] as? String
}
})
}
localView.isHidden = false
btAccept.isHidden = false
btDecline.isHidden = false
btEnd.isHidden = true
} else {
_ = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateLabelForSender), userInfo: nil, repeats: true)
if let name = receiverName {
lbReceiverName.text = name
}
localView.isHidden = false
btAccept.isHidden = true
btDecline.isHidden = true
btEnd.isHidden = false
}
if sinCall?.details.isVideoOffered == true {
localView.addSubview((videoController().localView())!)
}
}
func callDidAddVideoTrack(_ call: SINCall!) {
remoteView.addSubview((videoController().remoteView())!)
}

Related

How to keep reference of data when using ObservableObject

I am new to Swiftui and I struggle to understand how to properly retain data created in ObservableObject when rendering views? Or a completely different approach to the problem maybe?
More specifically, it is about getting HTTP data in each row in a List().
Right now, it makes the HTTP call far too often when parent views are rendered, which causes all rows to be reloaded.
The same issue can be found here: Keep reference on view/data model after View update
public class VideoFetcher: ObservableObject {
#Published var video: VideoResponse?
#Published var coverImage: UIImage?
#Published var coverImageLoading = false
#Published var categories: String?
#Published var loading = false
#Published var error = false
func load(mediaItemSlug: String = "", broadcasterSlug: String = "") {
self.loading = true
Video.findBySlug(
mediaItemSlug: mediaItemSlug,
broadcasterSlug: broadcasterSlug,
successCallback: {video -> Void in
self.video = video
self.loading = false
self.setCategories()
self.loadCoverImage()
},
errorCallback: {(error, _) -> Void in
self.loading = false
self.error = true
})
}
func loadCoverImage() {
guard self.video!.coverImageUrl != "" else {
return
}
self.coverImageLoading = true
let downloader = ImageDownloader()
let urlRequest = URLRequest(url: URL(string: self.video!.coverImageUrl)!)
let filter = AspectScaledToFillSizeFilter(size: CGSize(width: 520.0, height: 292.499999963))
downloader.download(urlRequest, filter: filter) { response in
if case .success(let image) = response.result {
self.coverImage = image
self.coverImageLoading = false
}
}
}
func setCategories() {
if (self.video!.broadcaster.categories.count > 0) {
let categoryNames = self.video!.broadcaster.categories.map { category in
return category.name == "" ? "(no name)" : category.name
}
self.categories = categoryNames.joined(separator: " • ");
}
}
}
List() row:
struct VideoCard: View {
#ObservedObject var fetcher = VideoFetcher()
...
init() {
// Causes reload each render
self.fetcher.load()
}
var body: some View {
...
.onAppear {
// Loads that on appear but fetcher.video is nil after view re-rendered because load() wasn't called
self.fetcher.load()
}
}
}
Thanks, Chris. I thought I was doing something wrong on an architectural level but I added caching and that solved my problem.
import Alamofire
import AlamofireImage
import Cache
public class VideoFetcher: ObservableObject {
#Published var video: VideoResponse?
#Published var coverImage: UIImage?
#Published var coverImageLoading = false
#Published var broadcasterImage: UIImage?
#Published var categories: String?
#Published var loading = false
#Published var error = false
func load(mediaItemSlug: String = "", broadcasterSlug: String = "") {
let videoCache = try? AppCache.video!.object(forKey: mediaItemSlug)
if (videoCache != nil) {
self.video = videoCache
self.setCategories()
self.loadCoverImage()
return
}
self.loading = true
Video.findBySlug(
mediaItemSlug: mediaItemSlug,
broadcasterSlug: broadcasterSlug,
successCallback: {video -> Void in
try? AppCache.video!.setObject(video, forKey: mediaItemSlug)
self.video = video
self.loading = false
self.setCategories()
self.loadCoverImage()
self.loadBroadcasterImage()
},
errorCallback: {(error, _) -> Void in
self.loading = false
self.error = true
})
}
func loadCoverImage() {
let coverImageUrl = self.video!.coverImageUrl
guard coverImageUrl != "" else {
return
}
let urlRequest = URLRequest(url: URL(string: coverImageUrl)!)
let cachedImage = AppCache.image!.image(for: urlRequest, withIdentifier: coverImageUrl)
if (cachedImage != nil) {
self.coverImage = cachedImage
return
}
self.coverImageLoading = true
let downloader = ImageDownloader(imageCache: AppCache.image!)
let filter = AspectScaledToFillSizeFilter(size: CGSize(width: 520.0, height: 292.499999963))
downloader.download(urlRequest, filter: filter) { response in
if case .success(let image) = response.result {
AppCache.image!.add(image, for: urlRequest, withIdentifier: coverImageUrl)
self.coverImage = image
self.coverImageLoading = false
}
}
}
func loadBroadcasterImage() {
let broadcasterImage = self.video!.broadcaster.avatarImageUrl
guard broadcasterImage != "" else {
return
}
let urlRequest = URLRequest(url: URL(string: broadcasterImage)!)
let cachedImage = AppCache.image!.image(for: urlRequest, withIdentifier: broadcasterImage)
if (cachedImage != nil) {
self.broadcasterImage = cachedImage
return
}
let downloader = ImageDownloader(imageCache: AppCache.image!)
let filter = AspectScaledToFillSizeFilter(size: CGSize(width: 16, height: 16))
downloader.download(urlRequest, filter: filter) { response in
if case .success(var image) = response.result {
image = image.af.imageRoundedIntoCircle()
AppCache.image!.add(image, for: urlRequest, withIdentifier: broadcasterImage)
self.broadcasterImage = image
}
}
}
func setCategories() {
let categories = self.video!.broadcaster.categories
if (categories.count > 0) {
let categoryNames = categories.map { category in
return category.name == "" ? "(no name)" : category.name
}
self.categories = categoryNames.joined(separator: " • ");
}
}
}

How can i set profile pic against firebase user authentication with email?

i am developing an employee management app thats why i need to save profile picture against employee id or email .In firebase database what is the process and how can i design database through source code.In swift 3,xcode 8.3.2,ios 10.
You need to store the profile Image on the firebase storage,
after that imageURL will be retrieved. After the imageURL is retrieved you need to save it on the firebase database as a child of the userId.
Refer the code below
func handleRegister() {
guard let email = self.emailTextField.text, let password = self.passwordTextField.text, let name = self.nameTextField.text else {
print("Form is not valid")
return
}
Auth.auth().createUser(withEmail: email, password: password) { (user: User?, error) in
if error != nil {
print(error!)
return
}
guard let uid = user?.uid else {
return
}
//success
let imageName = NSUUID().uuidString
let storageRef = Storage.storage().reference().child("\(imageName).png")
if let uploadData = UIImageJPEGRepresentation(self.profileImageView.image!, 0.1) {
storageRef.putData(uploadData, metadata: nil, completion:
{ (metadata, error) in
if error != nil {
print(error!)
return
}
print(metadata!)
if let progileImageURL = metadata?.downloadURL()?.absoluteString {
let values = ["name": name, "email": email, "profileImageUrl": progileImageURL]
self.registerUserIntoDatabaseWithUID(uid: uid, values: values as [String : AnyObject])
}
})
}
}
}
func registerUserIntoDatabaseWithUID(uid: String, values: [String: AnyObject]) {
let ref = Database.database().reference()
let userRef = ref.child("users").child(uid)
userRef.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print(err!)
return
}
print("Saved user successfully into Firebase db")
self.messagesController1?.checkIfUseLoggedin()
self.dismiss(animated: true, completion: nil)
})
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
selectedImageFromPicker = editedImage
print(editedImage)
}
else if let orignalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImageFromPicker = orignalImage
print(orignalImage)
}
if let selectedImage = selectedImageFromPicker {
self.profileImageView.image = selectedImage
}
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
print("picker cancled")
self.dismiss(animated: true, completion: nil)
}

Deletebackward() Swift 3

DeleteBackward() deletes only one character, is there any way to keep on deleting backwards ?
I am using emojiKeyboard and I have a delete emoticon. I detect the emoji being the delete emoticon and I call
if emoticon.isDelete{
deleteBackward()
return
}
Update:
Steven's solution works on buttons but not on my UITextView. Will try and find out why. I have tried having the addGestureRecognizer in ViewWillAppear as well as ViewDidLoad.
This should get you started, didn't test but should do the trick.
fileprivate var timer = Timer()
fileprivate var textField = UITextField() //change to your field
override func viewDidLoad() {
super.viewDidLoad()
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
textField.addGestureRecognizer(longPress)
}
func longPress(_ guesture: UILongPressGestureRecognizer) {
if guesture.state == UIGestureRecognizerState.began {
longPressBegun(guesture)
} else if guesture.state == UIGestureRecognizerState.changed {
//longPressStateChanged(guesture)
} else if guesture.state == UIGestureRecognizerState.ended {
longPressEnded()
} else if guesture.state == UIGestureRecognizerState.cancelled {
longPressCancelled()
}
}
func longPressBegun(_ guesture: UILongPressGestureRecognizer) {
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(repeatAction), userInfo: nil, repeats: true)
}
func longPressEnded() {
timer.invalidate()
}
func longPressCancelled() {
timer.invalidate()
}
func repeatAction() {
deleteBackward()
}

Error in conversion from Swift 2 to Swift 3

This is the second and last section I am battling with converting Swift 2 to Swift 3
The old working code was
func calculateSegmentDirections(index: Int,
time: NSTimeInterval, routes: [MKRoute]) {
let request: MKDirectionsRequest = MKDirectionsRequest()
request.source = locationArray[index].mapItem
request.destination = locationArray[index+1].mapItem
request.requestsAlternateRoutes = true
request.transportType = .Automobile
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler ({
(response: MKDirectionsResponse?, error: NSError?) in
if let routeResponse = response?.routes {
let quickestRouteForSegment: MKRoute =
routeResponse.sort({$0.expectedTravelTime <
$1.expectedTravelTime})[0]
var timeVar = time
var routesVar = routes
routesVar.append(quickestRouteForSegment)
timeVar += quickestRouteForSegment.expectedTravelTime
if index+2 < self.locationArray.count {
self.calculateSegmentDirections(index+1, time: timeVar, routes: routesVar)
} else {
self.showRoute(routesVar, time: timeVar)
self.hideActivityIndicator()
}
} else if let _ = error {
let alert = UIAlertController(title: nil,
message: "Directions not available.", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "OK",
style: .Cancel) { (alert) -> Void in
self.navigationController?.popViewControllerAnimated(true)
}
alert.addAction(okButton)
self.presentViewController(alert, animated: true,
completion: nil)
}
})
}
The converted code is
func calculateSegmentDirections(index: Int,
time: NSTimeInterval, routes: [MKRoute]) {
let request: MKDirectionsRequest = MKDirectionsRequest()
request.source = locationArray[index].mapItem
request.destination = locationArray[index+1].mapItem
request.requestsAlternateRoutes = true
request.transportType = .Automobile
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler ({
(response: MKDirectionsResponse?, error: NSError?) in
if let routeResponse = response?.routes {
let quickestRouteForSegment: MKRoute =
routeResponse.sort({$0.expectedTravelTime <
$1.expectedTravelTime})[0]
var timeVar = time
var routesVar = routes
routesVar.append(quickestRouteForSegment)
timeVar += quickestRouteForSegment.expectedTravelTime
if index+2 < self.locationArray.count {
self.calculateSegmentDirections(index+1, time: timeVar, routes: routesVar)
} else {
self.showRoute(routesVar, time: timeVar)
self.hideActivityIndicator()
}
} else if let _ = error {
let alert = UIAlertController(title: nil,
message: "Directions not available.", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "OK",
style: .Cancel) { (alert) -> Void in
self.navigationController?.popViewControllerAnimated(true)
}
alert.addAction(okButton)
self.presentViewController(alert, animated: true,
completion: nil)
}
})
}
It throws an error on the line
directions.calculateDirectionsWithCompletionHandler ({
The error is
Cannot convert value of type '(MKDirectionsResponse?, NSError?) -> ()' to expected argument type 'MKDirectionsHandler' (aka '(Optional, Optional) -> ()')
If anyone can help me I would be very thankful!!
NSError was renamed to Error in Swift 3.0 This may fix your issue.
This code compiles for me:
func calculateSegmentDirections(index: Int,
time: TimeInterval, routes: [MKRoute]) {
let request: MKDirectionsRequest = MKDirectionsRequest()
request.source = locationArray[index].mapItem
request.destination = locationArray[index+1].mapItem
request.requestsAlternateRoutes = true
request.transportType = .automobile
let directions = MKDirections(request: request)
directions.calculate (completionHandler: {
(response: MKDirectionsResponse?, error: Error?) in
if let routeResponse = response?.routes {
let quickestRouteForSegment: MKRoute =
routeResponse.sorted(by: {$0.expectedTravelTime <
$1.expectedTravelTime})[0]
var timeVar = time
var routesVar = routes
routesVar.append(quickestRouteForSegment)
timeVar += quickestRouteForSegment.expectedTravelTime
if index+2 < self.locationArray.count {
self.calculateSegmentDirections(index+1, time: timeVar, routes: routesVar)
} else {
self.showRoute(routesVar, time: timeVar)
self.hideActivityIndicator()
}
} else
if let _ = error {
let alert = UIAlertController(title: nil,
message: "Directions not available.", preferredStyle: .alert)
let okButton = UIAlertAction(title: "OK",
style: .Cancel) { (alert) -> Void in
self.navigationController?.popViewControllerAnimated(true)
}
alert.addAction(okButton)
self.presentViewController(alert, animated: true,
completion: nil)
}
})
}

Swift3: Passing parameters into NSFetchRequest method

I use a general CoreData query method in my project.
func query(table: String, searchPredicate: NSPredicate) -> [AnyObject]
{
let context = app.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: table)
fetchRequest.predicate = searchPredicate
let results = try! context.fetch(fetchRequest)
return results
}
In Swift 3 this doesn't work. I found this on Apple's web site:
func findAnimals()
{
let request: NSFetchRequest<Animal> = Animal.fetchRequest
do
{
let searchResults = try context.fetch(request)
... use(searchResults) ...
}
catch
{
print("Error with request: \(error)")
}
}
Using the Apple example, how would I pass Animal in to the method as a parameter to make findAnimals more generic?
I haven't tried this but I think something like this would work...
func findCoreDataObjects<T: NSManagedObject>() -> [T] {
let request = T.fetchRequest
do
{
let searchResults = try context.fetch(request)
... use(searchResults) ...
}
catch
{
print("Error with request: \(error)")
}
}
You have to make the entire function generic and so you have to tell it what type T is when calling it.
someObject.findCoreDataObjects<Animal>()
I think that should do the job. Not entirely certain though as I'm new to generics myself :D
How about this.
func query<T: NSManagedObject>(table: String, searchPredicate: NSPredicate) -> [T] {
let context = app.managedObjectContext
let fetchRequest: NSFetchRequest<T> = NSFetchRequest(entityName: table)
fetchRequest.predicate = searchPredicate
let results = try! context.fetch(fetchRequest)
return results
}
Here is the final result that may help someone:
import Foundation
import Cocoa
func addRecord<T: NSManagedObject>(_ type : T.Type) -> T
{
let entityName = T.description()
let context = app.managedObjectContext
let entity = NSEntityDescription.entity(forEntityName: entityName, in: context)
let record = T(entity: entity!, insertInto: context)
return record
}
func recordsInTable<T: NSManagedObject>(_ type : T.Type) -> Int
{
let recs = allRecords(T.self)
return recs.count
}
func allRecords<T: NSManagedObject>(_ type : T.Type, sort: NSSortDescriptor? = nil) -> [T]
{
let context = app.managedObjectContext
let request = T.fetchRequest()
do
{
let results = try context.fetch(request)
return results as! [T]
}
catch
{
print("Error with request: \(error)")
return []
}
}
func query<T: NSManagedObject>(_ type : T.Type, search: NSPredicate?, sort: NSSortDescriptor? = nil, multiSort: [NSSortDescriptor]? = nil) -> [T]
{
let context = app.managedObjectContext
let request = T.fetchRequest()
if let predicate = search
{
request.predicate = predicate
}
if let sortDescriptors = multiSort
{
request.sortDescriptors = sortDescriptors
}
else if let sortDescriptor = sort
{
request.sortDescriptors = [sortDescriptor]
}
do
{
let results = try context.fetch(request)
return results as! [T]
}
catch
{
print("Error with request: \(error)")
return []
}
}
func deleteRecord(_ object: NSManagedObject)
{
let context = app.managedObjectContext
context.delete(object)
}
func deleteRecords<T: NSManagedObject>(_ type : T.Type, search: NSPredicate? = nil)
{
let context = app.managedObjectContext
let results = query(T.self, search: search)
for record in results
{
context.delete(record)
}
}
func saveDatabase()
{
let context = app.managedObjectContext
do
{
try context.save()
}
catch
{
print("Error saving database: \(error)")
}
}
Call it with:
let name = "John Appleseed"
let newContact = addRecord(Contact.self)
newContact.contactNo = 1
newContact.contactName = name
let contacts = query(Contact.self, search: NSPredicate(format: "contactName == %#", name))
for contact in contacts
{
print ("Contact name = \(contact.contactName), no = \(contact.contactNo)")
}
deleteRecords(Contact.self, search: NSPredicate(format: "contactName == %#", name))
recs = recordsInTable(Contact.self)
print ("Contacts table has \(recs) records")
saveDatabase()
I use that way in my projects:
static func retrieveRecords<T: NSManagedObject>(table: String, sortDescriptorKey: NSSortDescriptor? = nil) -> [T] {
do {
let fetchRequest: NSFetchRequest<T> = NSFetchRequest(entityName: table)
fetchRequest.sortDescriptors = [sortDescriptorKey!]
let results = try context.fetch(fetchRequest)
print("\(results)")
return results
} catch let error {
print("Could not fetch \(error.localizedDescription)")
return []
}
}
And to call it:
personen = retrieveRecords(table: "Person", sortDescriptorKey: NSSortDescriptor(key: #keyPath(Person.nachname), ascending: true, selector: #selector(NSString.localizedCompare)))