I am using the following code within my application to create and populate a SMS for the user to send. This code is called by pressing a UIButton.
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"Hello from Mugunth";
controller.recipients = [NSArray arrayWithObjects:#"12345678", #"87654321", nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
On the initial loading of the view, I would like to test the device capabilities, and hide the button if necessary (for example on an iPod touch).
Does anyone have any code examples on how to do this? Please note, I am only targeting iOS 4.0 and greater, I am aware the above code will not work on devices using earlier versions of iOS.
Regards
[MFMessageComposeViewController canSendText] will determine whether the device you are on can send text messages. I've known it to react correctly on an iPhone with SIM, an iPod Touch and the iOS Simulator. I've not tested it on e.g. an iPhone without SIM or an iPad.
You can use the below code to detect whether the device can send SMS or not:
Preferred:
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"sms:"]]) {
}
or
[MFMessageComposeViewController canSendText]
Make sure you call [MFMessageComposeViewController canSendText] prior to allocating an instance of MFMessageComposeViewController. Newer versions of iOS will throw an alert informing the user that they can't send messages as soon as you instantiate MFMessageComposeViewController.
Related
Looks like iOS 5.1 has broken the standard URL encoding for navigating a user to a Preference.
For example:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"prefs:root=TWITTER"]];
Works in iOS 5.0 but not in iOS 5.1 (both device and simulator).
Has anyone found a way to replicate this functionality in iOS 5.1?
It is little tricky , i get by the removing the subviews in *TWTWeetComposeViewController*, so it shows only alert when user is not loged in and by the clicking on setting button , we can open Setting page in my app.
+ (void)setAlertForSettingPage :(id)delegate
{
// Set up the built-in twitter composition view controller.
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
// Create the completion handler block.
[tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) {
[delegate dismissModalViewControllerAnimated:YES];
}];
// Present the tweet composition view controller modally.
[delegate presentModalViewController:tweetViewController animated:YES];
//tweetViewController.view.hidden = YES;
for (UIView *view in tweetViewController.view.subviews){
[view removeFromSuperview];
}
}
here , delegate is your viewcontroller , if you are using this method inside your viewcontroller just use self instead of delegate.
EDIT: If you get any deprecated errors, use the following iOS6 compatible code instead:
- (void)setAlertForSettingPage
{
// Set up the built-in twitter composition view controller.
SLComposeViewController *tweetViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
// Present the tweet composition view controller modally.
[self presentViewController:tweetViewController animated:YES completion:nil];
for (UIView *view in tweetViewController.view.subviews){
[view removeFromSuperview];
}
}
No I don’t know a way to replicate this functionality.
But what you can do is file a Radar requesting the restoration. Here is a radar requesting that the schemes be documented in the first place.
David Barnard has confirmed that iOS 5.1 breaks the settings apps URL schemes.
Update: iOS 8 has similar functionality for opening your app’s settings. Thanks Apple, Mike and Soto_iGhost.
The constant UIApplicationOpenSettingsURLString (UIApplication Documentation) will open the settings for your app and not, say Twitter’s settings. Not exactly the same functionality but much cleaner than before and now officially recognized.
This should be extra useful now that each app has a place in Settings for using privacy, cellular data, background app refresh and notifications.
you can do this.
TWTweetComposeViewController *ctrl = [[TWTweetComposeViewController alloc] init];
if ([ctrl respondsToSelector:#selector(alertView:clickedButtonAtIndex:)]) {
// Manually invoke the alert view button handler
[(id <UIAlertViewDelegate>)ctrl alertView:nil
clickedButtonAtIndex:0];
}
If you look in Twitter's framework (that Twitter view controller), it has "prefs:root=TWITTER" inside, 5.1 also has this line. So probably Apple made something to disable it for other apps, like some special key in plist or method "openURL" somehow checks if it's not a system app.
Currently i am working in iPhone application, Using MFMessageComposeViewController to develop this application and its working fine.
But i want, message compose screen don't show on the screen, then the Message send programmatically ,
How to do this? it is possible? please help me
I tried this:
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"Hi";
controller.recipients = [NSArray arrayWithObjects:#"12345678", #"87654321", nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
I've tried to do this once but but it wont be accepted by app store. Apple wont let you send message/email without user knowledge about it.
You can use a sms:[target phone number] URL to open the SMS application, but there are no indications on how to prefill a SMS body with text (see this post on Apple Developer Forums).
You can't use the Message Framework to send a message without the user knowing about it. One common solution is to send the message details to a web service running on your server and send the message from the server. As long as you're not trying to be sneaky, I don't think there's anything wrong with doing that.
I am trying to send sms through device. But it crashes on the line
[self presentModalViewController:picker animated:YES];
CODE:
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
picker.recipients = rec; // your recipient number or self for testing
picker.body = billInfo;
[self presentModalViewController:picker animated:YES];
[picker release];
Error Log:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <billSummary: 0xfc7e780>.'
Have you checked +[MFMessageComposeViewController canSendText]? From the docs,
Before using this class, you must always check to see if the current device is configured to send SMS messages by calling the canSendText class method. If the user’s device is not set up for the delivery of SMS messages, you can notify the user or simply disable the SMS features in your application. You should not attempt to use this interface if the canSendText method returns NO.
Other reasons it might not work:
No SIM card?
Device isn't running iOS 4.
Device is an iPod Touch/iPad.
"Device" is actually the simulator.
[self presentModalViewController:picker.view animated:YES];
<MFMessageComposeViewControllerDelegate>
should be added in the .h of your class.
MFMessageComposeViewController is the subclass of UINavatigationController.
so apply pushViewController instead of presentmodalviewController
Is it possible to send sms using phonegap in iphone??If yes can anybody suggest me sample code or tutorials to do so???
Is it possible to find out user's phone number using phonegap in iphone??If yes can anybody suggest me sample code or tutorials to do so???
If you want to send sms via phone first you should create a plugin(see phonegap website to see how its done) then in plugin code(it should be native code do it like this :
You must add MessageUI.framework to your Xcode project and include a #import <MessageUI/MessageUI.h> in your header file.
Add these delegates to your header file MFMessageComposeViewControllerDelegate & UINavigationControllerDelegate.
In your IBAction method declare instance of MFMessageComposeViewController say messageInstance to check whether your device can send text use [MFMessageComposeViewController canSendText] in if condition, it'll return Yes/No in the if condition do these:
MFMessageComposeViewController *messageInstance = [[[MFMessageComposeViewController alloc] init] autorelease];
// set body for your messageInstance
messageInstance.body = #"Hello from Shah";
// then decide the recipients for the message as:
messageInstance.recipients = [NSArray arrayWithObjects:#"12345678", #"87654321", nil];
//Set a delegate to your messageInstance as:
messageInstance.messageComposeDelegate = self;
// Then present the messageViewController
[self messageInstance animated:YES];
In my iPhone app, I need to access the SMS application.
I need that when I click a button in my iphone app. It should open the SMS app of iphone and preload the message body with the text that I specify in my app.
How can we access the SMS API from iPhone app?
What should be done?
This is possible starting from iOS 4.0; use the MFMessageComposeViewController from the MessageUI Framework.
Details and example: http://developer.apple.com/library/ios/#samplecode/MessageComposer/Introduction/Intro.html
Try this :
MFMessageComposeViewController *pickerSMS = [[MFMessageComposeViewController alloc] init];
pickerSMS.messageComposeDelegate = self;
pickerSMS.body = #"hello!";
[self presentModalViewController:pickerSMS animated:YES];
[pickerSMS release];