I am using Storyboard-
I am pretty new to this-
I'm getting a EXC_BAD_ACCESS (code=2, address = 0x4) error when I click a button pointing to a MPMoviePlayerViewController.
To give you an idea of what I've got so far: I have several different ViewControllers all under the same ViewController.h/m class with navigation controlled by a NavigationController. I started with a NavController and single ViewController and have been linking new ViewControllers with the simple CTRL-Click button-to-new-ViewController. I added a new ViewController; changed it's class to MPMoviePlayerViewController in the Custom Class section of the Identity Inspector, but I keep getting this error when I click on the button to begin the transition.
Here's my ViewController.h code if it's relevant:
- (IBAction)postToTwitter:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#"Check this out!"];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
}
- (IBAction)postToFacebook:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:#"Posted from IOS App Test -- It works!"];
[self presentViewController:controller animated:YES completion:Nil];
}
}
- (IBAction)playVideo1:(id)sender {
NSURL * url = [[NSURL alloc]initWithString:#"link omitted"];
//create our moviePlayer
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
//Some additional customization
moviePlayer.fullscreen = YES;
moviePlayer.allowsAirPlay = YES;
moviePlayer.shouldAutoplay = NO;
moviePlayer.controlStyle = MPMovieControlStyleDefault;
}
Related
I am trying to show a Leader-board in my Cocos2d Game. The code below works correctly on iOS 5.0. However on 6.1 it crashes with the error message below. Thanks in advance for any suggestions.
* Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view controller at a time! View > is associated with . Clear this association before associating this view with .'
- (void)showLeaderboard:(id)sender
{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != NULL)
{
leaderboardViewController = [[UIViewController alloc] init];
leaderboardController.category = self.currentLeaderBoard;
leaderboardController.leaderboardDelegate = self;
[leaderboardViewController setView:[[CCDirector sharedDirector] openGLView]];
[leaderboardViewController presentViewController:leaderboardController animated:YES completion:nil];
}
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[leaderboardViewController dismissViewControllerAnimated:YES completion:nil];
[viewController release];
}
Try to stop openGL animation and resume back.
-(void)showLeaderboard
{
[[CCDirector sharedDirector] stopAnimation];
GKLeaderboardViewController *leaderboardViewController = [[[GKLeaderboardViewController alloc] init] autorelease];
leaderboardViewController.leaderboardDelegate = self;
AppController *app=(AppController*)[UIApplication sharedApplication].delegate;
[app.navController presentModalViewController:leaderboardViewController animated:YES];
}
-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
AppController *app=(AppController*)[UIApplication sharedApplication].delegate;
[app.navController dismissModalViewControllerAnimated:YES];
[[CCDirector sharedDirector] startAnimation];
}
I want to determine it a user is signed on to twitter, so I can prompt them to post a tweet but only if they are set up with ios and twitter. Also, is there a way to set up the twitter screen with a default tweet?
For iOS 5.x, you can check if the user is signed in to twitter using:
[TWTweetComposeViewController canSendTweet]
As for presenting a tweet screen with default message, you can do something like:
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:defaultMsg];
[self presentModalViewController:tweetSheet animated:YES];
You can refer this tutorial for more.
EDIT: For iOS 6.0 and above, use:
// requires "Social.framework"
[SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]
So, an example usage could be:
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler = myBlock;
[controller setInitialText:#"#myHashTag"];
[controller addImage:myImage];
[self presentViewController:controller animated:YES completion:Nil];
}
else
{ /* Show error alert, etc*/ }
Further reading
As I said in my comment if you are using IOS 5 or later versions just add Twitter.framework to your projects
#import <Twitter/Twitter.h>
//post tweets
- (IBAction)postTapped:(id)sender{
if ([TWTweetComposeViewController canSendTweet])
{
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:#" #hashtag"];
[self presentModalViewController:tweetSheet animated:YES];
}
else
{
TWTweetComposeViewController *viewController = [[TWTweetComposeViewController alloc] init];
//hide the tweet screen
viewController.view.hidden = YES;
//fire tweetComposeView to show "No Twitter Accounts" alert view on iOS5.1
viewController.completionHandler = ^(TWTweetComposeViewControllerResult result) {
if (result == TWTweetComposeViewControllerResultCancelled) {
[self dismissModalViewControllerAnimated:NO];
}
};
[self presentModalViewController:viewController animated:NO];
//hide the keyboard
[viewController.view endEditing:YES];
}
}
I have a problem when trying to display the GKMatchmakerViewController on my game view.
Normally I create a multiplayer match programmatically by auto-matching 2 opponents, and that works fine.
But when I try to display the standard Game Center Matchmaking view, it dismisses my game view and pushes me back to the menu.
Menu View -> Game View.
I think the problem might be that my menu view acts as my main view and all other views are removed when the Game Center view is displayed (since only one view controller can be shown at the time).
am I setting up my view hierarchy wrong? How should it be done so my Game View wont be dismissed when displaying the Game Center view?
EDIT - updated with code that calls the GKMatchMakerViewController
GameviewController with method that is called when I want to display the Game Center match making controller
- (void)presentCustomVSBattle {
ourRandom = arc4random();
[self setGameState:kGameStateWaitingForMatch];
AppDelegate * delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
[[GCHelper sharedInstance] findCustomMatchWithMinPlayers:2 maxPlayers:2 viewController:delegate.viewController delegate:self];
}
// This method is called in GCHelper.m
- (void)findCustomMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers viewController:(UIViewController *)viewController delegate:(id<GCHelperDelegate>)theDelegate {
if (!gameCenterAvailable) return;
matchStarted = NO;
self.match = nil;
self.presentingViewController = viewController;
delegate = theDelegate;
if (pendingInvite != nil) {
[presentingViewController dismissModalViewControllerAnimated:NO];
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:pendingInvite] autorelease];
mmvc.matchmakerDelegate = self;
[presentingViewController presentViewController:mmvc animated:YES completion:nil];
self.pendingInvite = nil;
self.pendingPlayersToInvite = nil;
}
else {
[presentingViewController dismissModalViewControllerAnimated:NO];
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = minPlayers;
request.maxPlayers = maxPlayers;
request.playersToInvite = pendingPlayersToInvite;
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[presentingViewController presentViewController:mmvc animated:YES completion:nil];
self.pendingInvite = nil;
self.pendingPlayersToInvite = nil;
}
}
In your case, the dismissView Controller calls look unnecessary, try getting rid of them :)
I am using the following code for UIModalTransitionStyle view in my application when i click the button
InfoViewController *infoViewController = [[InfoViewController alloc]initWithNibName:#"InfoViewController" bundle:nil];
infoViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.presentedViewController presentViewController:infoViewController animated:YES completion:NULL];
its worked perfecly in ios5 but i update the application ios6 it not worked. How can I handle this issue
This code works, tested in iOS 6.0 simulator.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:#"http://videoURL"]];
self.moviePlayerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:self.moviePlayerController animated:YES completion:nil];
}
Since the URL is invalid, the MPMoviePlayerViewController will just endlessly flip back and forth, but the point is that it works.
i want to select songs from ipod Library and play it using avplayer i want the music to continue playing even after the app goes to background i am new to iOS programming can anyone help me out ..
Thanks
To allow the user to pick a song (or songs) from their music library, use the MPMediaPickerController class.
-(void) pickSong {
// Create picker view
MPMediaPickerController* picker = [[MPMediaPickerController alloc] init];
picker.delegate = self;
// Check how to display
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
// Show in popover
[popover dismissPopoverAnimated:YES];
popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
// Present modally
[self presentViewController:picker animated:YES completion:nil];
}
}
Change self.navigationItem.rightBarButtonItem if you're not showing it from a button on the right side of title bar.
Then you need to listen for the result by implementing the delegate:
Called when the user cancelled the selection:
-(void) mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
// Dismiss selection view
[self dismissViewControllerAnimated:YES completion:nil];
[popover dismissPopoverAnimated:YES];
popover = nil;
}
Called when the user chose something:
-(void) mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
// Dismiss selection view
[self dismissViewControllerAnimated:YES completion:nil];
[popover dismissPopoverAnimated:YES];
popover = nil;
// Get AVAsset
NSURL* assetUrl = [mediaItemCollection.representativeItem valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:assetUrl options:nil];
// Create player item
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];
// Play it
AVPlayer* myPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[myPlayer play];
}
You'll need a UIPopoverController* popover; in your class .h file. Also you should retain myPlayer somewhere...
To allow music to continue in the background, add an audio string to the array in your Info.plist under the UIBackgroundModes key.