Showing posts with label programatically. Show all posts
Showing posts with label programatically. Show all posts

Sunday, September 14, 2014

Grabbing call notifications in iOS7

Looks like there is not much in the way of documentation for how to do this so I am going to create one here.

Sometimes people get calls on their phone, I know this is crazy but it can happen. What you should do about it is up to you but how can you let your app know when this happens? Well I can help with that.

in your .h file, add the following.


#import <CoreTelephony/CTCallCenter.h>

@property (nonatomic, strong) CTCallCenter* callCenter;

you need to set the property as if you don't have anything holding on to the connection, it will disappear on your. Gotta love ARC!

in the .m file, here is your master method;

#pragma mark - phone call interrupts
-(void) setupPhoneNotification{
    float static playerVolume;
    self.callCenter = [[CTCallCenter alloc] init];
    self.callCenter.callEventHandler=^(CTCall* call)
    {
        
        if(call.callState == CTCallStateDialing)
        {
            //The call state, before connection is established, when the user initiates the call.
            NSLog(@"Call is dailing");
        }
        if(call.callState == CTCallStateIncoming)
        {
            //The call state, before connection is established, when a call is incoming but not yet answered by the user.
            NSLog(@"Call is Coming");
        }
        
        if(call.callState == CTCallStateConnected)
        {
            //The call state when the call is fully established for all parties involved.
            NSLog(@"Call Connected");
        }
        
        if(call.callState == CTCallStateDisconnected)
        {
            //The call state Ended.
            NSLog(@"Call Ended");
        } 
    };
}

And there you have it. Do whatever you need to do in the block, possibly send a trigger event or notifcation and then you are off to the races. Have fun!

Thursday, July 11, 2013

How to create a subview that will be a popup on large devices and full screen on small ones in iOS 6 with ARC

Universal apps are becoming more and more necessary today. They are becoming more complicated as well. At least they don't hundreds of different size screens to deal with like android.

First you should have a subview that is the size of a standard smaller screen (itouch, iphone). We will call this "SearchViewController".  You might want to use a delegate to interact with the views but we won't worry about that in this exercise.

in the SearchViewController.m;

-(void) closeView{
    //all keyboard items should have "resignFirstResponder" attached to it here just in case
    [self.view removeFromSuperview];
}

in the MainViewController.h;

#import "SearchViewController.h"

@interface MainViewController <UIPopoverControllerDelegate>{
    SearchViewController *searcher;
}

in the MainViewController.m;

-(void)OpenSearch{
    static bool isSearchOpen = FALSE;
    //open up the subview for the search function. Open up as a mini view for the ipad and a full window view for the iphone.
    if (isSearchOpen == FALSE){
        isSearchOpen = TRUE;
        if (searcher == nil){
            searcher = [[SearchViewController alloc]init];
            searcher.delegate = self;
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
                CGRect frame = searcher.view.frame;
                searcher.view.frame = CGRectMake(0, 0, frame.size.width, frame.size.height-88);
            }
        }
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
            //iphone
            [self addChildViewController:searcher];
            [self.view addSubview:searcher.view];
            [searcher didMoveToParentViewController:self];
        }else{
            //ipad
            popSearch = [[UIPopoverController alloc] initWithContentViewController:searcher];
            popSearch.popoverContentSize = searcher.view.frame.size;
            popSearch.delegate = self;
            [popSearch presentPopoverFromRect:CGRectMake(self.view.frame.size.width - 5,0, 10, 10) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        }
    }else{
        isSearchOpen = FALSE;
        [self closeSearch];
    }    

}

-(void)closeSearch{
    //cancels the search and closes the search view
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        [searcher.view removeFromSuperview];
    }else{
        [popSearch dismissPopoverAnimated:YES];
    }
    isSearchOpen = FALSE;
}

Done :)

Thursday, May 2, 2013

iOS Creating Custom Delegates in iOS6 with ARC

So you want two Objects to talk to each other more than a little bit? Here is what to do by example.  Lets say we have our MainViewController and a new SearchViewController that you want a delegate attached to it. This is all you need to do.

In SearchViewController.h
@protocol SearchViewDelegate <NSObject>
@optional
@required
-(void)didAskToSearch:(NSString*)string;
@end

@interface SearchViewController : UIViewController {
}
@property (nonatomic, assign) id <SearchViewDelegate> delegate;
@end

optional has non-required methods and required of course has required methods. 

In SearchViewController.m;
    [_delegate didAskToSearch: [self searchRequestDetails]];

this is put anywhere in the code when you want it to send the information back to the other object. 

In the MainViewController.h;
@interface MainViewController : UIViewController <SearchViewDelegate> {
}

In MainViewController.m;
SearchViewController *searcher = [[SearchViewController alloc]init];
searcher.delegate = self;

#pragma mark - SearchViewControllerDelegates
-(void)didAskToSearch:(NSString*)string{
     //do whatever you want with the string results
}

When creating the object, make sure the tell the delegate you have those methods. Then you just have to create the methods as per it is required. 

Helpful link
http://stackoverflow.com/questions/12020539/how-to-make-custom-delegate-in-ios-app