Post

Replies

Boosts

Views

Created

Contact access in list and detail contact view showing error: cannot find ContactDetailView(contact:) in scope.
Hello Everyone please let me know what is wrong in this code. // ContactViewModel.swift import Foundation import Contacts import UIKit final class ContactViewModel : ObservableObject { @Published var contacts : [Contact] = [] @Published var permissionsError : PermissionsError? = .none init() { permissions() } func openSetting() { permissionsError = .none guard let settingsURL = URL(string: UIApplication.openSettingsURLString) else { return } if UIApplication.shared.canOpenURL(settingsURL) { UIApplication.shared.open(settingsURL)} } private func getContacts() { Contact.fetchAll{ [weak self] result in guard let self = self else { return } switch result { case .success(let fetchedContacts): DispatchQueue.main.async { self.contacts = fetchedContacts.sorted(by: {$0.lastName < $1.lastName }) } case .failure(let error): self.permissionsError = .fetchError(error) } } } private func permissions() { switch CNContactStore.authorizationStatus(for: .contacts) { case .authorized: getContacts() case .notDetermined, .restricted, .denied: CNContactStore().requestAccess(for: .contacts) { [weak self] granted, error in guard let self = self else { return } switch granted { case true: self.getContacts() case false: DispatchQueue.main.async { self.permissionsError = .userError } } } default: fatalError("Unknown Error!") } } } // ContactModel.swift import Contacts import UIKit struct Contact:Identifiable { var id: String { contact.identifier} var firstName: String {contact.givenName} var lastName: String {contact.familyName} var phone: String? {contact.phoneNumbers.map(.value).first?.stringValue} var contactImage: UIImage? let contact: CNContact static func fetchAll(_ completion: @escaping(Result<[Contact], Error>) -> Void) { let containerID = CNContactStore().defaultContainerIdentifier() let predicate = CNContact.predicateForContactsInContainer(withIdentifier: containerID) let keysToFetch = [ CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactPostalAddressesKey, CNContactImageDataAvailableKey, CNContactThumbnailImageDataKey ] as [CNKeyDescriptor] let request = CNContactFetchRequest(keysToFetch: keysToFetch as [CNKeyDescriptor]) let descriptor = [ CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactPostalAddressesKey, CNContactImageDataAvailableKey, CNContactThumbnailImageDataKey ] as [CNKeyDescriptor] do{ let rawContacts = try CNContactStore().unifiedContacts(matching: predicate, keysToFetch: descriptor) completion(.success(rawContacts.map{.init(contact:$0)})) } catch { completion(.failure(error)) } } } enum PermissionsError: Identifiable { var id:String { UUID().uuidString } case userError case fetchError(_:Error) var description: String { switch self { case .userError: return "Please change permissions in settings." case .fetchError(let error): return error.localizedDescription } } } // ContactsView.swift import SwiftUI struct ContactsView: View { @StateObject var contactsVM = ContactViewModel() let alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"] var body: some View { NavigationStack { VStack { ScrollViewReader { scrollProxy in ZStack { contactsList(scrollProxy: scrollProxy) VStack { ForEach(alphabet, id: \.self) { letter in HStack { Spacer() Button(action: { print("letter = \(letter)") if contactsVM.contacts.first(where: { $0.firstName.prefix(1) == letter }) != nil { withAnimation { scrollProxy.scrollTo(letter) } } }, label: { Text(letter) }) } } } } } } .alert(item: $contactsVM.permissionsError) { _ in Alert( title: Text("Permission denied"), message: Text(contactsVM.permissionsError?.description ?? "unknown error"), dismissButton: .default(Text("OK"), action: {contactsVM.openSetting()})) } .navigationTitle("Contacts") } } @ViewBuilder private func contactsList(scrollProxy: ScrollViewProxy) -> some View { List { ForEach(contactsVM.contacts.sorted(by: { $0.firstName < $1.firstName })) { contact in contactSection(contact: contact) } } } // MARK: - Contact Section @ViewBuilder private func contactSection(contact: Contact) -> some View { Section(header: Text(String(contact.firstName.first ?? "?"))) { NavigationLink(destination: ContactDetailView(contact:contact)) { VStack(alignment: .leading){ HStack { Text(contact.firstName) } } } } } } struct ContactsView_Previews: PreviewProvider { static var previews: some View { ContactsView() } } Here is my ContactDetailView.swift import SwiftUI struct ContactDetailView: View { let contact : Contact var body: some View { Text(contact.firstName) } } struct ContactDetailView_Previews: PreviewProvider { static var previews: some View { ContactDetailView(contact:contact) } }
1
0
949
Apr ’23
How to apple for CarPlay entitlement certificate correctly ?
Hello Everyone, I am developing an iOS app with CarPlay, I have very simple app with Siri enabled voice based messaging feature. For enabling additional capabilities in Apple Developer Account under Certificate, Identifiers and Profile Tab, we need to take approval from Apple with registering for MFI program or write about our app feature by filling a form provided by Apple. Also i am following all the guidelines of Apple CarPlay. I have applied once fews weeks back still did not hear back from them. My question is how to write to apple in correct manner about my app feature so that i can get approval of CarPlay entitlement enabled in Apple Developer portal ? Is there any particular keyword do i need to write or anything else ? Any kind of help or guidance is valuable for me. Thank you for your time. I really appreciate it.
2
1
1.8k
Mar ’23
Can we run iOS app in CarPlay simulator without CarPlay entitlement certificate
Hello Everyone, As i am new to CarPlay. I want to run my simple app with Siri enabled messaging feature app runs on CarPlay simulator without adding CarPlay entitlement certificate to my Xcode project. I understand developing for CarPlay app we need CarPlay entitlement certificate but if i want just to see how SiriKit is working in app in CarPlay. Is it possible to test or its against apple CarPlay guidelines. Any kind of feedback or answer will be acceptable and appreciable.
0
0
845
Mar ’23
Cannot see Additional Capabilities in Certificate, Identifiers & Profiles
Hello Everyone, I want to update App ID of my car-play enabled app to include necessary CarPlay capabilities. But when i am trying to Select AppID , i can see only two tabs Capabilities and App Services. But as per the CarPlay documentation there is Additional Capabilities tab also available. Which i cannot see in my Apple Developer Program portal. Do you think its related to User and Roles ? (I have admin access) Or is there any other issue. Please address me if i am wrong . Thank you in advance.
2
0
2.5k
Mar ’23
How siri read messages or notification from our app in swift.
Hello Everyone, I am new to iOS development, and i need to integrate SiriKit in SwiftUI. My app feature is something like when we receive new messages on your app or as a notification message. how Siri will read for you particular message or last received notification message. I am trying to integrate SiriKit in SwiftUI but do not know exactly which function i have to write in Intent Handler or where exactly i have to mention. Please help me or if any tutorial link is available then post here. Any kind of help will be appreciable.
0
0
783
Mar ’23