代码改变世界

iOS 网易彩票-6设置模块三(常用小功能)

2015-09-14 00:10  jiangys  阅读(581)  评论(0编辑  收藏  举报

该篇文章中,用到很多iOS开发过程中常用的小功能,当前只是将这些功能集成到网易彩票的设置中。iOS-常用小功能介绍,请参考我的另一篇文章:

iOS 常用小功能 总结:http://www.cnblogs.com/jys509/p/4805030.html 

关于

效果图

思路分析:

  • 使用self.tableView.tableHeaderView 自定义组头部,通过加载xib来实现
  • 评分支持,使用【iOS 常用小功能 总结】中“应用评分”小功能
  • 客户电话,使用【iOS 常用小功能 总结】中“打电话”来实现

1.新建一个xib.看效果图,具体操作就不演示了。

 

相关代码 MJAboutHeaderView.h

#import "MJAboutHeaderView.h"

@implementation MJAboutHeaderView

+(instancetype)headerView
{
    return [[NSBundle mainBundle] loadNibNamed:@"MJAboutHeaderView" owner:nil options:nil][0];
}

@end

2.新建一个控制器MJAboutViewController,继承于MJBaseSettingViewController

在里面加载xib,并实现应用评分和打电话功能。

#import "MJAboutViewController.h"
#import "MJSettingArrowItem.h"
#import "MJSettingGroup.h"

@interface MJAboutViewController ()
//@property (nonatomic, strong) UIWebView *webView;
@end

@implementation MJAboutViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIWebView *webView = [[UIWebView alloc] init];
    webView.frame = CGRectZero;
    [self.view addSubview:webView];
    
    // 1.具体数据
    MJSettingItem *mark = [MJSettingArrowItem itemWithTitle:@"评分支持" destVcClass:nil];
    mark.option = ^{
        NSString *appid = @"725296055";
        NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];
        NSURL *url = [NSURL URLWithString:str];
        [[UIApplication sharedApplication] openURL:url];
    };
    
    MJSettingItem *call = [MJSettingArrowItem itemWithTitle:@"客户电话" destVcClass:nil];
    call.subtitle = @"10010";
    call.option = ^{
        NSURL *url = [NSURL URLWithString:@"tel://10010"];
        [webView loadRequest:[NSURLRequest requestWithURL:url]];
    };
    
    MJSettingGroup *group = [[MJSettingGroup alloc] init];
    group.items = @[mark, call];
    [self.data addObject:group];
    
    // 2.header
    self.tableView.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];
}
@end

分享

效果图

MJShareViewController.h 

#import "MJShareViewController.h"
#import "MJSettingArrowItem.h"
#import "MJSettingGroup.h"
#import <MessageUI/MessageUI.h>

@interface MJShareViewController () <MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate>

@end

@implementation MJShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //block里使用了强指针self,为了避免循环引用导致的内存泄露,要使用弱指针
    __unsafe_unretained typeof(self) selfVc = self;
    
    MJSettingItem *sina = [MJSettingArrowItem itemWithIcon:@"WeiboSina" title:@"新浪微博" destVcClass:nil];
    MJSettingItem *sms = [MJSettingArrowItem itemWithIcon:@"SmsShare" title:@"短信分享" destVcClass:nil];
    sms.option = ^{
        if (![MFMessageComposeViewController canSendText]) return;
        
        MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
        // 设置短信内容
        vc.body = @"吃饭了没?";
        // 设置收件人列表
        vc.recipients = @[@"10010", @"02010010"];
        // 设置代理
        vc.messageComposeDelegate = selfVc;
        
        // 显示控制器
        [selfVc presentViewController:vc animated:YES completion:nil];
    };
    
    MJSettingItem *mail = [MJSettingArrowItem itemWithIcon:@"MailShare" title:@"邮件分享" destVcClass:nil];
    mail.option = ^{
        // 不能发邮件
        if (![MFMailComposeViewController canSendMail]) return;
        
        MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
        
        // 设置邮件主题
        [vc setSubject:@"会议"];
        // 设置邮件内容
        [vc setMessageBody:@"今天下午开会吧" isHTML:NO];
        // 设置收件人列表
        [vc setToRecipients:@[@"55866@qq.com"]];
        // 设置抄送人列表
        [vc setCcRecipients:@[@"1234@qq.com"]];
        // 设置密送人列表
        [vc setBccRecipients:@[@"56789@qq.com"]];
        
        // 添加附件(一张图片)
        UIImage *image = [UIImage imageNamed:@"lufy.png"];
        NSData *data = UIImagePNGRepresentation(image);
        [vc addAttachmentData:data mimeType:@"image/png" fileName:@"lufy.png"];
        
        // 设置代理
        vc.mailComposeDelegate = selfVc;
        // 显示控制器
        [selfVc presentViewController:vc animated:YES completion:nil];
    };
    
    MJSettingGroup *group = [[MJSettingGroup alloc] init];
    group.items = @[sina, sms, mail];
    [self.data addObject:group];
}

/**
 *  短信 点击取消按钮会自动调用
 */
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [controller dismissViewControllerAnimated:YES completion:nil];
}

/**
 *  邮件 击取消按钮会自动调用
 */
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [controller dismissViewControllerAnimated:YES completion:nil];
}

- (void)dealloc
{
    NSLog(@"----MJShareViewController----");
}
@end

Block内循环引用导致的内存泄露

在block代码内,使用了强指针指向block所在的对象(例如self),导致循环引用,不能释放资源

将block内的对象引用设置为弱指针:使用"__weak"修饰符

注意:也不要使用强指针访问对象的属性
block内的对象引用设置为弱指针,除了上面的使用 __unsafe_unretained 
__weak typeof(self) selfVc = self;

 

系统源码下载:点击下载