iOS

旧工程在Xcod5下出现的问题

最近把iOS6的项目拿到Xcode5下去编译并运行,出现了一些问题:

1、AssertMacros: queueEntry, file: /SourceCache/IOKitUser/IOKitUser-920.1.11/hid.subproj/IOHIDEventQueue.c,

修改main.m,添加如下代码

1
2
3
4
5
6
7
8
9
typedef int (*PYStdWriter)(void *, const char *, int);
static PYStdWriter _oldStdWrite;
int __pyStderrWrite(void *inFD, const char *buffer, int size)
{
    if ( strncmp(buffer, "AssertMacros:", 13) == 0 ) {
        return 0;
    }
    return _oldStdWrite(inFD, buffer, size);
}

然后在main()中的 @autoreleasepool 前添加

1
_oldStdWrite = stderr->_write; stderr->_write = __pyStderrWrite;
2、-[UIView setImage:]: unrecognized selector sent to instance 0x1d410f0

通过Debug定位到如下的代码:

1
2
3
4
5
6
for (int i = 0; i < [self.subviews count]; i++)
{
     UIImageView* dot = [self.subviews objectAtIndex:i];
     if (i == self.currentPage) dot.image = activeImage;
     else dot.image = inactiveImage;
}

self是一个UIPageControl,也就是它的subviews不都是UIImageView,所以要做一下检查,修改如下:

1
2
3
4
 if ([dot isKindOfClass:[UIImageView class]]) {
     if (i == self.currentPage) dot.image = activeImage;
     else dot.image = inactiveImage;
 }

关于strcmp

一个面试题 :-?

Q:
实现 strcmp(char c1,char c2)

A:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int strcmp_test(char *str1, char *str2)
{
    char    *s1 = str1;
    char    *s2 = str2;

    while (*s1 == *s2 && *s1 != '\0' && *s2 != '\0') {
        s1++; 
        s2++;
    }

    if (*s1 == *s2) {
        return 0;
    } else {
        if (*s1 > *s2) {
            return 1;
        } else {return -1; }
    }
}

Objective-C中的分类+load方法

分类是在运行时被添加到类中的,而定义分类的库可能是动态加载的,这就导致分类在比较晚的时候才被加载.

在第一次加载分类的时候会执行+load方法,可以使用他对静态变量进行初始化.

需要注意的是,在分类中是用init方法进行初始化不安全的,因为类自己已经实现了他,如果多个分类都实现init,那么无法预测哪个init会执行,但是对于load方法,每个分类都可以去实现他,而且所有的load方法都会执行,当然顺序也是不可预测的.还有,不要手动调用load方法!

load方法不会像init方法那样多次运行,他只会被调用一次,并且不能调用[super load];…

CocoaPods

CocoaPods一个Objective-C第三方库的管理利器。类似于Maven的。

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 $ sudo gem install cocoapods
 Password: 输入密码后就开始安装了,没有任何输出,需要等几分钟...
 Building native extensions. This could take a while...
 Building native extensions. This could take a while...
 Successfully installed xcodeproj-0.5.5
 Successfully installed escape-0.0.4
 Successfully installed json-1.7.7
 Successfully installed open4-1.3.0
 Successfully installed cocoapods-0.18.1
 5 gems installed
 Installing ri documentation for xcodeproj-0.5.5...
 Installing ri documentation for escape-0.0.4...
 Installing ri documentation for json-1.7.7...
 Installing ri documentation for open4-1.3.0...
 Installing ri documentation for cocoapods-0.18.1...
 Installing RDoc documentation for xcodeproj-0.5.5...
 Installing RDoc documentation for escape-0.0.4...
 Installing RDoc documentation for json-1.7.7...
 Installing RDoc documentation for open4-1.3.0...
 Installing RDoc documentation for cocoapods-0.18.1...

搜索

安装好后,就可以使用search命令来搜索你想要的library,例如:

1
2
3
4
5
6
7
$ pod search JSONKit

-> JSONKit (1.5pre)
   A Very High Performance Objective-C JSON Library.
   - Homepage: https://github.com/johnezang/JSONKit
   - Source:   https://github.com/johnezang/JSONKit.git
   - Versions: 1.5pre, 1.4 [master repo]

应用

以下通过一个实例演示,如何在一个xcode项目中使用pod

首先创建一个xcode项目,名字叫CocoaPodsTest,然后cd到工程所在目录ls一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ ls
CocoaPodsTest		CocoaPodsTest.xcodeproj
$ pod install
[!] No `Podfile' found in the current working directory.
$ sudo touch Podfile
Password:
$ vi Podfile

添加以下内容,保存退出
platform :ios
pod 'JSONKit'
$ pod install
Analyzing dependencies
Downloading dependencies
Installing JSONKit (1.5pre)
Generating Pods project
Integrating client project

[!] From now on use `CocoaPodsTest.xcworkspace`.

$ ls
CocoaPodsTest			CocoaPodsTest.xcworkspace	Podfile.lock
CocoaPodsTest.xcodeproj		Podfile				Pods
$ pod update
Analyzing dependencies
Downloading dependencies
Using JSONKit (1.5pre)
Generating Pods project
Integrating client project

现在通过双击CocoaPodsTest.xcworkspace打开项目就可以了;)

屏幕快照 2013-04-26 下午10.16.13
如果在使用import找不到头文件,需要再target里设置User Header Search Path为${SRCROOT},同时选择recursive。

自定义UIBarButtonItem的问题

显示一个按钮

1
2
3
4
5
6
7
8
9
10
11
12
13
14
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];

editButton.frame=CGRectMake(0, 0, 50, 30);

[editButton setBackgroundImage:[UIImage imageNamed:@"edit_off.png"] forState:UIControlStateNormal];

[editButton setBackgroundImage:[UIImage imageNamed:@"edit_on.png"] forState:UIControlStateHighlighted];

[editButton addTarget:self action:@selector(doneAction)  forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithCustomView:editButton];

self.navigationItem.rightBarButtonItem = editItem;

在iOS5 没有editButton.frame=CGRectMake(0, 0, 50, 30) 按钮会显示不出来。在iOS4 下 是不必须的…

ARC有效的工程中导人非ARC的代码

使用新特性ARC创建的工程,如果导入未进行ARC优化的代码,会产生编辑错误,比较粗暴的办法就是手动删除MRC代码,但是工作量巨大。

我最近做个一个项目,因为导入了新浪微博SDK,使用了一个比较方便的办法,就是让这些SDK在编译的时候不使用ARC。

设置方法如下:

引起Iphone应用被App Store拒绝的原因

Vibration. It is not permitted to use continuous vibration in your apps - short bursts as warnings is all that is allowed. Don’t bother trying to set up a timer to keep the vibration going, it will cause your app to be rejected.

Linking to private frameworks. This is obvious, but somehow in playing around with stuff we had linked to the MoviePlayer.framework. That’s a no-no, and cost us about ten days while we unlinked that framework, recompiled, and then resubmitted.

Improper handling of editing in tableview cells. Also obvious, but be aware that if you enable table cell editing, you’ll have to manually specify which cells should respond to editing controls and which should not. We had some random prefs cells in one of our early apps that were able to be swiped to bring up a ‘delete’ badge. Of course it didn’t do anything, but Apple justly considered this poor design and rejected our app.

Icons. Make sure the 57 pixel icon is identical to the 512 pixel version. Also, use a different icon if you are creating ‘lite’ and ‘pro’ versions of your app (i.e., free and paid). Using the same icon for both sends your app straight to … you guessed it … the bin.

Copying existing functionality. This one is much more subtle and insidious, and has probably affected the great percentage of developers. In addition to the widely publicized Podcaster debacle, reports from user comments indicate that Apple is casting a wide net when looking for duplicated functionality. Mini web browsers, or apps that essentially show web pages, seem particularly vulnerable, even if they add new and/or useful functionality. Stay away from email clients as well.

Using appropriate keyboard type. If your app asks for a phone number or other numeral-only input and you present a keyboard that also includes the possibility of entering standard alpha-numeric input … yep. (Thanks Jeremy1026)

Version numbers. If your app is currently at version 0.99 or below, you’d better consider giving it a promotion as Apple seems to prefer 1.0 and above. One of ours was recently rejected for being .016, with a message suggesting that our version number wasn’t even numeric. When we resubmitted the same app from scratch as version 1.0, it went through.

Network Reachability. If your app requires any type of network access you need to make sure it works when that access isn’t available. If it doesn’t it will be rejected. Apple provides sample code to test this which you can use as-is in most cases:http://developer.apple.com/library/ios/samplecode/Reachability/index.html

//=========================================================

振动。它是不允许的,在你的应用程序中使用连续振动 - 短时间作为警告是所有被允许的。不要理会试图建立一个计时器,以保持振动,它会导致您的应用程序被拒绝。

链接到私人框架。这是显而易见的,但不知何故在玩玩泥巴,我们已链接到MoviePlayer.framework。这是一个没有没有,和成本我们十天左右,而我们无关联的框架,重新编译,然后重新提交。

编辑处理不当tableview细胞。也是显而易见的,但要注意,如果启用表格单元格编辑,你必须手动指定哪些细胞应该响应编辑控件不应。我们已经在我们早期的应用程序,能够刷卡带来了一个“删除”徽章之一一些随机Prefs的细胞。当然,它没有做任何事情,但苹果公正地考虑这个可怜的设计,并拒绝了我们的的应用程序。

图标。确保57像素的图标是相同的512像素版本。此外,使用不同的图标,如果你正在创建“LITE”和“亲”你的应用程序的版本(即,免费和付费)。使用两个相同的图标,直接发送您的应用程序… …你猜对了… …完事。

复制现有的功能。这一个是更加微妙和阴险,并有可能影响开发商的很大比例。除了广泛宣传播客崩溃,从用户的意见的报告表明,苹果是铸造一个大网时复制功能。小型的Web浏览器,或应用程序,基本上显示网页,显得尤为脆弱,即使他们添加的新的和/或有用的功能。从电子邮件客户端以及。

使用适当的键盘类型。如果您的应用程序要求一个电话号码或其他数字,只有输入和你目前的键盘还包括进入标准的字母数字输入… YEP的可能性。 (谢谢Jeremy1026)

版本号。如果您的应用程序目前的版本为0.99或以下,你最好考虑给它一个推广苹果似乎更喜欢1.0及以上。我们最近拒绝为0.016,这表明我们的版本号,甚至没有数字的消息。当我们重新提交相同的应用程序1.0版从头的,它通过了。

网络可达性。如果您的应用程序需要任何类型的网络访问,您需要确保它的工作原理,访问不可用时。如果它不将被拒绝。苹果公司提供的示例代码来测试,您可以使用在大多数情况下是:
http://developer.apple.com/library/ios/samplecode/Reachability/index.html

www.J2meGame.com精心收集。

Objective-C内存管理教程和原理剖析

前言
初学objectice-C的朋友都有一个困惑,总觉得对objective-C的内存管理机制琢磨不透,程序经常内存泄漏或莫名其妙的崩溃。我在这里总结了自己对objective-C内存管理机制的研究成果和经验,写了这么一个由浅入深的教程。希望对大家有所帮助,也欢迎大家一起探讨。

此文涉及的内存管理是针对于继承于NSObject的Class。

获取iphone目录下所有图片

NSArray pngs = [[NSBundle mainBundle] pathsForResourcesOfType:@”png” inDirectory:nil];
for (id png in pngs)
{
NSString
name = [png lastPathComponent];
if([name rangeOfString:@”page_”].location!=NSNotFound)
{
UIImage *image = [UIImage imageNamed:[png lastPathComponent]];
}
}