iOS7较完善的变化

  • 坏消息:IOS7中有一些可能对app造成破坏性的变化,你必须要了解。因为当它们在ios7上运行的时候可能会造成app崩溃!
    • 已禁用-UIDevice uniqueidentifier
      • [UIDevice uniqueldentifier]在ios5实际就已经被遗弃了但是在iOS7中已经完全的禁用它了,即使是在ios7之前使用的app它也不会返回设备的uuid而是返回一串字符串,以ffffff开头,
    • UIPasteboard由共享变为沙盒化了
      • UIPasteboard过去用来做app之间的数据分享.UIPasterboard本没问题,但是开发者开始使用它来存储标示符,和其他的相关app分享这些标识符的时候问题就出现了,有一个使用这种的就是openudid,在ios7中使用+[UIPasterboard pasteboardWithName:create:]和+[UIPasteboard pasteboardWithUniqueName]创建剪切版,而且只是对相同的app groupk可见,这样就让openudid不那么有用了。
    • mac地址不能再用来识别设备
      • 使用ios设备的media access control(mac)地址也是可以生成唯一的标识符。他是物理网络层级分配给网络适配器的,这个地址又叫硬件地址和wifi地址。有很多工程和框架都使用这个方法来生成唯一的设备id ,然而苹果并不希望有人通过mac 地址来分辨用户,所以如果你在ios7系统上查询mac地址,他会只是返回02:00:00:00:00:00。现在苹果明确的表明你应该使用-[UIDevice identifierForVendor]或者-[ASIdentifierManager advertisingidentifier]来作为你框架和应用的唯一标示符,代码附上:
    NSString *identifierForVendor = [[UIDevice currentDevice].identifierForVendor UUIDString];
    NSSring *identifierForAdvertising  = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
  • identifierForVendor对供应商来说是一个唯一的值,由同一个公司发行的app在相同的设备上面的时候都会有这个相同的标识符,然后如果用户删除这个供应商的app然后在重新安装的话这个标识符就会不一致
  • advertisingIdentifier会返回给在这个设备上所有软件供应商相同的一个值所以只能在广告的时候使用。
    • ios现在要求app如需使用麦克风,需要征得用户同意:
      • 以前如果app需要使用用户的位置、通讯录、日历、提醒以及照片、接受推送消息、使用用户的社交网络的时候需要征的用户的同意,现在在ios7当中使用麦克风也需要取得用户的同意了。如果用户不同意app使用麦克风的话,那么使用麦克风的app就不能接收任何声音。
  • 好消息:有一些你熟悉的特性和api在ios7中的到了优化--但 是还有一些其他的特性被遗弃了,
    • -[NSArray firstObject]的实现
      • [NSArray firstObject]可以自己判断数组是否为空。
    • 增加了instancetype
      • instancetype用来代替id作为返回类型,用来声明一个方法时告诉编译器其返回类型,它表示返回调用该方法的类的对象,这比之前返回id好,编译器可以对返回类型做一些检查,如果出现错误在编译的时候可以提醒你,而不是在程序运行的时候发生奔溃,同时在调用子类方法时候使用它还可以省去返回值强制类型转换,编译器能够正确推断方法的返回值类型。
    • 设置UIimage的渲染模式:UIimage.renderingMode
      • 默认使用UIInageRenderingModeAutomatic (是否使用渲染取决于它显示的位置)
        UIImageRenderingModeAutomatic // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。
        UIImageRenderingModeAlwaysOriginal // 始终绘制图片原始状态,不使用Tint Color。
        UIImageRenderingModeAlwaysTemplate // 始终根据Tint Color绘制图片,忽略图片的颜色信息。
        
      • 使用:
        UIImage *img = [UIImage imageNamed:@"myimage"];
        img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        
    • tintColor vs barTintColor
      • 为了避免使用新属性的时候犯错误,如果你的appp需要支持iOS6以前的系统的时候,请检查一下
        UINavigationBar *bar = self.navigationController.navigationBar;
        UIColor *color = [UIColor greenColor];
        if ([bar respondsToSelector:@selector(setBarTintColor:)]) { // iOS 7+
        bar.barTintColor = color;
        } else { // what year is this? 2012?
        bar.tintColor = color;
        }
        
    • 去掉了纹理颜色
    • UIButtonTypeRoundRect被UIButtonTypeSystem 取代了
  • 非常好的消息:ios7的新功能
    • 检查无线路由是否可用
      • 在ios7当中,你可以通过Airplay,蓝牙或是其他的虚线机制了解是否有一个远程的设备可用。如果没有远程设备的时候就不显示AirPlay的icon,以下是新增加的MPVolumeView的新属性和推送
        @property (nonatomic,readonly)BOOL wirelessRoutesAvailable;//是否有设备可以连接的无线
        @property (nonatomic,readonly)BOOL wirelessRouteActive;//设备现在是否连接上了网络
        NSString *const MPVolumeViewWirelessRouteAvailableDidChangeNotificatuion;//是否有无线路线的通知
        NSSting *const MPVoumeViewWirelessRouteActiveDidChangeNotification//设备是否连接上了网络通知
        
    • 了解蜂窝网络
      • 在ios7之前使用Reachability来检测设备是和否连接wlan或者wifi。在ios7之后增加了currentRadioAccessTechnology属性和CTRadioAccessTechnologyDidChangeNotification, 以下是使用方法
        #import <CoreTelephony/CTTelephonyNetworkInfo.h>//加入头文件
        @property (nonatomic,strong) CTTelephonyNetworkInfo *networkInfo;
        self.networkInfo = [[CTTelephonyNetworkInfo alloc] init];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(radioaccessChange) name:CTRadioAccessTechnoloyDidChangeNotification object:nil];


        -(void)radioaccessChange
        {
            nslog(@"now you are connected via %@",self.networkInfo.currentRadioAccessTechnology);
        }
        注意:如果没有连接上网络那么返回的就是nil;
  • next

    • 通过icloud同步用户设备的密码

      • 开发者使用SSKeychain库来处理keychaind使用代码如下

        #import<SSKeychain.h>
        
        -(BOOL)saveCredntials:(NSError *)error
        {
         SSKeychainQuery *query = [[SSKeychainQuery alloc]init];
         query.password = @"myserectPassword";
         query.service = @"myAwesomeService";
         query.accont = @"john Doe";
         query.synchronizable = yes;
         return [query save:&error];
        }
        
        -(NSString *)savePassword:(NSError *)error
        {
         SSKeychainQuery *query = [[SSKeychainQuery alloc]init];
         query.service = @"myAwesomeService";
         query.account = @"john Doe";
         query.synchronizable = yes;
         query.password = nil;
         if([query fetch:&error])
         {
             return query.password;
         }
         return nil;
        }
        尽量使用cocoapods来安装SSKeychain
        
    • 使用nsattributedstring显示html

      • 可以使用少量的代码在html文件中创建一个NSAttributedString

        NSString *html = @"<bold>Wow!</bold> now <em>ios</em>can create <h3>NSAttributedString</h3>from HTMLS!";
        NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
        options:options documentAttributes:nil error:nil];
        

        你还可以从NSAttributedString中创建一个HTML字符串

        
        NSAttributedString *attrString; // from previous code
        NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
        
        NSData *htmlData = [attrString dataFromRange:NSMakeRange(0, [attrString length]) documentAttributes:options error:nil];
        NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
        
    • 使用原生的base64
      • Base64是使用ASCII码显示二进制数据的一种流行方法在ios7引入四种NSData方法来操作base64
        // From NSData.h

    /* Create an NSData from a Base-64 encoded NSString using the given options. By default, returns nil when the input is not recognized
 as valid Base-64.
    */
- (id)initWithBase64EncodedString:(NSString *)base64String options:(NSDataBase64DecodingOptions)options;

    /* Create a Base-64 encoded NSString from the receiver's contents using the given options.
*/
    - (NSString *)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)options;

    /* Create an NSData from a Base-64, UTF-8 encoded NSData. By default, returns nil when the input is not recognized as valid Base-64.
    */
    - (id)initWithBase64EncodedData:(NSData *)base64Data options:(NSDataBase64DecodingOptions)options;

    /* Create a Base-64, UTF-8 encoded NSData from the receiver's contents using the given options.
*/
    - (NSData *)base64EncodedDataWithOptions:(NSDataBase64EncodingOptions)options;

如果你需要支持iOS6或者更早以前的系统,你可以使用以下两个方法
- (id)initWithBase64Encoding:(NSString *)base64String;
- (NSString *)base64Encoding;
  • t

    • 使用UIApplicationUserDidTakeScreenshotNotification来检查截图
      • UIApplicationUserDidTakeScreenshotNotification 将会在截图完成之后显示。现在在截图截取之前无法得到通知
    • 实现多语言语音合成
      • IOS7加入了两个新类:AVSpeechSynthesizer(用来发出声音)以及AVSpeechUtterance(代表你想说什么,如何说)代码如下:
        AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
        AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"wow .hello"];
        uttertance.rate = AVSpeechUtteranceMaximunSpeechRate / 4.0f;
        uttertance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"]//系统语言
        [synthesizer speakUtteranceutterance];
        
    • 使用了新的手势识别
      • UIScreenEdgePanGestureRecognizer继承了UIPanGestureRecognizer它可以让你从屏幕边沿就可以检测到手势,使用如下:
        UIScreenEdgePanGestureRecognizer *recognizer = [UIScreenEdgePanGestureRecognizer alloc]initwithTarget:self action:@selector(handlsScreenEdgeRecognizer:)];
        recognizer.edges = UIRectEdgeLeft;
        [self.view addGestureRecognizert:recognizer];
        
    • 使用UIScrollViewKeyboardDismissMode实现了Message app 的行为
      • 苹果给UIScrollView添加了一个很好用的属性keyboardDismissMode,现在仅仅只需要在Storyboard中改变一个简单的属性,或者增加一行代码,你的app可以和办到和Messages app一样的事情了。这个属性使用了新的UIScrollViewKeyboardDismissMode enum枚举类型。这个enum枚举类型可能的值如下
        UIScrollViewKeyboardDismissModeNone        // the keyboard is not dismissed automatically when scrolling
        UIScrollViewKeyboardDismissModeOnDrag      // dismisses the keyboard when a drag begins
        UIScrollViewKeyboardDismissModeInteractive // the keyboard follows the dragging touch off screen, and may be
        pulled upward again to cancel the dismiss
        
    • 使用Core image来检测眨眼以及微笑
      • IOS给Core image增加了两种人脸检测功能:CIDetectorEyeBlink以及CIDetecrSmile,可以在图片中检测微笑以及眨眼。使用方法如下:
        UIImage *image = [UIImage imageNamed:@"myimage"];
        CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
        NSDictionary *options = @{CIDetectorSmile:@yes,CIDetectorEyeBlink:@yes};
        NSArray *features = [detector featuresimage:image.CIImage options:iotions];
        for(CIFaceFeature *feature in features)
        {
          if(feature.hasSmile)
          {
          nslog(@"nice smile");
          }
          if(feature.leftEyeClosed || feature.rightEyeClosed)
          {
          nslog(@"open your eyes");
          }
        }
        
    • 给UITextView增加了链接
      • 现在你可以给一个NSAttributedString增加链接了,然后当它被点击的时候唤起一个定制的action。
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];
[attributedString addAttribute:NSLinkAttributeName
                         value:@"username://marcelofabri_"
                         range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];


NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
                                 NSUnderlineColorAttributeName: [UIColor lightGrayColor],
                                 NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};

// assume that textView is a UITextView previously created (either by code or Interface Builder)
textView.linkTextAttributes = linkAttributes; // customizes the appearance of links
textView.attributedText = attributedString;
textView.delegate = self;

这样就可以让链接在文本中显示。然而,你也可以控制当链接被点击的时候会发生什么,实现这个可以使用UITextViewDelegate协议的新的shouldInteractWithURL方法,就像这样:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange { 
    if ([[URL scheme] isEqualToString:@"username"]) { 
        NSString *username = [URL host];  
        // do something with this username 
        // ... 
        return NO; 
    } 
    return YES; // let the system open this URL 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值