Implemented iOS AppIntents don't show in Shortcuts - ios

I'm trying to test out the new AppIntents API that is currently on iOS16 Beta. Looking at the documentation, the implementation seems pretty straight forward, but after implementing it, I'm not seeing my AppIntent in the Shortcuts app. The device is running iOS 16.0.
This is how I implemented the AppIntent:
import AppIntents
struct DoSomethingIntent: AppIntent {
static var title: LocalizedStringResource = "This will do something"
static var description = IntentDescription("Does something")
func perform() async throws -> some PerformResult {
return .finished(value: "Done")
}
}
According to the documentation the Shortcuts app should be able to find my AppIntent after my app gets installed, but I see that's not the case. Does anybody know what my implementation is missing?

AppIntents APIs are a little bit new and have strange behaviours and the documentation is very poor.
In my case, I was able to make them work by adding the \(.applicationName) parameter to the phrase. Try this:
struct LibraryAppShorcuts: AppShortcutsProvider {
#AppShortcutsBuilder static var appShortcuts: [AppShortcut] {
AppShortcut(intent: DoSomethingIntent(), phrases: ["Do something with \(.applicationName)"])
}
}

First you need to select your Xcode-beta.app via xcode-select
Clean derived data
Kill your app & shortcut app
Add a shortcut library in your code
struct LibraryAppShorcuts: AppShortcutsProvider {
#AppShortcutsBuilder static var appShortcuts: [AppShortcut] {
AppShortcut(intent: DoSomethingIntent(), phrases: ["My something phrase"])
}
}
Build

Related

How to add String Extension on Apple DocC

I am checking new Apple documentation on Xcode 13 beta. I am able to build documentation with DocC.
But I am not able to see any documentation created for extensions like String extension, Date extension.
As per example for below case it will not add any documentation.
extension String {
/// Description for DocC
func myMethod() {
}
}
Consider, If I have created an extension for any struct or class, then it is adding documentation like below code
public struct MyStruct {
}
extension MyStruct {
/// Description
func myMethod() {
}
}
Is this default behaviour by apple or I am missing anything?
This is not currently supported. DocC cannot generate documentation for anything but a single module's types. Extensions to types outside your module aren't handled. (It also won't allow you to link to types outside your module using ``...`` references.)

Mapbox Navigation, Rerouting Issue in iOS SDK

Navigation keeps on constantly rerouting after every route progress even though the user device location is on the same route. I thought Mapbox automatically handles rerouting only when the user leaves the current route. After I read this part of the documentation, I thought I needed to handle it manually.
Based on documentation, I used below code to handle rerouting manually but the code is deprecated.
func navigationViewController(_ navigationViewController: NavigationViewController, shouldRerouteFrom location: CLLocation) -> Bool {
return navigationViewController.routeController.userIsOnRoute(location)
}
So crash redirects me to this part of Mapbox SDK
#available(*, deprecated, renamed: "navigationService", message: "NavigationViewController no longer directly manages a RouteController. See MapboxNavigationService, which contains a protocol-bound reference to the RouteController, for more information.")
/// :nodoc: obsoleted
#objc public final var routeController: RouteController! {
get {
fatalError()
}
set {
fatalError()
}
}
but navigationService not containing userIsOnRoute function. Is there an alternative function I could use, or is this a bug?
This issue appears to be a bug. Base on #luugiathuy report if you just change SDK to below code if will fix everything, voice and rerouting problem
index 47d8132..bc44ed7 100644
--- a/ios/Pods/MapboxCoreNavigation/MapboxCoreNavigation/NavigationService.swift
+++b/ios/Pods/MapboxCoreNavigation/MapboxCoreNavigation/NavigationService.swift
## -127,7 +127,7 ## public protocol NavigationService: CLLocationManagerDelegate, RouterDataSo #objc(MBNavigationService)
public class MapboxNavigationService: NSObject, NavigationService, DefaultInterfaceFlag {
- typealias DefaultRouter = RouteController
+ typealias DefaultRouter = LegacyRouteController
/**
The default time interval before beginning simulation when the `.onPoorGPS` simulation
In MapboxNavigationService change RouteController to LegacyRouteController and everything will work as expected.
The issue is on for almost 4 months, modifying SDK is not good idea but this is and alternative solution until Mapbox fix this issue.

Accidentally overriding getter with swift extension - Only during runtime when app enters foreground again

The last 2 days i brooded over a strange behavior i found in my iOS app. It was one of these moments when you start to have doubts about everything. I found the problem and fixed it. But i want to write this here to search for answers and gain knowledge about the system behind.
I am the developer of "Controller for HomeKit", an app to control HomeKit devices. In HomeKit you have HMActionSets to execute many changes at once. E.g. Turn on all lights.
Executing HMActionSet is a basic functionality and worked always. Here with a swipe of UITableViewCell.
let performAction = UIContextualAction(style: .normal, title: AppStrings.Actions.perform, handler: { (ac: UIContextualAction, view: UIView, success: #escaping (Bool) -> Void) in
self.home?.executeActionSet(selectedActionSet, completionHandler: { (error: Error?) in
if let err = error {
UIApplication.printToConsole(text: err)
success(false)
return
}
success(true)
})
})
BUT suddenly it stopped working flawlessly with a new development version. It took me a while to reproduce the situation, when it wasn't executing the action set.
Every time i start the app from scratch, everything works.
If i execute the HMActionSet right after navigating from the previous UIViewController, everything works. (Most common use case)
It stops working when being in the view, press the home button, reentering the app. After that all executions won't work, until going one view backwards and forward again.
Console logs this error:
Error Domain=HMErrorDomain Code=2 "Object not found." UserInfo={NSLocalizedDescription=Object not found.}
I walked backwards every commit until the problem was solved. After digging in the dark in a too big commit, i found the root cause.
But why am i not getting compile errors? Why does going to the home screen breaks it?
For a new feature i needed a new way to identify a HomeKit object on multiple devices, because the .uniqueIdentifier parameter is salted on every device.
import Foundation
import HomeKit
protocol Identifyable {
func getUniqueIdentifier() -> String?
}
extension HMActionSet: Identifyable {
func getUniqueIdentifier() -> String? {
return self.name.toBase64()
}
}
I created a swift protocol and made an extension to HMActionSet. Of course now that i found the error it looks stupid, to name the method like a getter. But this should not be the discussion now. It seems like this methods overrides the attribute of HMActionSet. When executing the actionSet internal functions accidentally use the wrong uniqueIdentifier, therefore the error is correct. (No object found).
/*!
* #brief A unique identifier for the action set.
*/
#available(iOS 9.0, *)
open var uniqueIdentifier: UUID { get }
If i name the method func uniqueIdentifier() -> String? Xcode immediately shows me a warning.
Method 'uniqueIdentifier()' with Objective-C selector 'uniqueIdentifier' conflicts with getter for 'uniqueIdentifier' with the same Objective-C selector
As i know HomeKit is written in Objective-C. I am using swift 4.
My questions to you:
Why are there no compiler errors (getUniqueIdentifier())?
If its a runtime problem mixing a swift extension into a objective-c framework, why does it only occur when reentering the app and not at the first start or later?
Looking forward to read your explanations about the problem that stole my sleep. :)

how can i test to see if only one of the methods works in xcode with swift programming?

I do not want to run the whole project as i have other classes and also the main storyboard runs which i dont want, i just want to run the below code, is this possible?
import Foundation
var eventCapacity : Int?;
var eventAttendees : String?;
var attendees = 0;
func addAttendee (user_id:User){
if eventCapacity < attendees {
attendees++;
print("Going to event")
}
}
func removeAttendee(user_id:User){
attendees--;
print("Not going")
}
func getAttendees(){
print(attendees)
}
You can use Playground to achieve that. Open Xcode and then go to File -> New -> Playground and follow the steps. Copy your code there along with the implementation of User class. And you should be able to call your functions and test them.
As Vishnu gondlekar says: Use playgrounds!
But one small side note:
For testing small pieces of code we usually write Unit Tests ;-)
And as default every new Xcode projects comes with a test setup ... so
go ahead and write tests! At the same time you will improve the quality of your code ;-)

Create iOS framework using swift

I am trying to create an iOS framework using swift. I follow this blog and also few others but the output is not the way I want.
Below is my original source file
public class TestClass: NSObject {
// publicVar description
public var publicVar: Int = 0
// doSomething description
public func doSomething() {
print("doSomething method")
}
}
After adding the framework into my project it's create an TestFramwork-Swift.h
You can see it's not contain my description. I want the framework
header files like Apple.
Can anyone help me to figure out this. I am not able to understand where I am doing wrong. I am also not able to add more swift files into my framework.
Just use triple slash instead of double slash for comments you want to show in your framework headers.

Resources