iphone: how to detect last caller number programmatically? - iphone

Is there any way to detect last caller number & call duration on iPhone, I am able to get all notification (Core Telephony) but don't know how to get caller number.

You can't, the API will not allow you to do this.
I think apple will never allow this due to privacy concerns.

According to api you cant do it... but here something which might help you ... though I haven't tried it myself...
http://iosstuff.wordpress.com/2011/08/19/accessing-iphone-call-history/

Apple officially don't allow.You cann't access the database of the other application then your one.

You can use this
if ([name isEqualToString:#"kCTCallIdentificationChangeNotification"])
{
// CTCallCenter *center = [[CTCallCenter alloc] init];
// center.callEventHandler = ^(CTCall *call) {
// NSLog(#”call:%#”, [call description]);
// };
//NSDictionary *info = (NSDictionary *)userInfo;
CTCall *call = (CTCall *)[info objectForKey:#"kCTCall"];
NSString *caller = CTCallCopyAddress(NULL, call);
NSLog(#"caller:%#",caller);
//CTCallDisconnect(call);
/* or one of the following functions: CTCallAnswer
CTCallAnswerEndingActive
CTCallAnswerEndingAllOthers
CTCallAnswerEndingHeld
*/
//if ([caller isEqualToString:#"+1555665753"])
if ([caller isEqualToString:#"+918740061911"])
{
NSLog(#"disconnecting call");
//disconnect this call
CTCallDisconnect(call);
}
}

Related

Sending SMS to dynamic Telephone number

I have an app which sends an sms and then calls a dynamic telephone number, here is the code:
#pragma mark - PictureListMainTableCellDelegate methods
-(void)pictureListMainTableCell:(PictureListMainTableCell *)cell wantsToCallNumber:(NSString *)number
{
MFMessageComposeViewController *messageComposer =
[[MFMessageComposeViewController alloc] init];
NSString *message = #"Your Message here";
[messageComposer setBody:message];
messageComposer.recipients = [NSArray arrayWithObjects:#"0003233", nil];
messageComposer.messageComposeDelegate = self;
[self presentViewController:messageComposer animated:YES completion:nil];
NSLog(#"Texting telephone number [%#]", messageComposer);
NSString *urlString = [NSString stringWithFormat:#"tel://%#", number];
NSLog(#"calling telephone number [%#]", number);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
NSLog(#"%#", [self deviceLocation]);
}
the telephone call works but not the sms, can anyone help?
First check if your device can send SMS messages by:
if ( [MFMessageComposeViewController canSendText] ) {
// build your text message like you posted
}
else {
// handle appropriately
// no point in trying to display the compose view controller
}
Whether this solves your specific problem, I am not sure, since I am just following Apple's docs for this.
just replace this line in your method and you'll be able to send dynamic messages
messageComposer.recipients = [NSArray arrayWithObjects:number,nil];
and you can send emails and messages automatically without pressing send button but in that case there is a chance of rejecting of your app from the apple app store as it is not under their legal process so they'll reject your application.
and you should check condition for
if([MFMessageComposeViewController canSendText])
otherwise in iPad and iPod your application might meet crash.

MPNowPlayingInfoCenter defaultCenter will not update or retrieve information

I'm working to update the MPNowPlayingInfoCenter and having a bit of trouble. I've tried quite a bit to the point where I'm at a loss. The following is my code:
self.audioPlayer.allowsAirPlay = NO;
Class playingInfoCenter = NSClassFromString(#"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:#"series_placeholder"]];
[songInfo setObject:thePodcast.title forKey:MPMediaItemPropertyTitle];
[songInfo setObject:thePodcast.author forKey:MPMediaItemPropertyArtist];
[songInfo setObject:#"NCC" forKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}
This isn't working, I've also tried:
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];
In an attempt to get it to remove the existing information from the iPod app (or whatever may have info there). In addition, just to see if I could find out the problem, I've tried retrieving the current information on app launch:
NSDictionary *info = [[MPNowPlayingInfoCenter defaultCenter] nowPlayingInfo];
NSString *title = [info valueForKey:MPMediaItemPropertyTitle];
NSString *author = [info valueForKey:MPMediaItemPropertyArtist];
NSLog(#"Currently playing: %# // %#", title, author);
and I get Currently playing: (null) // (null)
I've researched this quite a bit and the following articles explain it pretty thoroughly, however, I am still unable to get this working properly. Am I missing something? Would there be anything interfering with this? Is this a service something my app needs to register to access (didn't see this in any docs)?
Apple's Docs
Change lock screen background audio controls
Now playing info ignored
I finally figured out the problem, I was not prompting my app to receive remote control events, simply adding this line fixed the problem:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
I use the code below and it always works. I'm also using MPMoviePlayer like you. Have you checked whether NSClassFromString(#"MPNowPlayingInfoCenter") ever actually returns YES? Have you set you app play audio in background key in your plist?
- (void) loadMPInformation
{
NSDictionary *mpInfo;
if([savedTrack.belongingAlbum.hasAlbumArt boolValue] == NO){
mpInfo = [NSDictionary dictionaryWithObjectsAndKeys:savedTrack.belongingAlbum.album, MPMediaItemPropertyAlbumTitle,
savedTrack.belongingArtist.artist, MPMediaItemPropertyArtist, savedTrack.name, MPMediaItemPropertyTitle, nil];
} else {
UIImage *artImage = [UIImage imageWithData:savedTrack.belongingAlbum.art];
MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:artImage];
mpInfo = [NSDictionary dictionaryWithObjectsAndKeys:savedTrack.belongingAlbum.album, MPMediaItemPropertyAlbumTitle,
savedTrack.belongingArtist.artist, MPMediaItemPropertyArtist, savedTrack.name, MPMediaItemPropertyTitle, artwork, MPMediaItemPropertyArtwork, nil];
}
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = mpInfo;
}

Not release NSMutableString

I started developing a project in ios-5, I have created database in my application.
Here I create bdgDBArr in appDelegate that contains values like 93,55,68,95,45...
I want to create string like badge_id= #"93,55,68,95,45"
here appDelegate.sb is NSString type and sb1 is NSMutableString type
This is my code
NSMutableString *sb1 = [[NSMutableString alloc] init];
if (![appDelegate.bdgDBArr count]==0) {
for (int i=0; i < [appDelegate.bdgDBArr count]; i++) {
if (!i==0) {
[sb1 appendString:#","];
}
[sb1 appendString:[[appDelegate.bdgDBArr objectAtIndex:i] valueForKey:#"key1"]];
}
}
else {
[sb1 appendString:#""];
}
appDelegate.sb = sb1;
NSLog(#"appDelegate.sb showSB===%#",appDelegate.sb);
[sb1 release]; //error wait_fences: failed to receive reply: 10004003
sb1 = nil;
This code is working perfectly and get the output 93,55,68,45
but at the same time I got this error in NSLog
wait_fences: failed to receive reply: 10004003
Any ideas?
I can't help with your problem but you can — and should — reduce your code down to a one-liner: appDelegate.sb = [appDelegate.bdgDBArr componentsJoinedByString:#","]; which is much more expressive and does the right thing.
And while we're there:
Objective-C makes it rather easy to write code that can be read like prose. Don't break that by using member/variable names like sb or bdgDBArr.
Oh and there is an operator to test for inequality: != use that instead of negating the result of an equality test. Your future self and every other person looking at your code will be thankful.
just to try: use autorelease instead of release. does it change something?
is your property appDelegate.sb used in app delegate with a #syntetise setter/getter, or did you used your own code for the setter? in this case post your code, please.

facebook connect not working

This all is driving me nuts.
I just integrated facebook in my iphone app.
After typing in my username and password in the login dialog this method is called.
- (void)request:(FBRequest*)request didLoad:(id)result {
if ([request.method isEqualToString:#"facebook.fql.query"]) {
NSLog(#"result %#",result);
NSArray* users = result;
NSLog(#"users %#",users);
NSDictionary* user = [users objectAtIndex:0];
NSString* name = [user objectForKey:#"name"];
self.facebookName = name;
if (_posting) {
[self postToWall];
_posting = NO;
}
}
}
But after this the app crashes most of the times and when I tried to log the "result" array it appears to be empty. Why is it so?
What should I do? Please suggest.
use this code
NSArray *users=[[NSArray alloc]initWithObjects:result, nil];
NSLog(#"users %#",users);
NSDictionary* user = [[NSDictionary alloc]initWithObjectsAndKeys:users,#"users", nil];
NSLog(#"%#",user);
I got the reason for this.
This was due to the session getting lost frequently in between.
So I had to call [session resume] in the facebook login action event
and in this manner if at all the session is lost by any chance then the previous session is resumed and the "result" does not appears to be nil in (void)request:(FBRequest*)request didLoad:(id)result method.
Hope this helps out those who are stuck in the same issue.

Using tweet search with Twitter+OAuth on iPhone

Originally I needed the ability to use the search API with twitter. I did this using Matt Gemmell's great MGTwitterEngine. That code was very very simple and looked something like this:
- (void)viewDidLoad {
[super viewDidLoad];
tweetArrays = nil;
tweetNameArray = nil;
NSString *username = #"<username>";
NSString *password = #"<password>";
NSString *consumerKey = #"<consumerKey>";
NSString *consumerSecret = #"<consumerSecret>";
// Most API calls require a name and password to be set...
if (! username || ! password || !consumerKey || !consumerSecret) {
NSLog(#"You forgot to specify your username/password/key/secret in AppController.m, things might not work!");
NSLog(#"And if things are mysteriously working without the username/password, it's because NSURLConnection is using a session cookie from another connection.");
}
// Create a TwitterEngine and set our login details.
twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self];
[twitterEngine setUsesSecureConnection:NO];
[twitterEngine setConsumerKey:consumerKey secret:consumerSecret];
// This has been undepreciated for the purposes of dealing with Lists.
// At present the list API calls require you to specify a user that owns the list.
[twitterEngine setUsername:username];
[twitterEngine getSearchResultsForQuery:#"#HelloWorld" sinceID:0 startingAtPage:1 count:100];
}
This would end up calling the function:
- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier
And then I could do what I wanted with the searchResults. This required me to include the yajl library.
I then wanted to expand my code to allow users to tweet. I downloaded Ben Gottlieb's great code Twitter-OAuth-iPhone
So there's only one problem. The getSearchResultsForQuery returns a requestFailed with the following error:
Error Domain=HTTP Code=400 "The operation couldn’t be completed. (HTTP error 400.)"
To call this code I simply took the demo project in Twitter-OAuth-iPhone and added a call to getSearchResultsForQuery as seen here:
- (void) viewDidAppear: (BOOL)animated {
if (_engine) return;
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate: self];
_engine.consumerKey = kOAuthConsumerKey;
_engine.consumerSecret = kOAuthConsumerSecret;
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];
if (controller)
[self presentModalViewController: controller animated: YES];
else {
[_engine getSearchResultsForQuery:#"HelloWorld"];
// [_engine sendUpdate: [NSString stringWithFormat: #"Already Updated. %#", [NSDate date]]];
}
}
This as stated above returns a 400 error. Every other twitter API call I add here does work such as:
- (NSString *)getRepliesStartingAtPage:(int)pageNum;
Am I doing anything wrong? Or does getSearchResultsForQuery no longer work? The two code bases seem to use different versions of MGTwitterEngine, could that be causing the problem?
Thanks!
The problem is that you have to instantiate the twitter engine as an instance of SA_OAuthTwitterEngine, instead of MGTwitterEngine. When you call getSearchResultsForQuery, it uses a call to _sendRequestWithMethod to actually send the search request. SA_OAuthTwitterEngine overrides the MGTwitterEngine's implementation, but its version of that function doesn't work with getSearchResultsForQuery.
So, you need to go to getSearchResultsForQuery and make it use the MGTwitterEngine's version of the function.