This is some code from my sms plugin for phonegap. I'm trying to make callbacks work properly: https://github.com/aharris88/phonegap-sms-plugin/issues/11
Here's the code I'm working on. You can see that I get the callback function at the beginning of the send method like this:
NSString* callback = command.callbackId;
Then I present an MFMessageComposeViewController and I need to call that callback when it finishes. So I'm using the messageComposeViewController:didFinishWithResult:, but how can I access that callback function that I need to call?
#import "Sms.h"
#import <Cordova/NSArray+Comparisons.h>
#implementation Sms
- (CDVPlugin *)initWithWebView:(UIWebView *)theWebView {
self = (Sms *)[super initWithWebView:theWebView];
return self;
}
- (void)send:(CDVInvokedUrlCommand*)command {
NSString* callback = command.callbackId;
if(![MFMessageComposeViewController canSendText]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notice"
message:#"SMS Text not available."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
return;
}
MFMessageComposeViewController* composeViewController = [[MFMessageComposeViewController alloc] init];
composeViewController.messageComposeDelegate = self;
NSString* body = [command.arguments objectAtIndex:1];
if (body != nil) {
[composeViewController setBody:body];
}
NSArray* recipients = [command.arguments objectAtIndex:0];
if (recipients != nil) {
[composeViewController setRecipients:recipients];
}
[self.viewController presentViewController:composeViewController animated:YES completion:nil];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
#pragma mark - MFMessageComposeViewControllerDelegate Implementation
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
int webviewResult = 0;
CDVPluginResult* pluginResult = nil;
switch(result) {
case MessageComposeResultCancelled:
webviewResult = 0;
break;
case MessageComposeResultSent:
webviewResult = 1;
break;
case MessageComposeResultFailed:
webviewResult = 2;
break;
default:
webviewResult = 3;
break;
}
[self.viewController dismissViewControllerAnimated:YES completion:nil];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
if (webviewResult == 1) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:#"Arg was null"];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:callback];
}
#end
You'll want to store the callback ID as a property on the class.
#interface Sms
#property (nonatomic, strong) NSString *callbackId
#end
And then store it when you are in your send method.
- (void)send:(CDVInvokedUrlCommand*)command {
self.callbackId = command.callbackId;
You can then access it again from your delegate method:
NSString *callbackId = self.callbackId;
You should be good to go.
Related
I am facing one issue for send message to multiple contacts :
301 up contacts : Message app is not opening, If we click invite on multiple time then iMessage is opening but it will take more time (2 minute) to load message
351 up contacts : Message is open with black new message screen then come back to our application with out opening message screen.
Here is my code:
contacts is the array of phone number
NSMutableArray *contacts = [NSMutableArray array];
for (User *user in users) {
if (user.phone.length) {
NSString *strphonenumber = [NSString stringWithFormat:#"%#",user.phone];
[contacts addObject:strphonenumber];
}
}
MFMessageComposeViewController *messanger = [[MFMessageComposeViewController alloc]init];
messanger.messageComposeDelegate = self;
messanger.recipients = contacts;
messanger.body = [NSString stringWithFormat:#“body”;
[self presentViewController:messanger animated:YES completion:NULL];
I am getting this error:
<CKSMSComposeRemoteViewController: 0x12802f810> timed out waiting for fence barrier from com.apple.mobilesms.compose
Try this...
NSString *strContacts = [NSString stringWithFormat:#"%#",add multipal contacts];
MFMessageComposeViewController *message = [[MFMessageComposeViewController new];
message.recipients = #[strContacts];
I to had the same problem then figured
messanger.recipients = // should always be an array of strings.
Make sure the phone numbers you send to messanger.recipients are NSString.
this works for me:
set delegate to interface:
#interface ViewController <MFMessageComposeViewControllerDelegate>{}
check if device is able to send message
if([MFMessageComposeViewController canSendText] ){
//device is possible to send messages
}else{
//device can't send messages
}
prepare message:
MFMessageComposeViewController* comp = [[MFMessageComposeViewController alloc] init];
//set properties
comp.body = #"body";
comp.recipients = [NSArray arrayWithObjects:phone1, phone2, nil];
comp.messageComposeDelegate = self;
open dialog:
[self presentViewController:comp animated:YES completion:nil];
determine result
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
//test result
switch (result) {
case MessageComposeResultCancelled:
[self makeAlert:#"Result canceled"];
break;
//message was sent
case MessageComposeResultSent:
[self makeAlert:#"Result sent"];
break;
case MessageComposeResultFailed:
[self makeAlert:#"Result Failed"];
break;
default:
break;
}
//dismiss view
[self dismissViewControllerAnimated:YES completion:nil];
}
Just use this method:
- (IBAction)sendSMS:(id)sender {
MFMessageComposeViewController *controller =
[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText]){
controller.body = #"Hello";
controller.recipients = [NSArray arrayWithObjects:#"12345678",#"87654321", nil];
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
}
Callback method:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
switch (result) {
case MessageComposeResultCancelled:
NSLog(#"Cancelled");
break;
case MessageComposeResultFailed:
NSLog(#"Error occured");
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
I have a button (Buy) to open up a UIView (viewInAppPurchases) on top of another UIView (viewMenu). The Buy button is in the UIView viewMenu. I also have a button (Close) in the UIView viewInAppPurchases.
In the code when clicking the close button in the viewInAppPurchases, it shall close the view and go back to the viewMenu. But when clicking on the Buy button again, nothing happens. It should reopen the viewInAppPurchases UIView.
What else should I add and/or change to the code so when the Buy button is clicked again, it shall open up the viewInAppPurchases again?
In Header file:
#property (strong, nonatomic) IBOutlet UIView *viewInAppPurchases;
In implementation .m file:
- (IBAction)buttonClose: (UIButton*)sender
{
[_viewInAppPurchases removeFromSuperview];
}
Below is the IAP as GaryRiches request. I noticed that I didn't have a "close button" instruction in the IAP itself, but only through the MenuViewController.
How do I put the close button instructions in it?
BuyView.h
#import <UIKit/UIKit.h>
#import <Social/Social.h>
#import "MyIAPHelper.h"
#interface BuyView : UIView
{
SKProduct *product;
NSMutableArray *lstProducts;
int productIndex;
BOOL hasProducts;
}
#property (nonatomic, strong) UIViewController *parentViewController;
- (IBAction)buyCoin:(id)sender;
- (IBAction)shareTwitterGetCoin:(id)sender;
- (IBAction)shareFacebookGetCoin:(id)sender;
#end
BuyView.m
#import "BuyView.h"
#implementation BuyView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(productPurchased:)
name:IAPHelperProductPurchasedNotification object:nil];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
NSArray *nibs = [[NSBundle mainBundle]
loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
// NSArray *subviewArray = [[NSBundle mainBundle]
loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
UIView *mainView = [nibs objectAtIndex:0];
//Just in case the size is different (you may or may not want this)
mainView.frame = self.bounds;
[self addSubview:mainView];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(productPurchased:)
name:IAPHelperProductPurchasedNotification object:nil];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during
animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (IBAction)buyCoin:(id)sender {
BUTTON_SOUND
UIButton *btn = (UIButton *)sender;
NSString *productID = #"";
switch (btn.tag) {
case 0:
NSLog(#"buy 0.99");
productIndex = 0;
productID = FIRST_TIER;
break;
case 1:
NSLog(#"buy 1.99");
productIndex = 1;
productID = SECOND_TIER;
break;
case 2:
NSLog(#"buy 4.99");
productIndex = 2;
productID = THIRD_TIER;
break;
case 3:
NSLog(#"buy 9.99");
productIndex = 3;
productID = FOURTH_TIER;
break;
case 4:
NSLog(#"buy 19.99");
productIndex = 4;
productID = FIFTH_TIER;
break;
default:
break;
}
SKProduct *selectedProduct;
for (int i=0; i<lstProducts.count; i++) {
SKProduct *_product = [lstProducts objectAtIndex:i];
if ([_product.productIdentifier isEqualToString:productID]) {
selectedProduct = _product;
break;
}
}
[[MyIAPHelper shareInstance] buyProduct:selectedProduct];
}
- (void)productPurchased:(NSNotification *)notification {
NSLog(#"===========PurchaseViewController===========");
NSLog(#"purchased success");
//Add coin here IAPHelperProductPurchasedNotification
int increaCoin = 0;
switch (productIndex) {
case 0:
increaCoin = 100;
break;
case 1:
increaCoin = 250;
break;
case 2:
increaCoin = 750;
break;
case 3:
increaCoin = 2000;
break;
case 4:
increaCoin = 5000;
break;
default:
break;
}
NSString *strMsg = [NSString stringWithFormat:#"You have purchased
successfully and got %d coins",increaCoin];
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:#"Message" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
[self updateCoinWithIncreaseCoin:increaCoin];
}
- (void)updateCoinWithIncreaseCoin:(int)increaseCoin_{
int currentCoin = [Utils getCoin];
[Utils updateCoin:(currentCoin + increaseCoin_)];
}
- (IBAction)shareTwitterGetCoin:(id)sender {
BUTTON_SOUND
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
if (![userdefaults objectForKey:#"FIRST_SHARE_TWITTER"]) {
[userdefaults setObject:#"Abcd" forKey:#"FIRST_SHARE_TWITTER"];
[userdefaults synchronize];
SLComposeViewController *controller = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[controller setInitialText:#"Check out this new App!"];
[controller addURL:[NSURL URLWithString:APP_URL]];
[self.parentViewController presentViewController:controller
animated:YES completion:^{
//get coin here
NSLog(#"get coin share twitter");
int currentCoin = [Utils getCoin] + 30;
[Utils updateCoin:currentCoin];
}];
}
}
- (IBAction)shareFacebookGetCoin:(id)sender {
BUTTON_SOUND
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
if (![userdefaults objectForKey:#"FIRST_SHARE_FACEBOOK"]) {
[userdefaults setObject:#"Abc" forKey:#"FIRST_SHARE_FACEBOOK"];
[userdefaults synchronize];
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:#"Check out this new App!"];
[controller addURL:[NSURL URLWithString:APP_URL]];
[self.parentViewController presentViewController:controller animated:YES completion:^{
//add coin here
NSLog(#"get coin share facebook");
int currentCoin = [Utils getCoin] + 30;
[Utils updateCoin:currentCoin];
}];
}else{
}
}
#end
What else should I add and/or change to the code so when the Buy
button is clicked again, it shall open up the viewInAppPurchases
again?
You should add it back to the view from which you removed it.
You can do it on click event by:
[self.view addSubview: _viewInAppPurchases];
Also, have you consider hiding it instead of removing it completly and then adding it back?
To hide your view:
[_viewInAppPurchases setHidden:YES];
To show it again:
[_viewInAppPurchases setHidden:NO];
I changed the
removeFromSuperview
to
setHidden:Yes
and it did the trick.
I currently have a progress view which shows the current amount of how much of a file has been downloaded so far. When the user clicks the download button the progress succesfully updates and when they leave the view controller and return to it while it is still downloading the progress still updates successfuly. However my problem is when the download finishes and the user has left and returned to the view the progress view no longer responds to any updates. I have did some research into multi-threading and seen that many people suggest to do any UI updates in the main thread like so:
[self performSelectorOnMainThread:#selector(progressUpdate) withObject:nil waitUntilDone:NO];
So I tried it in my code and the still got the same results (once the user leaves and returns to the view controller after the download has finished the progress view does not respond to any updates). I then added a NSLog to see if the method was being called and the debugger outputted the NSLog. So what is going on? Here is the code for the view controller:
VideoTest.m
#import "VideoTest.h"
#import "AppDelegate.h"
#import "FileDownloadInfo.h"
#interface VideoTest ()
#property (nonatomic, strong) NSURLSession *session;
#property (nonatomic, strong) NSMutableArray *arrFileDownloadData;
#property (nonatomic, strong) NSURL *docDirectoryURL;
-(void)initializeFileDownloadDataArray;
-(int)getFileDownloadInfoIndexWithTaskIdentifier:(unsigned long)taskIdentifier;
#end
#implementation VideoTest{
NSString *url;
}
#synthesize moviePlayer;
#synthesize download;
#synthesize videoAlreadyPlaying, progressView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Setting video URl
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
url = #"http://therosary.info/AppVideos/TheChaplet/Information%20on%20Divine%20Mercy.mp4";
//selectors set
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(MPMoviePlayerPlaybackStateDidChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
//Play the movie
videoAlreadyPlaying=#"TRUE";
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:url]];
[self.view addSubview:moviePlayer.view];
self.moviePlayer.view.frame = CGRectMake(0,64,320,220);
[moviePlayer play];
[self initializeFileDownloadDataArray];
NSArray *URLs = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
self.docDirectoryURL = [URLs objectAtIndex:0];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:#"com.jjdoherty98.Marion_s_Net"];
sessionConfiguration.HTTPMaximumConnectionsPerHost = 5;
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:nil];
if([appDelegate.isAlreadyDownloading isEqual:#"FALSE"] || appDelegate.isAlreadyDownloading==nil){
self.progressView.hidden = YES;
progressView.progress=0;
}
if([appDelegate.isAlreadyDownloading isEqual:#"TRUE"]){
[self performSelectorOnMainThread:#selector(progressUpdate) withObject:nil waitUntilDone:NO];
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.isMovingFromParentViewController || self.isBeingDismissed) {
NSLog(#"Left View");
[moviePlayer stop];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown;
}
- (BOOL)shouldAutorotate{
AppDelegate *mainDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
mainDelegate.fullScreenVideoIsPlaying = #"TRUE";
return YES;
}
- (void)MPMoviePlayerPlaybackStateDidChange:(NSNotification *)notification
{
if (moviePlayer.playbackState == MPMoviePlaybackStatePlaying)
{ //playing
videoAlreadyPlaying = #"TRUE";
}
if (moviePlayer.playbackState == MPMoviePlaybackStateStopped)
{ //stopped
}if (moviePlayer.playbackState == MPMoviePlaybackStatePaused)
{ //paused
}if (moviePlayer.playbackState == MPMoviePlaybackStateInterrupted)
{ //interrupted
}if (moviePlayer.playbackState == MPMoviePlaybackStateSeekingForward)
{ //seeking forward
}if (moviePlayer.playbackState == MPMoviePlaybackStateSeekingBackward)
{ //seeking backward
}
}
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
appDelegate.fullScreenVideoIsPlaying = #"TRUE";
}
- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(#"enteredFullscreen");
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(#"willExitFullscreen");
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
appDelegate.fullScreenVideoIsPlaying = #"FALSE";
}
- (void)exitedFullscreen:(NSNotification*)notification {
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSLog(#"exitedFullscreen");
if(appDelegate.CurrentProgress==0){
progressView.hidden=TRUE;
}else{
[self performSelectorOnMainThread:#selector(progressUpdate) withObject:nil waitUntilDone:NO];
}
}
- (void)playbackFinished:(NSNotification*)notification {
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
case MPMovieFinishReasonPlaybackEnded:
NSLog(#"playbackFinished. Reason: Playback Ended");
videoAlreadyPlaying=#"FALSE";
[moviePlayer stop];
[moviePlayer play];
break;
case MPMovieFinishReasonPlaybackError:
NSLog(#"playbackFinished. Reason: Playback Error");
videoAlreadyPlaying=#"FALSE";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Video Failed To Load!"
message:#"Unable to connect to server, please try again later!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
break;
case MPMovieFinishReasonUserExited:
NSLog(#"playbackFinished. Reason: User Exited");
videoAlreadyPlaying=#"FALSE";
break;
default:
break;
}
[self.moviePlayer setFullscreen:NO animated:YES];
}
-(IBAction)buttonPressed:(id)sender{
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
if([appDelegate.isThereInternet isEqual:#"TRUE"]){
if([appDelegate.isAlreadyDownloading isEqual: #"FALSE"]){
//If there is internet and not already downloading
progressView.hidden=FALSE;
progressView.progress=0;
FileDownloadInfo *fdi = [self.arrFileDownloadData objectAtIndex:0];
fdi.downloadTask = [self.session downloadTaskWithURL:[NSURL URLWithString:fdi.downloadSource]];
fdi.taskIdentifier = fdi.downloadTask.taskIdentifier;
// Start the task.
[fdi.downloadTask resume];
}}
if ([appDelegate.isThereInternet isEqual:#"FALSE"]) {
//No internet available
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unable to connect to server"
message:#"Internet connection appears to be offline, please try again later!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
if([appDelegate.isAlreadyDownloading isEqual:#"TRUE"]){
//Is already downloaing
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"File Already Downloading"
message:#"Multiple files can not be downloaded at the same time!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
-(void)initializeFileDownloadDataArray{
self.arrFileDownloadData = [[NSMutableArray alloc] init];
[self.arrFileDownloadData addObject:[[FileDownloadInfo alloc] initWithFileTitle:nil andDownloadSource:url]];
}
-(int)getFileDownloadInfoIndexWithTaskIdentifier:(unsigned long)taskIdentifier{
int index = 0;
for (int i=0; i<[self.arrFileDownloadData count]; i++) {
FileDownloadInfo *fdi = [self.arrFileDownloadData objectAtIndex:i];
if (fdi.taskIdentifier == taskIdentifier) {
index = i;
break;
}
}
return index;
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *destinationFilename = downloadTask.originalRequest.URL.lastPathComponent;
NSURL *destinationURL = [self.docDirectoryURL URLByAppendingPathComponent:destinationFilename];
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
if ([fileManager fileExistsAtPath:[destinationURL path]]) {
[fileManager removeItemAtURL:destinationURL error:nil];
}
BOOL success = [fileManager copyItemAtURL:location
toURL:destinationURL
error:&error];
if (success) {
int index = [self getFileDownloadInfoIndexWithTaskIdentifier:downloadTask.taskIdentifier];
FileDownloadInfo *fdi = [self.arrFileDownloadData objectAtIndex:index];
fdi.isDownloading = NO;
fdi.downloadComplete = YES;
// Set the initial value to the taskIdentifier property of the fdi object,
// so when the start button gets tapped again to start over the file download.
fdi.taskIdentifier = -1;
// In case there is any resume data stored in the fdi object, just make it nil.
fdi.taskResumeData = nil;
}
else{
NSLog(#"Unable to copy temp file. Error: %#", [error localizedDescription]);
}
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
if (error != nil) {
NSLog(#"Download completed with error: %#", [error localizedDescription]);
appDelegate.isAlreadyDownloading=#"FALSE";
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failed to download!"
message:#"Unable to connect to the server!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
});
}
else{
appDelegate.isAlreadyDownloading=#"FALSE";
NSLog(#"Download finished successfully.");
dispatch_async(dispatch_get_main_queue(), ^{
FileDownloadInfo *fdi = [self.arrFileDownloadData objectAtIndex:0];
fdi.isDownloading = NO;
fdi.downloadComplete = YES;
fdi.taskIdentifier = -1;
fdi.taskResumeData = nil;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Download Complete!"
message:#"Go to downloads section to view the file now!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
});
}
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
if (totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown) {
NSLog(#"Unknown transfer size");
}
else{
int index = [self getFileDownloadInfoIndexWithTaskIdentifier:downloadTask.taskIdentifier];
FileDownloadInfo *fdi = [self.arrFileDownloadData objectAtIndex:index];
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
appDelegate.isAlreadyDownloading=#"TRUE";
fdi.downloadProgress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
appDelegate.CurrentProgress = fdi.downloadProgress;
[self performSelectorOnMainThread:#selector(progressUpdate) withObject:nil waitUntilDone:NO];
}
}
-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
if ([downloadTasks count] == 0) {
if (appDelegate.backgroundTransferCompletionHandler != nil) {
// Make nil the backgroundTransferCompletionHandler.
appDelegate.backgroundTransferCompletionHandler = nil;
progressView.hidden=TRUE;
// Show a local notification when all downloads are over.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"All files have been downloaded!";
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
}
}];
}
-(void)progressUpdate{
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
if ([appDelegate.isAlreadyDownloading isEqual:#"TRUE"]) {
progressView.progress = appDelegate.CurrentProgress;
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:#selector(progressUpdate) userInfo:nil repeats:NO];
NSLog(#"Is this method being called");
if (appDelegate.CurrentProgress==1) {
progressView.hidden=TRUE;
}
}
}
#end
Any help would be appreciated!
I have project very simply as Unity projects, and I want add a new function - sending for email application screenshot,
I try many ways to do that, but I am neebie in IOS and need your help :(
This version is working without errors, but after click button I didnt see email form
code is very short and simple - I hope someone has help me :(((
SampleViewsAppDelegate.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <MapKit/MKMapView.h>
#import <MessageUI/MessageUI.h>
#import "tiDFusionMobile.h"
#interface SampleViewsAppDelegate : NSObject <UIApplicationDelegate,MFMailComposeViewControllerDelegate> {
///Application Window
UIWindow *mWindow;
UIViewController *rootViewController;
///Application views
UIView *mRender;
tiComponent* mPlayer;
}
///IBOutlet properties
#property (nonatomic, retain) IBOutlet UIWindow *mWindow;
#property (nonatomic, retain) IBOutlet UIViewController *rootViewController;
#property (nonatomic, retain) IBOutlet UIView *mRender;
- (IBAction)openMailBtn:(id)sender;
- (void)start;
- (void)stop;
#end
file mm
#import "SampleViewsAppDelegate.h"
#implementation SampleViewsAppDelegate
#synthesize mWindow;
#synthesize mRender;
#synthesize rootViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// allocate the Component
mPlayer = [tiComponent alloc];
// set correct renderer
[mPlayer setRendererType:[tiComponent TI_RENDERER_GLES2]];
// initialze
[mPlayer initialize:mRender];
// start scenario
[self start];
return YES;
}
- (void)dealloc
{
[self stop];
//If the player is still instanciated, it is terminated and released
if (mPlayer)
{
[mPlayer terminate];
[mPlayer release];
mPlayer = nil;
}
[mRender release];
[mWindow release];
[super dealloc];
}
- (IBAction)openMailBtn:(id)sender {
rootViewController = (UIViewController*)
[(SampleViewsAppDelegate*)[[UIApplication sharedApplication] delegate] rootViewController];
if ([MFMailComposeViewController canSendMail]) {
// compose
MFMailComposeViewController* mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
//format message
NSArray *recipientsArray = [[NSArray alloc] initWithObjects:#"test#aaaa.com", nil];
[mail setToRecipients:recipientsArray];
NSString *emailBody = #"DSDSDSDS";
[mail setSubject:[NSString stringWithFormat:#"AAAAAA"]];
//UIImage *myImage = [UIImage imageNamed:#"mobiletuts-logo.png"];
//NSData *imageData = UIImagePNGRepresentation(myImage);
//[mail addAttachmentData:imageData mimeType:#"image/png" fileName:#"mobiletutsImage"];
[mail setMessageBody:emailBody isHTML:YES];
//send
//if (controller)
[rootViewController presentModalViewController:mail animated:YES];
[mail release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
#pragma mark - MFMailComposeController delegate
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled: you cancelled the operation and no email message was queued");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved: you saved the email message in the Drafts folder");
break;
case MFMailComposeResultSent:
NSLog(#"Mail send: the email message is queued in the outbox. It is ready to send the next time the user connects to email");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed: the email message was nog saved or queued, possibly due to an error");
break;
default:
NSLog(#"Mail not sent");
break;
}
[self dismissViewControllerAnimated:YES complete:nil];
}
- (void)start
{
if (mPlayer != nil) {
BOOL isLoaded = [mPlayer loadScenario:#"Scenario/SampleViews_GLES1/project.dpd"];
if (isLoaded) {
[mPlayer playScenario];
}
}
}
- (void)stop
{
if (mPlayer && ![mPlayer isScenarioPaused]) {
[mPlayer pauseScenario];
}
}
#end
I GUESS YOU ARE NOT IMPORTING FRAMEWORK
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface MailClassViewController : UIViewController<MFMailComposeViewControllerDelegate>
And then the code to present the email screen:
- (IBAction)openMailBtn:(id)sender {
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
mailCont.mailComposeDelegate = self;
[mailCont setSubject:#"Your email"];
[mailCont setMessageBody:[#"Your body for this message is " stringByAppendingString:#" this is awesome"] isHTML:NO];
NSString *file = [documentsDirectory stringByAppendingPathComponent:#"MaintenanceRequest.pdf"];
//IF YOU DONT WANT TO ATTACH FILE THEN COMMENT IT
NSData *data=[NSData dataWithContentsOfFile:file];
[mailViewController addAttachmentData:data mimeType:#"application/pdf" fileName:#"MaintenanceRequest.pdf"];
[self presentViewController:mailCont animated:YES completion:nil];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
//handle any error
[controller dismissViewControllerAnimated:YES completion:nil];
}
I created absolutely simple multiplayer app which just finds the match (with only 1 opponent) and terminates it in 5 seconds:
-(void)matchmakerViewController:(GKMatchmakerViewController *)viewController
didFindMatch:(GKMatch *)match {
[self.presentingViewController dismissModalViewControllerAnimated:YES];
self.match = match;
self.match.delegate = self;
if (!self.matchStarted && self.match.expectedPlayerCount == 0)
{
NSLog(#"Ready to start match!");
//[self lookupPlayers];
// My Own Test Code Begin
if (!self.delegate) NSLog(#"No delegate on match invite.");
// Notify delegate match can begin
self.matchStarted = YES;
[self.delegate matchStarted];
// My Own Test Code End
}
}
And some methods in AppDelegate:
-(void)matchStarted
{
CCLOG(#"Match started. Delay 5 seconds...");
[self performSelector:#selector(matchEnded) withObject:nil afterDelay:5];
}
-(void)matchEnded
{
CCLOG(#"Match ended");
[[GameCenterMatchHelper sharedInstance].match disconnect];
[GameCenterMatchHelper sharedInstance].match = nil;
}
All works fine for the first match. But when the match is finished there are 3 additional threads (I can see them pausing execution):
Thread com.apple.gamekitservices.gcksession.recvproc
Thread com.apple.gamekitservices.gcksession.sendproc
Thread com.apple.gamekitservices.eventcallback.eventcbproc
And if I start two matches - there are already 6 (3 and 3) threads after match finish.
And the main reason why it is so bad - app crashes and it is like all players are disconnected.
I use iPod touch 4g and iPad 2 for tests with the newest iOS 6. And these threads are created at both devices.
I thought it is because I use a singleton-class for GCHelper and all delegates but I tryed to extract delegates to other one-time-using classes - each time the additional threads appear.
Maybe smb know how can I fix it?
I think this is helpful for you.
#import <Foundation/Foundation.h>
#import <GameKit/Gamekit.h>
#interface GCHelper : NSObject<GKMatchmakerViewControllerDelegate, GKMatchDelegate>
{
BOOL isUserAuthenticated;
UIViewController *presentingViewController;
GKMatch *match;
BOOL matchStarted;
GKInvite *pendingInvite;
NSArray *pendingPlayersToInvite;
NSMutableDictionary *playersDict;
NSString *MultiplayerID;
NSData *MultiData;
NSString *otherPlayerID;
char AlertMessageBoxNo;
BOOL isDataRecieved;
}
//variables
#property (assign, readonly) BOOL gameCenterAvailable;
#property (retain) UIViewController *presentingViewController;
#property (retain) GKMatch *match;
#property (retain) GKInvite *pendingInvite;
#property (retain) NSArray *pendingPlayersToInvite;
#property (retain) NSMutableDictionary *playersDict;
#property (retain) NSString *MultiplayerID;
#property (retain) NSData *MultiData;
-(NSString*)getOtherPlayerId;
-(void)setOtherPlayerId;
//Functions
+ (GCHelper *)sharedInstance;
-(BOOL)isGameCenterAvailable;
-(void)authenticationChanged;
-(void)authenticateLocalUser;
-(void)gameOver:(NSString*)message;
-(void)setDataRecieved:(BOOL)d;
-(BOOL)getDataRecieved;
- (void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers viewController:(UIViewController *)viewController;
#end
/////////
#import "GCHelper.h"
#import "IPadSharebleClass.h"
#implementation GCHelper
#synthesize gameCenterAvailable;
#synthesize presentingViewController;
#synthesize match;
#synthesize pendingInvite;
#synthesize pendingPlayersToInvite;
#synthesize playersDict;
#synthesize MultiData;
#synthesize MultiplayerID;
static GCHelper *sharedHelper = nil;
+(GCHelper *) sharedInstance
{
if (!sharedHelper)
{
sharedHelper = [[GCHelper alloc] init];
}
return sharedHelper;
}
- (BOOL)isGameCenterAvailable
{
Class gcClass = (NSClassFromString(#"GKLocalPlayer"));
NSString *reqSysVer = #"4.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
return (gcClass && osVersionSupported);
}
- (id)init
{
if ((self = [super init]))
{
gameCenterAvailable = [self isGameCenterAvailable];
if (gameCenterAvailable)
{
NSNotificationCenter *nc =
[NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:#selector(authenticationChanged)
name:GKPlayerAuthenticationDidChangeNotificationName
object:nil];
}
else
{
UIAlertView* alert=[[UIAlertView alloc]initWithTitle:#"Game Center Alert" message:#"Game Center Not Available" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
return self;
}
-(void)authenticationChanged
{
if ([GKLocalPlayer localPlayer].isAuthenticated && !isUserAuthenticated)
{
NSLog(#"Authentication changed: player authenticated.");
isUserAuthenticated = TRUE;
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite)
{
NSLog(#"Received invite");
self.pendingInvite = acceptedInvite;
self.pendingPlayersToInvite = playersToInvite;
IPadCallAnyWhereF.inviteReceived();
};
}
else if (![GKLocalPlayer localPlayer].isAuthenticated && isUserAuthenticated)
{
NSLog(#"Authentication changed: player not authenticated");
isUserAuthenticated = FALSE;
}
}
- (void)authenticateLocalUser
{
if (!gameCenterAvailable) return;
NSLog(#"Authenticating local user...");
if ([GKLocalPlayer localPlayer].authenticated == NO)
{
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:nil];
}
else
{
NSLog(#"Already authenticated!");
}
}
-(void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers viewController:(UIViewController *)viewController
{
if (!gameCenterAvailable) return;
matchStarted = NO;
self.match = nil;
self.presentingViewController = viewController;
if (pendingInvite != nil)
{
[presentingViewController dismissModalViewControllerAnimated:NO];
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:pendingInvite] autorelease];
mmvc.matchmakerDelegate = self;
[presentingViewController presentModalViewController:mmvc animated:YES];
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 presentModalViewController:mmvc animated:YES];
self.pendingInvite = nil;
self.pendingPlayersToInvite = nil;
}
}
#pragma mark GKMatchmakerViewControllerDelegate
- (void)matchmakerViewControllerWasCancelled:(GKMatchmakerViewController *)viewController
{
[presentingViewController dismissModalViewControllerAnimated:YES];
UIAlertView* alert=[[UIAlertView alloc]initWithTitle:#"Game Center Alert" message:#"Game Cancel By you" delegate:self cancelButtonTitle:#"Try Again" otherButtonTitles:#"Main Menu", nil];
[alert show];
[alert release];
AlertMessageBoxNo='E';
}
- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFailWithError:(NSError *)error
{
[presentingViewController dismissModalViewControllerAnimated:YES];
NSLog(#"Error finding match: %#", error.localizedDescription);
UIAlertView* alert=[[UIAlertView alloc]initWithTitle:#"Game Center Alert" message:#"Connection Time out" delegate:self cancelButtonTitle:#"Try Again" otherButtonTitles:#"Main Menu", nil];
[alert show];
[alert release];
AlertMessageBoxNo='A';
}
- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)theMatch
{
[presentingViewController dismissModalViewControllerAnimated:YES];
self.match = theMatch;
match.delegate = self;
if (!matchStarted && match.expectedPlayerCount == 0)
{
NSLog(#"***************Ready to start match!**************");
[self lookupPlayers];
}
}
- (void)lookupPlayers
{
NSLog(#"Looking up %d players...", match.playerIDs.count);
[GKPlayer loadPlayersForIdentifiers:match.playerIDs withCompletionHandler:^(NSArray *players, NSError *error)
{
if (error != nil)
{
NSLog(#"Error retrieving player info: %#", error.localizedDescription);
matchStarted = NO;
//IPadCallAnyWhereF.matchEnded();
UIAlertView* alert=[[UIAlertView alloc]initWithTitle:#"Game Center Alert" message:#"Error retrieving player info" delegate:self cancelButtonTitle:#"Try Again" otherButtonTitles:#"Main Menu", nil];
[alert show];
[alert release];
AlertMessageBoxNo='F';
}
else
{
self.playersDict = [NSMutableDictionary dictionaryWithCapacity:players.count];
for (GKPlayer *player in players)
{
NSLog(#"Found player: %#", player.alias);
[playersDict setObject:player forKey:player.playerID];
}
NSLog(#"Total Number of Players : %d",players.count);
matchStarted = YES;
IPadCallAnyWhereF.matchStarted();
}
}];
}
#pragma mark GKMatchDelegate
- (void)match:(GKMatch *)theMatch didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID
{
if (match != theMatch) return;
MultiData=data;
MultiplayerID=playerID;
if(otherPlayerID==nil)
{
otherPlayerID=[playerID retain];
}
IPadCallAnyWhereF.match();
}
-(void)setDataRecieved:(BOOL)d
{
isDataRecieved=d;
}
-(BOOL)getDataRecieved
{
return isDataRecieved;
}
-(NSString*)getOtherPlayerId
{
return otherPlayerID;
}
-(void)setOtherPlayerId
{
otherPlayerID=nil;
}
- (void)match:(GKMatch *)theMatch player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state
{
if (match != theMatch) return;
switch (state)
{
case GKPlayerStateConnected:
NSLog(#"New Player connected!");
if (!matchStarted && theMatch.expectedPlayerCount == 0)
{
NSLog(#"&&&&&&&&&& Ready to start match in the match!");
[self lookupPlayers];
}
break;
case GKPlayerStateDisconnected:
NSLog(#"--------Player disconnected!--------");
matchStarted = NO;
UIAlertView* alert=[[UIAlertView alloc]initWithTitle:#"Game Center Alert" message:#"Player Disconnected" delegate:self cancelButtonTitle:#"Try Again" otherButtonTitles:#"Main Menu", nil];
[alert show];
[alert release];
AlertMessageBoxNo='B';
//IPadCallAnyWhereF.matchDisconnect();
break;
}
}
- (void)match:(GKMatch *)theMatch connectionWithPlayerFailed:(NSString *)playerID withError:(NSError *)error
{
if (match != theMatch) return;
NSLog(#"Failed to connect to player with error: %#", error.localizedDescription);
matchStarted = NO;
//IPadCallAnyWhereF.matchEnded();
UIAlertView* alert=[[UIAlertView alloc]initWithTitle:#"Game Center Alert" message:#"Failed to connect to player" delegate:self cancelButtonTitle:#"Try Again" otherButtonTitles:#"Main Menu", nil];
[alert show];
[alert release];
AlertMessageBoxNo='C';
}
- (void)match:(GKMatch *)theMatch didFailWithError:(NSError *)error
{
if (match != theMatch) return;
NSLog(#"Match failed with error: %#", error.localizedDescription);
matchStarted = NO;
//IPadCallAnyWhereF.matchEnded();
UIAlertView* alert=[[UIAlertView alloc]initWithTitle:#"Game Center Alert" message:#"Match failed" delegate:self cancelButtonTitle:#"Try Again" otherButtonTitles:#"Main Menu", nil];
[alert show];
[alert release];
AlertMessageBoxNo='D';
}
-(void)gameOver:(NSString*)message
{
UIAlertView* alert=[[UIAlertView alloc]initWithTitle:#"Game Center Alert" message:message delegate:self cancelButtonTitle:#"Try Again" otherButtonTitles:#"Main Menu", nil];
[alert show];
[alert release];
AlertMessageBoxNo='G';
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Try Again"])
{
IPadCallAnyWhereF.matchDisconnect();
}
else if([title isEqualToString:#"Main Menu"])
{
IPadCallAnyWhereF.gotoMainMenu();
}
}
#end
the right answer is that XCode doesn't show a real thread picture. It shows a lot of different threads but if I try to see them in profiler then I understand there are no additional threads. So I should suggest, no problem with threads but there is a problem with debug process. As a topic starter I think it is closed.
Thank you all.