last day (15 days later) » 

9:36 PM
0
Q: My URL isn't running after I tap on my notification action. What do I do?

krishin my app I want a URL to run when my notification action is tapped on. The problem is that when I run the app it puts me into the foreground but doesn't run the URL (I added .foreground to the notification action). Here is my code: extension AppDelegate: UNUserNotificationCenterDelegate { func ...

 
Where does the variable number get set or accessed? I don't see any reference to it anywhere.
 
@creeperspeak the variable number is set right above the function, I just didn't want to share any phone numbers
 
OK, have you confirmed that the if let urled is passing? You can try calling canOpenUrl to see if the url is a valid format. Have you tried printing "tel://\(number)" to see if it looks right? If number is an optional you'll need to unwrap it first too. Could be a lot of things, but print statements and breakpoints could solve the problem for you.
 
@creeperspeak normally I don't have a variable called number there, because this app is just a testing app, normally I have a typical string right there. I've tested using this url many times and it works I just can't figure out how to get it to work when the notification action is tapped on.
 
Try canOpenUrl just to be sure, and put a breakpoint at if response... and just step through the code one line at a time to see what happens.
 
9:36 PM
@creeperspeak Where do I put canOpenUrl?
 
You can put canOpenUrl anywhere in there - it'll return true/false to let you know if you have an app installed that can open that.
For example, it'll fail on the simulator because you can't make phone calls on the simulator.
 
I know I am using my iPhone to run the app
 
Do you know for sure that your if statements are even passing? That's the very first thing to check. Maybe openUrl is never even getting called.
 
I'll check
 
Do you know how to put a breakpoint in and step one line at a time through the code?
 
9:40 PM
Yes
Is it okay if I just put: UIApplication.shared.open(URL(string: "tel://1234567891")!, options: [:])

Instead of: if let urled = URL(string: "tel://\(number)") {
UIApplication.shared.open(URL(string: "tel://1234567891")!, options: [:])


}
 
You can, but it will crash if your urlstring is not valid. It's fine for testing, but I wouldn't do it for production.
The way you had it was correct - it seems like it should be working, which is why I think one of your if statements isn't passing.
 
Both work as URLs I just can't get them to run when my notification action is tapped on
 
So are you even sure that code is being executed at all then?
Do something easy - put an alert popup in there or something, and see if it pops up when you tap the notification.
 
I tried to make a line print when the notification action is pressed and it works. But the URL doesn't run in this case
 
OK. What happened when you stepped through one line at a time with a breakpoint?
 
9:46 PM
This notification takes place when you aren't in the app
 
Sure, but you can still test by running the app from xcode on your phone, and after it's open you can put the app in teh background and test tapping a notification.
 
My point is that the URL doesn't enter the call when the notification action is tapped
 
I don't understand what you mean by that.
 
Basically when the URL runs it enters into a call. What I want to happen is, when you tap on the notification action it runs the URL therefore entering into the call
 
Yes, I get that part. But I still don't know if your if statements are passing or not. You need to know that so you know if the app is even TRYING to open that url. Step through with breakpoints or something.
That open(url function also has a completion handler available to tell you if it worked or not. It'll give you information that might answer your question. developer.apple.com/reference/uikit/uiapplication/…
 
9:57 PM
The if statements are trying to be run
I just used the breakpoints to tst it
*test
 
So openUrl executes?
 
Yes, but for some reason it isn't actually working
 
so if you try canOpenUrl does it return true?
 
This is what is says for the completion handler on the URL: 0x00000001000637bc NotificationsUI`partial apply forwarder for reabstraction thunk helper from @callee_unowned @convention(block) () -> () to @callee_owned () -> () at AppDelegate.swift
I'm confused on how to use canOpenURL
 
OK - instead of using openUrl try putting print(UIApplication.shared.canOpenUrl(URL(string: "tel://(number)")))
That'll print true/false.
If it prints false then it means either the link is no good, or your phone thinks it doesn't have an app installed that can open that link.
 
10:05 PM
I'm getting an error that says 'UIApplication' has no member 'canOpenURL'
Nevermind
 
Are you using Swift 3?
 
Yes. It printed true
 
Huh. I guess I don't have an answer then.
Sorry - I tried.
 
Is there anyway to detect when my app enters the foreground
?
 
Yes
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
 
10:10 PM
How
?
 
It's one of the built-in lifecycle methods in AppDelegate.
It's usually added by default when you create the project in XCode so it might already be in your AppDelegate.
If not you can add it.
It will get called right when your app is about to enter foreground
 
How about when my app enters the foreground
?
Is there a way to detect that?
 
The function I just pasted in tells you when the app enters foreground.
Well it tells you right before.
You can use didFinishLaunching if you need to see after the app is launched.
 
Do you know any ways that I could get this to work?
 
10:25 PM
No. The only other idea I have is that maybe Apple doesn't let you launch into a phone call automatically.
So you could try just showing the user an alert and waiting til they press a button to try to make the call.
Just an idea. I would need to play around with it myself to figure it out.
Sorry I can't help.
 

last day (15 days later) »