Executing Code after programmatically making a phone call - iphone

How can you execute code after the phone call is made? My app makes a phone call with the following code:
- (void)callTelURL:(NSURL *)url
{
UIWebView *webview = [[UIWebView alloc] init];
[self.view addSubview:webview];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
[webview release];
}
I immediately get applicationWillResignActive, applicationDidBecomeActive and applicationDidEnterBackground but I never get applicationWillEnterForeground or applicationDidBecomeActive once the phone call has completed and I am back in my application. Is there something I can do to be notified when I have returned from my call so I can execute some code?

You won't get a call to applicationWillEnterForeground if your app is in the foreground when a call comes in and then the call is dismissed.
http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
So execute the code in applicationDidBecomeActive but only when the preceeding call was applicationWillresignActive.
P.S.
Just because your app gets a applicationWillResignActive then applicationDidBecomeActive does not necessarily mean you got a phone call, other things could cause this too. So you cannot rely on this if you want to execute code after only a phone call has caused this behaviour and nothing else

Related

Validate if call was made after telprompt

I want to validate if user tapped on Call button or Cancel button after telprompt
Current code:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#", [personDetails valueForKey:#"phone"]]];
[[UIApplication sharedApplication] openURL:url];
How can I do so?
First telprompt: is not a documented URL schema and should not be used. Since Apple can change the way it used at any moment.
Second since data is passed back to your app, you will not be able to detect a if call was made. You might be able to detect if you use the CoreTelephony. But getting this to work require your app to run in the background and you might have to misuse some background mode for this which will make Apple reject your app.
Can you explain why you want to detect if there was a call made?
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(called:) name:#"UIApplicationSuspendedNotification" object:nil];
-(void)called:(NSNotification *) notification
{
NSLog(#"Tapped Call button");
}
if Call button was tapped then application will be terminated and go into background so just add an observer for the UIApplicationSuspendedNotification notification.
For my case, I used tel:// instead of using telprompt:// and make my own UIAlertView. This way you could detect if the call option is tapped from the UIAlertView's delegate.

iOS local notification: I would like to execute a function upon reentering the app via local notification

I'm working with XCode 4.6, and am trying to build a local notification feature on iOS that will execute a function upon reentering the app.Basically I would like to change the text in some of the labels and add some sound upon reentering the app. I thought I was on the right track, but only some parts of my code work when reentering the app via local notification.
First I added this function to my AppDelegate:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(#"test1"); //this traces successfully
myAppViewController * controller = [myAppViewController alloc];
[controller doSomething]; //calling a function in my myAppViewController.m
}
I thought I had figured it out, but now only the NSLog works in my function in myAppViewController.m:
-(void)doSomething{
NSLog(#"do something"); //traces successfully
self.notificationTime.text=#"something else"; //nothing happens here, it works in other functions but not here
[self doSomethingElse]; //calling another function from this function for further testing
}
The next function is called....
-(void)doSomethingElse{
NSLog(#"do something else"); //this works
//this whole thing doesn't work -- no sound --
NSURL* url = [[NSBundle mainBundle] URLForResource:#"cash" withExtension:#"mp3"];
NSAssert(url, #"URL is valid.");
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.player prepareToPlay];
[self.player play];
//this doesn't work again
self.notificationTime.text=#"something else";
}
I was hoping to get some general advice here and it would be much appreciated. If anyone knows a complete different way of solving the problem, that would be great as well!
The didReceiveLocalNotification method is only called when you application is running in the foreground. If you see a badge and click on the App to start it, then you need to process the local notification using application:willFinishLaunchingWithOptions: (or application:didFinishLaunchingWithOptions:) To get at your local notification in either of these two methods, use UIApplicationLaunchOptionsLocalNotificationKey as a key to the options dictionary.
Note, once you extract the local notification from the launch options, it is a viable approach to call your didReceiveLocalNotification method.
You shouldn't need to allocate a second instance of the app controller. You can just use self. If you do that, does the code work as expected?

Event/Method triggered when call triggered from WebView ends in IPhone?

I am triggering a phone call through a UIWebView, so that when the call ends, the user is returned directly to my app, via the following code:
UIWebView *callWebview = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:#"tel:number-to-call"];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
When it returns control back to my App I'd like to execute some code, but I am unable to determine what if any method the OS is calling when it returns to my App after the call ends? ViewWill, ViewDid etc never get called when control returns.
What if anything gets called when the call ends and control returns to the webview/app?
UIWebview Class has some delegate founctions.webview call these founctions when start loading and finish loading request and so on. I have poor English,I wish you can understand me.if I misread your question and my answer isn't what you want,please forgive me.

"[[UIApplication sharedApplication] openURL" after Link is tapped in UIWebView - Multitasking

I show an UIWebView inside an Application which sends it delegate methods calls, after it receives taps on links.
In the Delegate Method
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
I catch those calls, process the link-urls and run
[[UIApplication sharedApplication] openURL:processedURL];
This works as intended, I end up at the right place.
Problem Multitasking:
if I get back into my app, which is still running in the background after it closed, it will again call the "webView shouldStartLoadWithRequest" with the same link, so I end up sending the user to the page twice. Is there a preferred way to avoid this?
Solution:
You guys are totally right, I did a quite elongated if-else to analyze the given URL, but not in all branches the decision could end up in there existed a "return no"... duh, totally stupid error ;)
Clear the UIWebView so that when it reloads itself, it doesn't have a request pending. This is probably best done this way:
[webView loadRequest:nil];
But if that doesn't work, you can use:
[webView loadHTMLString:#"" baseURL:nil];
And of course you should be returning NO from this delegate method since you don't want the webview to try to load this content.
Not tested, but I'd suggest doing this:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
[[UIApplication sharedApplication] performSelector:#selector(openURL:)
withObject:processedURL
afterDelay:0];
return NO;
}
This way the callback can return as desired, and in the very next runloop iteration it will call openURL:.

Restarting iPhone application after [UIApplication sharedApplication] openURL

As title says I'd like to know how to restart my iPhone app after doing this:
[[UIApplication sharedApplication] openURL:[NSURL UrlWithString:#"tel://0123456789"]]
It seems pretty simple as I saw many topics also talking about restoring the very state of the application when openURL is called, but I can't find how to simply restart the app when the calling is finished.
Is it supposed to be the default behavior? As for me, the iPhone opens Favorites after call is finished, I don't know why.
You can't. Starting an app is solely user's responsibility - which I consider a good thing.
check the discussion here: https://devforums.apple.com/message/128046#128046
create a UIWebView to load the phone url like this:
UIWebView *webview = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
just use
[[UIApplication sharedApplication] openURL:[NSURL UrlWithString:#"telprompt://0123456789"]]
It will return to the app after call finished
You can't restart an app after a phone call, as your app has terminated and your code is no longer being run.
If you want to restart after the user visits a webpage, you can put a link with a custom scheme in that webpage, and register it with your app. The user can then tap the link to open your app again.