iOS开发Tips1


  • 动态获取iphone键盘的高度
  • ios异常捕获发送邮件
  • 邮件,信息,剪贴板
  • #include,#import,@class的区别
  • __bridge,__bridge_retained和__bridge_transfer的意思,区别与使用

---------------------------------------------------------------------


动态获取iphone键盘的高度


监听键盘呼出事件的消息:

    [[NSNotificationCenterdefaultCenter] addObserver:self
                                            selector:@selector(keyboardWillShow:)
                                                name:UIKeyboardWillShowNotification
                                              object:nil];

针对键盘高度做出自适应:

- (void)keyboardWillShow:(NSNotification *)notification
{
   staticCGFloat normalKeyboardHeight =216.0f; //iphone键盘
   NSDictionary *info = [notificationuserInfo];
    CGSize kbSize = [[infoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
   CGFloat distanceToMove = kbSize.height - normalKeyboardHeight;
        //自适应代码
}

猛戳这里获取Demo


---------------------------------------------------------------------

ios异常捕获发送邮件


将以下代码放在AppDelegate中:

void UncaughtExceptionHandler(NSException *exception)
{
    NSArray *arr = [exception callStackSymbols];
    NSString *reason = [exception reason];
    NSString *name = [exception name];
    NSString *urlStr = [NSString stringWithFormat:
                        @"mailto:123456@qq.com?subject=xxxxxx Bug Report &body=Thanks for your coorperation!<br><br><br>"
                        "AppName:xxxxxx<br>"\
                        "Details:<br>%@<br>--------------------------<br>%@<br>---------------------<br>%@",
                        name,reason,[arr componentsJoinedByString:@"<br>"]];
    NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [[UIApplication sharedApplication] openURL:url];
}

然后在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
中添加如下代码:
  NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);


---------------------------------------------------------------------

邮件,信息,剪贴板
在程序中我们经常会用到调用系统邮件,信息及剪贴板功能。
发送邮件(代码如下):
//发送消息的内容  
NSString *body = [NSString stringWithFormat:@"%@ %@/product%d.html",@"I found something good,I like this. Do you think I should buy it? ",HOST,self.shareProuct.uniqueId];  
NSString *subject = @"Everbuying";  
//检查是否能发送邮件,若没绑定邮箱账号,则会跳转到绑定的界面  
if ([MFMailComposeViewController canSendMail])  
{  
    MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];  
    mailVC.mailComposeDelegate =self;  
    [mailVC setSubject:subject];  
    //设置收件人  
    //[mailVC setToRecipients:[NSArray arrayWithObjects:@"example@gmail.com",nil]];  
    //设置附件  
    NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:shareProuct.thumb]];  
    [mailVC addAttachmentData:imgData mimeType:@"" fileName:@"product.jpg"];  
    //设置邮件内容  
    [mailVC setMessageBody:body isHTML:YES];  
    [self presentModalViewController:mailVC animated:YES];  
    [mailVC release];  
}  
else  
{  
    NSString* str = [NSString stringWithFormat:@"mailto:%@?subject=%@&body=%@",@"", subject, body];  
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];  
} 

记得引入MessageUI.framework,和头文件#import <MessageUI/MessageUI.h>。

在邮件发送完成,或者失败或取消后会执行一个代理方法:

#pragma mark -
#pragma mark MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController*)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError*)error
{
    NSString *msg=@"";
    
    switch (result)
    {
        case MFMailComposeResultCancelled:
        {
            msg = @"Mail Send Cancel";
            [self.presentingViewController toggleIndicatorShowWithMessage:msg withEndSelector:nil withHandle:nil];
        }
            break;
        case MFMailComposeResultSaved:
        case MFMailComposeResultSent:
        case MFMailComposeResultFailed:
        {
            BOOL isSuccess= NO;
            switch (result) {
                case MFMailComposeResultSaved:
                    isSuccess= YES;
                    msg = @"Email save success";
                    break;
                case MFMailComposeResultSent:
                    isSuccess= YES;
                    msg = @"Email send success";
                    break;
                case MFMailComposeResultFailed:
                    isSuccess= NO;
                    msg = @"Email save failed";
                    break;
                default:
                    break;
            }
            [self.presentingViewController toggleIndicatorShowWithIsSuccess:isSuccess withMessage:msg withEndSelector:nil withHandle:nil];
        }
            break;
        default:
            break;
    }
    [controller dismissViewControllerAnimated:NO completion:^{[self cancelShare];}];
}

app中发送信息(代码如下):
BOOL canSendSMS = [MFMessageComposeViewController canSendText];  
    NSLog(@"can send SMS [%d]", canSendSMS);  
    if (canSendSMS) {  
        MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init];  
        messageVC.messageComposeDelegate = self;  
        NSString *body = [NSString stringWithFormat:@"%@ %@/product%d.html",@"I found something good,I like this. Do you think I should buy it? ",HOST,self.shareProuct.uniqueId];  
        [messageVC setBody:body];  
        //messageVC.recipients = [NSArray arrayWithObject:@"18688886666"];  
        [self presentModalViewController:messageVC animated:YES];  
        [messageVC release];  
    }  
发送信息执行的代理方法与邮件类似,可参照。
拷贝内容(代码如下):
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];  
pasteboard.string = [NSString stringWithFormat:@"%@/product%d.html",HOST,shareProuct.uniqueId];

----------------------------- ----------------------------------------

#include,#import,@class的区别

  • #include
#include <> :用于对系统文件的引用,编译器会在系统文件目录下去查找该文件。
#include "xx.h":用于对用户自定义的文件的引用,编译器首先会去用户目录下查找,然后去安装目录,最后去系统目录查找。
注:使用include要注意重复引用的问题:
class A,class B都引用了class C,class D若引用class A与class B,就会报重复引用的错误。
  • #import
功能与include基本相同,不过它避免了重复引用的问题。所以在OC中我们基本用的都是import。
  • class
@class就是告诉编译器有这个类存在,但是类是如何实现的不用告诉编译器.若.m文件用到了这个类,还是要在.m文件汇总import这个类的。
既然这样,为什么不直接在头文件中import呢,举个例子:
class A引用了class B,class B引用了class C.... , class A,B,C...的头文件又import了很多文件,那么 import了A的话,编译器就需要编译大量的文件,编译时间就会增加。

难道头文件中都是用@class吗?当然不是,有时也是需要#import的,那么什么时候该用什么呢?
(1)一般如果有继承关系的用#import,如B是A的子类那么在B中声明A时用#import;

(2) 另外就是如果有循环依赖关系,如:A->B,B->A这样相互依赖时,如果在两个文件的头文件中用#import分别声明       对方,那么就会出现头文件循环利用的错误,这时在头文件中用@class声明就不会出错;

(3)还有就是自定义代理的时候,如果在头文件中想声明代理的话如@interface 

SecondViewController:UIViewController时应用#import不然的话会出错误,注意XXXXDelegate是自定义的。


参考:http://blog.sina.com.cn/s/blog_a843a8850101b6a7.html
http://blog.sina.com.cn/s/blog_b6e3f37101019f0j.html


---------------------------------------------------------------------

__bridge,__bridge_retained和__bridge_transfer的意思,区别与使用

最近碰到了CFObject和NSObject转换的问题,由于ARC不能管理Core Foundation Object的生命周期,所以在Core Foundation和ARC之间,我们需要使用到__bridge,__bridge_retained和__bridge_transfer三个转换关键字。

       根据苹果官方的文档(https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html):

__bridge只做类型转换,但是不修改对象(内存)管理权;

__bridge_retained(也可以使用CFBridgingRetain)将Objective-C的对象转换为Core Foundation的对象,同时将对象(内存)的管理权交给我们,后续需要使用CFRelease或者相关方法来释放对象;

__bridge_transfer(也可以使用CFBridgingRelease)将Core Foundation的对象转换为Objective-C的对象,同时将对象(内存)的管理权交给ARC。

转自:http://www.1mima.com/__bridge__bridge_retained和__bridge_transfer的意思,区别与使用/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值