iOS Notes



This blog is to record useful codes for iOS development.

Capture current screen

-(UIImage *)captureView:(UIView *)aView {
CGRect screenRect = [aView bounds];
UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//CGContextFillRect(ctx, screenRect);
[aView.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

Share Framework

 if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
    SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
    NSString *text = NSLocalizedString(@"trend", "");
    [mySLComposerSheet setInitialText:text];
    [mySLComposerSheet addImage:[self captureView:self.view]];
    [mySLComposerSheet addURL:[NSURL URLWithString:@"https://itunes.apple.com/au/app/guangzhou-air-quality-index/id533417463?mt=8"]];
    [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
        switch (result) {
            case SLComposeViewControllerResultCancelled:
                DLog(@"Post Canceled");
                break;
            case SLComposeViewControllerResultDone:
                DLog(@"Post Sucessful");
                break;

            default:
                break;
        }
    }];
    [self presentViewController:mySLComposerSheet animated:YES completion:nil];
} else {
    [SVProgressHUD showErrorWithStatus:NSLocalizedString(@"socialSetting", nil)];
}

Resources folder in xcode

[NSBundle mainBundle] uses the “Resources” folder in a xcode project. This folder can be created by creating a “New Group”.

Retset Xcode’s preferences

run this command in Therminal

defaults delete com.apple.dt.Xcode

Look for simulator folder

Use SimPholders2 http://simpholders.com/

Browser simulator app Sqlite data

When using SqliteBrowser to browser data, dbname.sqlite, dbname.sqlite-wal and dbname.sqlite-shm must exit together. Otherwise the data of simulator database cannot be found.