ios经常使用方法

my百宝箱

1.隐藏状态栏

-(BOOL)perfersStatusBarHidden
{
    return YES;
}

2.

高度为0以后,UILabel仍是可见的,UIImageView不可见html

数组快速执行方法

/**
 *  让数组的每个对象都执行同一个操做
 *
 *  @param aSelector 操做方法名
 *  @param argument  传递的参数
 */
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;

等同于ios

for (int i=0; i<array.count; i++) {
    UIButton *btn = self.array[i];
    btn.selected = NO;
}

3,构造方法注意点

自定义的类中,经常要写initWith....相似的构造方法,而后在里面修改对象自己self并返回。这里要注意的是:不能将init后面的with的首字母写成小写,那样就不是构造方法了。也就不能修改对象自己了。web

四、本地json 文件转换成模型

//从json文件中读取
    NSString *jsonPath = [[NSBundle mainBundle]pathForResource:@"products" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:jsonPath];
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    //将字典转化成模型
    NSMutableArray *mutableArray = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        Product *product = [Product productWithDict:dict];
        [mutableArray addObject:product];
    }
    _productArray = mutableArray;

五、LoadView

自定义view的时候在这个方法里面更改系统的viewjson

六、打电话

  • 方法一
    最简单最直接的方式:直接跳到拨号界面
NSURL *url = [NSURL URLWithString:@"tel://10010"];
[[UIApplication sharedApplication] openURL:url];
缺点
电话打完后,不会自动回到原应用,直接停留在通话记录界面
  • 方法二
拨号以前会弹框询问用户是否拨号,拨完后能自动回到原应用
NSURL *url = [NSURL URLWithString:@"telprompt://10010"];
[[UIApplication sharedApplication] openURL:url];
缺点
由于是私有API,因此可能不会被审核经过(尽可能不要用 )
  • 方法三
建立一个UIWebView来加载URL,拨完后能自动回到原应用
if (_webView == nil) {
    _webView = [[UIWebView alloc] initWithFrame:CGRectZero];
}
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10010"]]];

须要注意的是:这个webView千万不要添加到界面上来,否则会挡住其余界面

七、发短信

  • 方法一
直接跳到发短信界面,可是不能指定短信内容,并且不能自动回到原应用
NSURL *url = [NSURL URLWithString:@"sms://10010"];
[[UIApplication sharedApplication] openURL:url];
  • 方法二

若是想指定短信内容,那就得使用MessageUI框架数组

  • 包含主头文件
#import <MessageUI/MessageUI.h>
  • 显示发短信的控制器
if (![MFMailComposeViewController canSendText]) return;
MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
// 设置短信内容
vc.body = @"吃饭了没?";
// 设置收件人列表
vc.recipients = @[@"10010", @"02010010"];
// 设置代理
vc.messageComposeDelegate = self;
// 显示控制器
[self presentViewController:vc animated:YES completion:nil];
  • 代理方法,当短信界面关闭的时候调用,发完后会自动回到原应用
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    // 关闭短信界面
    [controller dismissViewControllerAnimated:YES completion:nil];

    if (result == MessageComposeResultCancelled) {
        NSLog(@"取消发送");
    } else if (result == MessageComposeResultSent) {
        NSLog(@"已经发出");
    } else {
        NSLog(@"发送失败");
    }
}

八、发邮件

  • 方法一

用自带的邮件客户端,发完邮件后不会自动回到原应用浏览器

NSURL *url = [NSURL URLWithString:@"mailto://10010@qq.com"];
[[UIApplication sharedApplication] openURL:url];
  • 方法二

跟发短信的第2种方法差很少,只不过控制器类名叫作:MFMailComposeViewControllerapp

// 不能发邮件
if (![MFMailComposeViewController canSendMail]) return;

MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];

// 设置邮件主题
[vc setSubject:@"会议"];
// 设置邮件内容
[vc setMessageBody:@"今天下午开会吧" isHTML:NO];
// 设置收件人列表
[vc setToRecipients:@[@"643055866@qq.com"]];
// 设置抄送人列表
[vc setCcRecipients:@[@"1234@qq.com"]];
// 设置密送人列表
[vc setBccRecipients:@[@"56789@qq.com"]];

// 添加附件(一张图片)
UIImage *image = [UIImage imageNamed:@"lufy.jpeg"];
NSData *data = UIImageJPEGRepresentation(image, 0.5);
[vc addAttachmentData:data mimeType:@"image/jepg" fileName:@"lufy.jpeg"];

// 设置代理
vc.mailComposeDelegate = self;
// 显示控制器
[self presentViewController:vc animated:YES completion:nil];

邮件发送后的代理方法回调,发完后会自动回到原应用框架

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    // 关闭邮件界面
    [controller dismissViewControllerAnimated:YES completion:nil];

    if (result == MFMailComposeResultCancelled) {
        NSLog(@"取消发送");
    } else if (result == MFMailComposeResultSent) {
        NSLog(@"已经发出");
    } else {
        NSLog(@"发送失败");
    }
}

九、打开其余应用

若是想打开一些常见文件,好比html、txt、PDF、PPT等,均可以使用UIWebView打开dom

只须要告诉UIWebView文件的URL便可url

至于打开一个远程的共享资源,好比http协议的,也能够调用系统自带的Safari浏览器:

NSURL *url = [NSURL URLWithString:@”http://www.baidu.com"];
[[UIApplication sharedApplication] openURL:url];

十、应用间跳转

有时候,须要在本应用中打开其余应用,好比从A应用中跳转到B应用

首先,B应用得有本身的URL地址(在Info.plist中配置)

B应用的URL地址就是:YL://ios.xyl.cn

接着在A应用中使用UIApplication完成跳转

NSURL *url = [NSURL URLWithString:@"YL://ios.xyl.cn"];
[[UIApplication sharedApplication] openURL:url];

十一、应用评分

为了提升应用的用户体验,常常须要邀请用户对应用进行评分

应用评分无非就是跳转到AppStore展现本身的应用,而后由用户本身撰写评论

如何跳转到AppStore,而且展现本身的应用
方法1

//ios7不支持
NSString *appid = @"444934666";
NSString *str = [NSString stringWithFormat:
                 @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@", appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

方法2
NSString *str = [NSString stringWithFormat:
                 @"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

十二、随机数

arc4random_uniform(256) / 255.0;

控制台打印临时变量的类型

相关文章
相关标签/搜索