Category Archives: iOS

MailCore CTCoreAttachment example

To add attachments in MailCore add this ONLY after setBody/setHTMLBody:

// Set Attachments
NSString *filePrefix = [[NSBundle mainBundle] bundlePath];
NSString *path = [NSString stringWithFormat:@"%@/%@",filePrefix,@"TestData/DSC_6201.jpg"];
NSLog(@"path:%@", path);
CTCoreAttachment *attach = [[CTCoreAttachment alloc] initWithContentsOfFile:path];
if ([attach data]==nil) {
    NSLog(@"Error: attachment data is nil");
}
[myMessage addAttachment:attach];

Here, you should add TestData folder with image to your project as a folder reference.

TWTweetComposeViewController doesn’t display initial tweet

TWTweetComposeViewController doesn’t display tweets, that are too long. You can take a substring of a tweet by cutting down to 140 symbols:

Apple’s Twitter framework example

This is an example, how to integrate Twitter framework of Apple to your app.

Firstly, add Twitter (Twitter.framework) and MobileCoreServices frameworks to your app.

Then add header files to your source file:

[crayon-66452e648fc1c100758617/]

Then add this method or similar:

- (void)sendToTwitter {
useDelegate
if ([appDelegate iOSVersion5]) {
[self showErrorAlert:@"Отправка в Twitter доступна только в iOS 5 и выше."];
return;
}
tweetView = [[TWTweetComposeViewController alloc] init];
TWTweetComposeViewControllerCompletionHandler
completionHandler =
^(TWTweetComposeViewControllerResult result) {
switch (result)
{
case TWTweetComposeViewControllerResultCancelled: {
NSLog(@"Twitter Result: canceled");
[self showErrorAlert:@"Отправка в Twitter отменена."];
break;
}
case TWTweetComposeViewControllerResultDone: {
NSLog(@"Twitter Result: sent");
[self showErrorAlert:@"Отправка в Twitter выполнена."];
break;
}
default: {
NSLog(@"Twitter Result: default");
[self showErrorAlert:@"Отправка в Twitter не выполнена."];
break;
}
}
[self dismissModalViewControllerAnimated:YES];
};
[tweetView setCompletionHandler:completionHandler];
[tweetView setInitialText:self.textToExport];
[self presentModalViewController:tweetView animated:YES];
}

Here, firstly iOS version is checked, because it will work only in iOS 5 or later. Then, I create TWTweetComposeViewController instance, it’s block completion handler, set text to export to Twitter and then present controller as a modal controller.

How to check iOS version programmatically

To check the iOS version you can use the following method:

Deleting from Core Data

Deleting objects from Core Data should not be recognized as deleting rows from SQLite table. It is like deleting objects from Object Graph. This is an example of deleting all rows for the current file number. I retrieve all NSManagedObject objects and delete them in cycle.

+ (BOOL)deleteScrollPositionsForFileNumber:(NSInteger)fileNumber {
    BOOL success;
    useDelegate
    NSManagedObjectContext *context = appDelegate.managedObjectContext;
    if (context == nil) {
        NSLog(@"Error: No context");
        success = NO;
        return success;
    }
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"fileNumber == %d", fileNumber];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ScrollPosition" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:predicate];
    NSError *error = nil;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    NSLog(@"Number of deleted objects: %d", [fetchedObjects count]);
    for (NSManagedObject *object in fetchedObjects) {
        [context deleteObject:object];
    }
    success = YES;
    return success;
}

Change MBProgressHUD color

To change the background color of MBProgressHUD comment this line and add this in MBProgressHUD.m in drawRect method:

This code changes HUD color to blue.

Reload UITableView cells

Sometimes it’s needed to reload only some of the rows of UITableView to save some time and energy and not reload whole the table. This is how it can be done:

Save current position in UIScrollView and then scroll to this position

To save a position of the current scroll view use this method:

To scroll programmatically to the saved position use this method:

How to build MailCore framework on iOS

MailCore is a Cocoa e-mail framework built by Matt Ronge that is the core of the e-mail client Kiwi. MailCore provides a nice set of objects for working with IMAP, MIME and SMTP, all of which use the C e-mail library LibEtPan. Here is some short information about MailCore. This is short documentation on MailCore from Matt Ronge. Official website is here.

To make it run on your iOS simulator, do the following:

  1. Download MailCore from GitHub:
  2. Build m4, autoconf, automake, libtool on Mac OS X Lion using this post.
  3. Run update.sh script in MailCore to build and install everything before opening it in Xcode. If you don’t know, how to run shell scripts, read this
  4. Open MessageSender project in MailCore library. Select MessageSender to build. It will not do anything. To make it sending a test message write the following code in ViewDidLoad:
  5. Use this method to retrieve message subjects from Inbox of your mail:

Official MailCore website.

What’s new in iOS 6: developer-related features

This is the link for Apple Developers.

Brief content (developer-related features):

iOS 6.0

Maps
In addition to the new map tiles provided by Apple, the Maps app and MapKit framework now support additional interactions with other apps.

Social Framework
The Social framework (Social.framework) provides a simple interface for accessing the user’s social media accounts.

Pass Kit
Pass Kit is a new technology that uses web services, a new file format, and an Objective-C framework (PassKit.framework) to implement support for downloadable passes.

Game Center
Game Center has been updated and new features have been added to improve the game playing experience.

Reminders
The Event Kit framework now includes interfaces for creating and accessing reminders on the user’s device.

In-App Purchase
The Store Kit framework (StoreKit.framework) now supports the purchasing of iTunes content inside your app and provides support for having downloadable content hosted on Apple servers

Collection Views
The UICollectionView class offers a new way to present ordered data to users. With a collection view object, you are now able to define the presentation and arrangement of embedded views

UI State Preservation
State preservation makes it easier for apps to restore their user interface to the state it was in when the user last used it.

Auto Layout
Auto layout improves upon the “springs and struts” model previously used to lay out the elements of a user interface. With auto layout, you define rules for how to lay out the elements in your user interface.

Data Privacy
In addition to location data, the system now asks the user’s permission before allowing third-party apps to access certain user data

Additional Framework Enhancements