ios开发中的常见问题和解决以及总结

来源:互联网 发布:网页数据采集器 开源 编辑:程序博客网 时间:2024/05/17 23:12


1.如何从自己的app跳到系统设置界面?


需要在需要跳转的地方调用下面的方法,以openURL的方式

//蓝牙设置界面

   NSURL *url = [NSURL URLWithString:@"prefs:root=Bluetooth"];

   if ([[UIApplication sharedApplication] canOpenURL:url])

   {

      [[UIApplication sharedApplication] openURL:url];

   }

其他设置界面的调用只需要修改prefs:root=...即可。

2.判断是否耳机插入

#import <AVFoundation/AVFoundation.h>


- (BOOL)isHeadsetPluggedIn {

    AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];

    for (AVAudioSessionPortDescription* desc in [route outputs]) {

        if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])

            return YES;

    }

    return NO;

}


3.在pch文件中添加下列预处理指令,然后在项目中使用Log(…)来输出日志信息,就可以在发布应用的时候,一次性将NSLog语句移除(在调试模式下,才有定义DEBUG)

#ifdef DEBUG

#define Log(...) NSLog(__VA_ARGS__)

#else

#define Log(...) /* */

#endif

4.infoPlist中常用的属性项和说明如下:

Localization  native  name  --- >本地化设置

Bundle  display name --- >程序在安装后显示的App名字

Icon  file --- >设置程序图标(Xcode5以后再Images.xcassets中设置)

Main storyboardfile base name --- >主storyboard文件的名称

Bundle  version --- >程序版本号,用于内部项目管理

Bundle  version string, short --- >在itunes上显示的版本号,对外使用

Bundle  identifier --- >应用的唯一标识

5.真机调试出现下面问题   :bitcode相关

‘/Users/**/Framework/SDKs/PolymerPay/Library/mobStat/lib**SDK.a(**ForSDK.o)’does not contain bitcode. You must rebuild it with bitcode enabled (Xcodesetting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64

解决方法:进入TARGETS->BuildingSetting,将Enable Bitcode设置为NO.


6.白名单问题:

升级到xcode7.2,如果项目有要跳转到其他app的,则需要在info.plist中添加对应app的Scheme,否则无法调用或跳转。 



7.打包遇到问题

Failed to locate or generate matching signing assets
Xcode attempted to locate or generate matching signing assets and failed to do so because of the following issues.
Missing iOS Distribution signing identity for ... Xcode can request one for you.

截图如下



原因是Apple World Wide Developer Relations Certificate Authority的过期时间是2016年2月14。苹果的回答如下:

Thanks for bringing this to the attention of the community and apologies for the issues you’ve been having. This issue stems from having a copy of the expired WWDR Intermediate certificate in both your System and Login keychains. To resolve the issue, you should first download and install the newWWDR intermediate certificate (by double-clicking on the file). Next, in the Keychain Access application, select the System keychain. Make sure to select “Show Expired Certificates” in the View menu and then delete the expired version of the Apple Worldwide Developer Relations Certificate Authority Intermediate certificate (expired on February 14, 2016). Your certificates should now appear as valid in Keychain Access and be available to Xcode for submissions to the App Store.

简单的说就是颁发开发者证书的根证书过期了。如果这个时候你打开keychain看你的发布证书会是这样的:

pastedGraphic_2.png



就是这个Apple World Wide Developer Relations Certificate Authority过期了,所以这个颁发的证书都不能使用了。

现在来说下解决方案:

1.打开keychain(钥匙串),在登录和系统中找到过期的 Apple World Wide Developer Relation Certification Authority,然后删除它
注意在keychain显示菜单下,设置成显示过期证书pastedGraphic_3.png


2.下载这个链接里的AppleWWDRCA.cer的证书到本地

3.记得要把系统钥匙串的设置权限打开pastedGraphic_4.png


4.把AppleWWDRCA.cer安装到登录和系统中

设置成功后就可以了。查看下你的发布证书是否已经正常了。


pastedGraphic_5.png


8.iOS UITableView中点击状态栏无法回滚到顶部

解决:必须只有一个scrollView设置属性scrollToTop=yes,其他scrollView的该属性设置为NO,才可以实现。


9.防止Block的循环引用:

__weak typeof(self) weakSelf = self;


10.枚举的推荐写法:

typedef NS_OPTIONS(NSUInteger, UIControlState) {

UIControlStateNormal       = 0,

UIControlStateHighlighted  = 1 << 0,

UIControlStateDisabled     = 1 << 1,

};


11.使用第三方框架,尽量不要更改内部文件,而应该再次封装,个性定制;


12.在一个自定义的View中,或者自定义cell中,modal出一个控制器建议:

[UIApplication sharedApplication].keyWindow.rootViewController


13.从系统相册中取照片之前,应该判断系统相册是否可用,如果从相机中拍照获取,要判断相机是否可用

// 判断相册是否可以打开

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;


//  判断相机是否可以打开

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;


14.耗时操作应该放在子线程,避免卡主主线程,比如计算文件大小,下载大文件,清除缓存;


15.建议POST请求参数字典的写法,这样比较装逼~

// NSDictionaryOfVariableBindings这个宏生成一个字典,这个宏可以生成一个变量名到变量值映射的Dictionary,比如:

NSNumber * packId=@(2);

NSNumber *userId=@(22);

NSNumber *proxyType=@(2);

NSDictionary *param=NSDictionaryOfVariableBindings(packId,userId,proxyType);


16.navgationBar实现渐变效果网址

http://www.ithao123.cn/content-10965040.html


17.将图片转换为base64位编码(一)

- (BOOL) imageHasAlpha: (UIImage *) image

{

    CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);

    return (alpha == kCGImageAlphaFirst ||

            alpha == kCGImageAlphaLast ||

            alpha == kCGImageAlphaPremultipliedFirst ||

            alpha == kCGImageAlphaPremultipliedLast);

}

- (NSString *) image2DataURL: (UIImage *) image

{

    NSData *imageData = nil;

    NSString *mimeType = nil;

    

    if ([self imageHasAlpha: image]) {

        imageData = UIImagePNGRepresentation(image);

        mimeType = @"image/png";

    } else {

        imageData = UIImageJPEGRepresentation(image, 1.0f);

        mimeType = @"image/jpeg";

    }

    

    return [NSString stringWithFormat:@"data:%@;base64,%@", mimeType,

            [imageData base64EncodedStringWithOptions: 0]];

    

}


18.图片转换为base64(二)

http://www.ithao123.cn/content-10965038.html


19.获取图片后缀

+ (NSString *)typeForImageData:(NSData *)data {

    uint8_t c; 

    [data getBytes:&c length:1];

    switch (c) { 

        case 0xFF:   

            return @"image/jpeg";

        case 0x89:

            return @"image/png";

        case 0x47:

            return @"image/gif";

        case 0x49:

        case 0x4D:

            return @“image/tiff";

    }

    return nil;

}


20.IOS中 xib自定义View在storyboard中的使用

http://blog.csdn.net/wlq861025/article/details/50727678


里面有setFrame的使用,受教!


21.ios 指定某个页面是横屏还是竖屏

适用于某些页面显示为横屏,有些显示为竖屏的产品需求中


22.获取文件路径(plist文件存储)

NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;

    NSString *fileName = [path stringByAppendingPathComponent:@“123.plist"];

存储时使用writeToFile: atomically:方法。 其中atomically表示是否需要先写入一个辅助文件,再把辅助文件拷贝到目标文件地址。这是更安全的写入文件方法,一般都写YES。

读取时使用arrayWithContentsOfFile:方法。


23.ios应用内直接跳转到Appstore

http://www.ithao123.cn/content-10962266.html


24.iOS-顶部提示框源码分享,在顶部弹出提示框

http://www.ithao123.cn/content-10961851.html


25.TIPS

1)滑动时隐藏navigationbar

navigationController.hidesBarsOnSwipe = Yes

2)解决左划返回失效

self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;

3)CollectionView 怎么实现tableview那种悬停的header?

CSStickyHeaderFlowLayout

4)使用剪切板共享数据

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];   pasteboard.string=self.label.text;

5)获取触摸的点

- (CGPoint)locationInView:(UIView *)view; 

  • (CGPoint)previousLocationInView:(UIView *)view;


26.利用UIWebView显示pdf文件,网页等等


UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.bounds]; 

webView.delegate = self; webView.scalesPageToFit = YES;

webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

[webView setAllowsInlineMediaPlayback:YES]; 

[self.view addSubview:webView]; 

NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"book" ofType:@"pdf"]; NSURL *url = [NSURL fileURLWithPath:pdfPath]; 

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:5]; 



27.加载大量图片的时候可以使用

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"png"]; 

UIImage *myImage = [UIImage imageWithContentsOfFile:imagePath];


28.各种炫酷动画集合

https://github.com/sxyx2008/awesome-ios-animation


29.利用友盟定位ios线上奔溃位置

http://www.ithao123.cn/content-10951494.html


30.很多开源项目和库总结

http://www.ithao123.cn/content-10948845.html


31.使用UINavigationController自带的属性hidesBarsOnSwipe来使得轻扫屏幕的时候可以隐藏navBar和toolBar!

0 0
原创粉丝点击