messaging API in IOS [duplicate] - iphone

This question already has answers here:
How to programmatically send SMS on the iPhone?
(18 answers)
Closed 9 years ago.
I am a IOS Developer seeking for the Messaging api in ios,I had gone through the internet and find that functionality in other possible domains as Java,php,android but,not in IOS Sector.so, can you tell me the solution for the SMS API to send message to other mobiles

Note that you can't automatically send SMS without user's knowledge. You can involve the user & then make him/her do the deed...
Sending SMS in iOS is pretty simple. You would require MFMessageComposeViewController for action. Below we have 2 delegate methods - one for composing your SMS & other when the control comes back to you after the user action.
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissModalViewControllerAnimated:YES];
if (result == MessageComposeResultCancelled)
NSLog(#"Message cancelled")
else if (result == MessageComposeResultSent)
NSLog(#"Message sent")
else
NSLog(#"Message failed")
}
There are plenty of online tutorials with full code to quick start you. Here's one. Hope this helps...

Related

sending SMS in iOS7 - problems after upgrade from iOS6

I am sending SMS programmatically like this for a long time now. It worked without any problems in iOS6.
But now after update to iOS7 some users have probelms with the app. They need to deinstall the app - reboot the iPhone - reinstall it and then it works.Just reinstalling it without rebooting the phone does not work either.
What could be the reason for this really annoying problem?
Furthermore there are a few cases where they can send several SMS after this procedure but then the iPhone SMS-Dialog appears very slowly and no SMS is being sent again, until they restart the iPhone. Just stopping and restarting the app does not help.
Here's the normal SMS code:
MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
[messageVC setMessageComposeDelegate:self];
if ([MFMessageComposeViewController canSendText]) {
NSString *smsString = [NSString stringWithFormat:#"bla bla bla"];
messageVC.body = smsString;
messageVC.recipients = #[userPhone];
messageVC.messageComposeDelegate = self;
[self presentViewController:messageVC animated:YES completion:nil];
}
I even released a new Version of the app with the newest Xcode 5.0 with Deployment Target 5.1, since I need to support iOS5.1 users still.
There isn't enough information to tell what's causing the problem. Btw, why are you setting messageComposeDelegate twice?
This is apple's most recent sample code that I've modified worked on my own devices running iOS 7 and iOS 8. Make sure to import MessageUI.framework.
/* -------------------------------------------------------------------------------
showSMSPicker:
IBAction for the Compose SMS button.
------------------------------------------------------------------------------- */
- (IBAction)showSMSPicker:(id)sender
{
/* Checks that the current device can send SMS messages. If no, [[MFMessageComposeViewController alloc] init] will return nil and the app will
crash when -presentViewController:animated:completion: is called with a nil view controller */
if ([MFMessageComposeViewController canSendText])
// The device can send email.
{
[self displaySMSComposerSheet];
}
else
// The device can not send email.
{
self.feedbackMsg.hidden = NO;
self.feedbackMsg.text = #"Device not configured to send SMS.";
}
}
/* -------------------------------------------------------------------------------
displayMailComposerSheet
Displays an SMS composition interface inside the application.
------------------------------------------------------------------------------- */
- (void)displaySMSComposerSheet
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
/* One or more preconfigured recipients can be specified. The user has the option to remove
or add recipients from the message composer view controller */
/* picker.recipients = #[#"Phone number here"]; */
// Message body
picker.body = #"This is a message about how great this app is. Please download it by clicking on the link below.";
[self presentViewController:picker animated:YES completion:nil];
}
/* -------------------------------------------------------------------------------
messageComposeViewController:didFinishWithResult:
Dismisses the message composition interface when users tap Cancel or Send.
Proceeds to update the feedback message field with the result of the
operation.
------------------------------------------------------------------------------- */
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result
{
self.feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MessageComposeResultCancelled:
self.feedbackMsg.text = #"Result: SMS sending canceled";
break;
case MessageComposeResultSent:
self.feedbackMsg.text = #"Result: SMS sent";
break;
case MessageComposeResultFailed:
self.feedbackMsg.text = #"Result: SMS sending failed";
break;
default:
self.feedbackMsg.text = #"Result: SMS not sent";
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}

sending sms programmatically in ios 7

Im trying to send an sms programmatically and in ios 6 its works perfect but in ios 7 its not working. Its open a white view with nothing inside and just stuck my app!
my code looks like this:
MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];
[messageVC setMessageComposeDelegate:self];
if ([MFMessageComposeViewController canSendText]) {
NSString *smsString = [NSString stringWithFormat:#"bla bla bla"];
messageVC.body = smsString;
messageVC.recipients = #[userPhone];
messageVC.messageComposeDelegate = self;
[self presentViewController:messageVC animated:YES completion:nil];
}
please help!! ios7 driving me crazy!
Your code works fine, but the message "Text messaging is not available" is given on devices that are not capable to send messages. I tested on an iPad Mini and it's working fine.

Is automated sharing possible for Twitter in the background on iOS?

I wanted to share a Twitter feed on the Twitter wall which contains an image and some text. I want support from iOS 4.3 to iOS 6.0.1. Is sharing possible in the background without a send/share button? How do I implement it?
The API call that you need to send is:
https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media
Before making that call, of course, you will need to authenticate with Twitter via xAuth/OAuth. Unless you get special permission from Twitter to do otherwise, it looks like you will need to use OAuth,
https://dev.twitter.com/docs/oauth/xauth
To background the request, it will likely make sense to use Grand Central Dispatch --that is unless you have a lot of different Twitter requests to send. In that case, I would instead opt for an NSOperationQueue where maxConcurrentOperationCount = 1. See the following:
http://www.fieryrobot.com/blog/2010/06/27/a-simple-job-queue-with-grand-central-dispatch/
http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues
Nevertheless, because OAuth is such a pain, it will likely make sense to use a third party library. I've never used it before, but here is an example using MGTwitterEngine:
Twitter's statuses/update_with_media on iOS returns 500 error
If you were able to limit use to iOS 5+, then I would highly recommend using the SLRequest object. The advantage of this approach is that you integrate with the iOS users account directly, so they don't have to authenticate through a UIWebView or something cheesy.
To do so, you would simply plug in the appropriate Twitter API url in the following function requestForServiceType:requestMethod:URL:parameters: and obtain your SLRequest object. Then assign the appropriate Twitter ACAccount obtained from the ACAccountStore using requestAccessToAccountsWithType:options:completion:. Finally make your call to performRequestWithHandler, which would then perform your request asynchronously.
The following code will not post in background but it can post across ios versions...
You can use condition for ios versions like below code.This is working code I have implemented and it is working on both ios 5 and 6. Please check in ios 4 to confirm.I think it should work.
#import "Twitter/Twitter.h"
#import "Social/Social.h"
-(IBAction)tweetPost:(id)sender
{
if ([self isSocialAvailable])
{
SLComposeViewController *tweetComposer=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewControllerCompletionHandler __block completionHandler=
^(SLComposeViewControllerResult result){
[tweetComposer dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
NSLog(#"Cancelled.....");
}
break;
case SLComposeViewControllerResultDone:
{
NSLog(#"Posted....");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Sent"
message:nil
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles: nil];
[alert show];
}
break;
}};
NSString*message = #"posting to twitter test ios 6";
[tweetComposer setInitialText:message];
[tweetComposer addImage:[UIImage imageNamed:#"2.jpg"]];
[tweetComposer addURL:[NSURL URLWithString:#"http://www.youtube.com/watch?v=GoZ2Be2zLq8"]];
[tweetComposer setCompletionHandler:completionHandler];
[self presentViewController:tweetComposer animated:YES completion:nil];
}
}
else
{
TWTweetComposeViewController *twitter= [[TWTweetComposeViewController alloc] init];
[twitter addImage:[UIImage imageNamed:#"2.jpg"]];
[twitter addURL:[NSURL URLWithString:#"http://www.youtube.com/watch?v=GoZ2Be2zLq8"]];
[twitter setInitialText:#"Tweet from iOS 5 app using the Twitter framework."];
[self presentModalViewController:twitter animated:YES];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult result)
{
NSString *title = #"Tweet Status";
NSString *msg;
if (result == TWTweetComposeViewControllerResultCancelled)
msg = #"Tweet compostion was canceled.";
else if (result == TWTweetComposeViewControllerResultDone)
msg = #"Tweet composition completed.";
// Show alert to see how things went...
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alertView show];
};
}
}
-(BOOL)isSocialAvailable {
return NSClassFromString(#"SLComposeViewController") != nil;
}
You need to include three frameworks named social,adSupport and Accounts.Check which one not needed with twitter feed post.
Hope ,this will help you.
Yes, but you'll need find some 1.1 API wrapper (thing which generates API requests, singns them etc) for you and authoriser (MGTWitter engine works fine). I have a working solution for sharing (text only) and getting user info for iOS 4+.
And about background part - that depends on how you implement that (i.e. notifications or continious background execution or gps callbacs etc...).

I want this button to show an error message if you are on iOS 5.1

I have a button that shares twitter message. The problem is social network does not work on iOS 5.1 so my question is how do I send an error message if the user is using iOS 5.1?
-(IBAction)Twitter:(id)sender{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Cancelled");
} else
{
NSLog(#"Done");
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
[controller setInitialText:#"#VOX"];
[controller addURL:[NSURL URLWithString:#""]];
[controller addImage:[UIImage imageNamed:#""]];
[self presentViewController:controller animated:YES completion:Nil];
}
else{
alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please check your Twitter settings." delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:nil ,nil];
[alert show];
}
}
This is my code.
If you are supporting iOS 5.1 as your deployment target, not allowing the user to post their tweet is a terrible user experience. Instead, your method should look something like this:
- (IBAction)sendTweetTapped:(id)sender {
if ([SLComposeViewController class]) {
// Execute your code as you have it
}
else {
// Use TWTweetComposeViewController and the Twitter framework
}
}
You'll need to weakly link the Social framework. In doing so, if the user's iOS version doesn't support the Social framework (i.e. is less than 6.0), you're basically just sending a message to nil, which is allowed. In such a case, you'd fall back to using the Twitter framework and everyone gets to happily tweet!
** NOTE: I changed the name of your method because it's terrible and doesn't describe what-so-ever what the method is supposed to do.
To solely get the system version, you can find a good answer already here: How can we programmatically detect which iOS version is device running on?
To sum it up, however, you can call:
[[[UIDevice currentDevice] systemVersion] floatValue];
Which returns the iOS version as a float value.
This, however, is a bad practice for what you need it for. It is better to check for a feature as well as checking for the current OS.To fully successfully integrate Twitter you should consider including built in Twitter functionality for iOS 5.0 as well (You will need to weakly include and #import both Twitter.framework and Social.framework):
float osv = [[[UIDevice currentDevice] systemVersion] floatValue];
if (osv >= 6.0 && [SLComposeViewController class]) { //Supports SLComposeViewController, this is preferable.
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
//Success, you can tweet! (using the class SLComposeViewController)
} else {
if ([TWTweetComposeViewController canSendTweet]) { //Perhaps redundant, but worth a try maybe?
//Success, you can tweet! (using the class TWTweetComposeViewController)
} else {
//Error Message
}
}
} else if (osv < 6.0 && osv >= 5.0 && [TWTweetComposeViewController class]) {
if ([TWTweetComposeViewController canSendTweet]) {
//Success, you can tweet! (using the class TWTweetComposeViewController)
} else {
//Error Message
}
} else {
//No internal solution exists. You will have to go with 3rd party or write your own.
}

What will happen to users running a lower version of IOS if new code is called?

I am fairly new to iOS Development and I've always wondered if a user running my application on iOS 4 were to try and run this code:
//POST TWEET//
- (void)showTweetSheet
{
TWTweetComposeViewController *tweetSheet =
[[TWTweetComposeViewController alloc] init];
tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result) {
switch(result) {
case TWTweetComposeViewControllerResultCancelled:
break;
case TWTweetComposeViewControllerResultDone:
break;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:^{
NSLog(#"Tweet Sheet has been dismissed.");
}];
});
};
[tweetSheet setInitialText:#"Check out this cool picture I found on #Pickr_"];
// Add an URL to the Tweet. You can add multiple URLs.
if (![tweetSheet addURL:[NSURL URLWithString:ImageHost]]){
NSLog(#"Unable to add the URL!");
}
[self presentViewController:tweetSheet animated:YES completion:^{
NSLog(#"Tweet sheet has been presented.");
}];
}
What would happen? Would the application just terminate with an error or will the code just not run? And how do I properly implement features that are OS specific? Would I just use something like this:
NSString *DeviceVersion = [[UIDevice currentDevice] systemVersion];
int DeviceVersionInt = [DeviceVersion intValue];
if (DeviceVersionInt > 5)
{
//do something.
}
else
{
//don't do a thing.
}
It will crash on iOS 4 if you write iOS5 features without checking if they are available or not. Try to implement Twitter like this
Class twClass = NSClassFromString(#"TWTweetComposeViewController");
if (!twClass) // Framework not available, older iOS
{
//use iOS4 SDK to implement Twitter framework
}
else {
//use Apple provided default Twitter framework
}
Make sure you have added Twitter Framework with weak link.
Id imagine that it would work the same as with any other api. If you link against a function which is not in a previous version, the program will crash on an attempt to call the function. Therefore, version switches are used, as you demonstrated, to avoid crashes.
The app would crash. If you want to implement features based on iOS, you can use a variety of methods. See this question.