Category Archives: iOS

iOS5不支持gmail推送的解决方法

iOS5后苹果居然不支持gmail的推送了。找到个解决方法:

  1. 删除原有的Gmail账户
  2. 添加新邮件账户,选择 Exchange (不是gmail)
  3. 输入 email, username(还是邮件名),passcode
  4. 接下来会提示输入服务器 m.google.com
  5. 验证后即重新支持推送了!
遗留问题:此方法不支持gmail的存档功能
资料来源:
1.installed IOS5, bye-bye gmail push
2.Setting up Google Sync with IOS

[iOS]设置界面快速生成脚手架介绍

写iOS程序最讨厌的就是写个如下图的设置界面 或 登陆(注册)界面了

这完全是体力活,没头脑,还得在cell里加入一大堆控件。同时由于tableview的重用机制,导致必须每次都得对各个控件设置属性,防止出现内容错误。

虽然iOS5中增加了新的静态tableview类型,可等它普及估计至少要半年,还是早些其他解决方法为妙。

目前有两个开源的代码能完成类似的功能

InAppSettingsKit

原来是将苹果app外部的setting.bundle在程序内部显示用的开源库。现在也提供了快速搭建脚手架的功能

地址 https://github.com/futuretap/InAppSettingsKit

QuickDialog

QuickDialog allows you to create HIG-compliant iOS forms for your apps without having to directly deal with UITableViews, delegates and data sources. Fast and efficient, you can create forms with multiple text fields, or with thousands of items with no sweat!

刚发现的,看起来比InAppSetting方便,未测试使用过~

地址 https://github.com/escoz/QuickDialog/

[iOS] In App Purchase 坑爹啊

原来计划几个小时内加个IAP功能,结果一天过去了,还没搞定。无奈的刷机中。

搜索了下IAP是个超级大坑,各种奇怪的问题,而且苹果没一份完整的文档解释可能出现的情况。于是有人根据回馈总结了这么个表格

To save you the pain of exhaustively searching the web for the cause of your error, here is a checklist of everything I have stumbled across that can cause an invalid product ID. Make sure you can answer “Yes” to each of these questions:

  • Have you enabled In-App Purchases for your App ID?
  • Have you checked Cleared for Sale for your product?
  • Have you submitted (and optionally rejected) your application binary?
  • Does your project’s .plist Bundle ID match your App ID?
  • Have you generated and installed a new provisioning profile for the new App ID?
  • Have you configured your project to code sign using this new provisioning profile?
  • Are you building for iPhone OS 3.0 or above?
  • Are you using the full product ID when when making an SKProductRequest?
  • Have you waited several hours since adding your product to iTunes Connect?
  • Are your bank details active on iTunes Connect?
  • Have you tried deleting the app from your device and reinstalling?
  • Is your device jailbroken? If so, you need to revert the jailbreak for IAP to work.

If you answered “No” to any one of these questions, there’s your problem.

原文附录了一大堆可能提供解决方案的链接,如果你也有类似的问题可以尝试下。
当然stackoverflow也有大量关于IAP的离奇问题

--- 更新 ---

刷玩最新的 4.3.5后,然后什么都没改动就突然能用了。

这里有篇写的不错的教程,希望对用IAP的人有帮助

In App Purchases: A Full Walkthrough

 

UITableView 离奇滚动到顶部的问题

你是否有过类似的经验,从一个TableView push到另一个视图后,然后pull back, 这时候有可能会发现tableView的contentOffset 已经滚动到顶部了,而且时有时无,所以也就没深入探讨下去了?

举个很简单的例子,现在有个附件位置聊天工具叫 陌陌, 我就喜欢在列表页面一直向下滑动找各种美女看PP,可是在看完美女资料返回列表后经常发现居然又滚回视图的顶部了,悲痛之余,又得话费很久时间重新滚动到下面,继续看美女。然后又被离奇的滚动到顶部。。。。

其实这个问题在 这篇文章 里提到过,没错还是 -viewDidUnload 导致的问题。首先我们来快速重现下这个bug

  1. 新建一个iOS项目,模板使用 Navigation-base App
  2. 在默认的tableViewController里
    1. - viewDidLoad 设置下 self.title=@”table”
    2. - numberOfRows 设置为 200 个cell
    3. 给每个cell添加点文字,当前的indexPath.row 的值就很好
    4. - didSelectRow 里随便push到个新的 ViewController
    5. - viewDidUnload 加个NSLog,方便跟踪消息
  3. 向下滚动些cell,然后点击进入第二个vc的视图
  4. 选择模拟器的菜单 Hardware->Simulate Memory warning , 你应该能在console里看到2行log,第二行log应该是来自于你的 -viewDidUnload  方法
  5. 返回tableView,你会发现bug重现,tableView的y偏移位已经变回0 !

其原因很容易解释,在接收到内存警报的通知后,非当前用户可见的视图控制器(vc)会接收到额外的通知,并将它的view给释放掉节约内存占用。然后当该vc的视图即将重新被用户可见时(比如被pullBack),vc会调用 -loadView , – viewDidLoad 这些方法重新将view创造出来, 但是偏移位却没有被保存下来,所以就发生这个Bug了。

写到这里应该已经知道如何解决这个问题了吧,增加一个变量记录来记录当前tableView.contentOffset.y 的数值,然后 – viewDidLoad 里面将tableview的偏移位给恢复即刻。

番外篇

iOS 中内存问题会导致各种奇怪的现象,在 – viewDidUnload被执行后会导致下次 – loadView, – viewDidLoad 被重新执行,所以一定要在 -viewDidUnload 中把其他retain的东西给释放掉。

每次通过模拟器的菜单来模拟内存警告比较麻烦,这里提供一个特殊的类来实现自动内存警报功能,将你的视图控制器从该类继承下来,这里以UIViewController为例子。当视图不可见时,该控制器便会自动接收到内存警告并释放掉自己的视图。这样子调试起来就方便了。有木有!!!

@interface MotherUIViewController : UIViewController
- (void)simulateMemoryWarning;
@end
@implementation MotherUIViewController
- (void)viewDidDisappear:(BOOL)animated{
	[super viewDidDisappear:animated];
	[self simulateMemoryWarning];
}
- (void)simulateMemoryWarning
{
#if TARGET_IPHONE_SIMULATOR
#ifdef DEBUG
	CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)@"UISimulatedMemoryWarningNotification", NULL, NULL, true);
#endif
#endif
}
@end

[iOS]UIActionSheet动态添加按钮

UIActionSheet 只提供了一个构造函数

- (id)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...

如果需要动态的添加按钮,就必须使用另外个函数:

- (NSInteger)addButtonWithTitle:(NSString *)title;

但出现的诡异结果是新添加的内容均出现在 取消 按钮之下,非常讨厌。

解决方法直接看代码

//.....
    UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"请选择埋掉的原因", @"reason to bury")
                                                       delegate:self
                                              cancelButtonTitle:nil
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:nil];
    [sheet addButtonWithTitle:@"btn1"];
    [sheet addButtonWithTitle:@"btn1"];
    [sheet addButtonWithTitle:@"btn1"];
    [sheet addButtonWithTitle:NSLocalizedString(@"取消", @"cancel button title")];
    sheet.cancelButtonIndex = sheet.numberOfButtons - 1;
//....

UIViewController 的内存管理

在iOS3.0后,UIViewController多了一个叫做viewDidUnLoad的方法.不少人都不清楚这个方法的具体意义,苹果的文档也就一句”Called when the controller’s view is released from memory” 简单的解释了下,并要求你把IBOutlet绑定的视图给清空,为什么呢?

先看下UIViewController从创建view到展示的流程的几个函数

-init
-initWithNibName:bundle:

这两个方法都是初始化一个vc,但请注意view不是这时候载入的

-loadView
-viewDidLoad

当一个视图准备展现时,vc首先会判断view是否已经创建,否则便通过之前指定的xib文件来初始化view,以及绑定其他关系(若没有指定xib文件,则默认会搜索和vc同名的xib,比如myNameViewController就会搜索 myNameViewController.xib文件)

若是没有xib文件,你就可以在loadview中自己手动创建这个viewControoler需要的视图.
接下来就是调用到 -viewDidLoad,许多人喜欢在这里做些其他事情,比如做个http请求,建立个数组啥的, 这里若不处理正确, -viewDidUnload 激活时内存就容易泄露了,稍后提到.

-view()appear
-view()disappear

这几个方法就不解释了

-viewDidUnload

该方法在收到内存警告,同时该视图并不在当前界面显示时候会被调用,此时该controller的view已经被释放并赋值为nil.
接下来你要做的是

  1. 把实例变量的子视图释放(IBOulet的,以及自己添加的),.
  2. 其他实例变量,比如之前在-viewDidLoaded中实例的数据数组,http请求释放掉.

因为当该viewController再次被激活准备显示时(比如navigationControler返回到上一级),vc发现自己的view为空后会重复之前的流程直到把view给创建起来,若没将自己额外添加的子视图,各种类实例变量释放,这里便会重新再次创建.

于是,内存泄露了.

iBook Page Curl Animation 翻页效果

好久没来RP掉的厉害,刚把wp升级到3.0,庆幸没什么异样。

最近很少花时间码代码,整理点资料加点RP

Steven 写的 Apple’s iBooks Dynamic Page Curl

Steven: I manage the CoreAnimation team at Apple. Are you interested in working for us?

注意文章的评论,以及另一个没使用private api的项目

http://github.com/brow/leaves

还有一篇

App Store-safe Page Curl animations by Ole Begemann

App Store 销售调查报告[2010-5]

见到cocoachina上dr_watson的分享

原文地址 http://techcrunch.com/2010/05/16/iphone-app-sales-exposed/

摘录其中一段

However, when the top 10% of the most successful apps are removed from the data set, the numbers skew much lower, giving a far better impression of what the iPhone industry looks like for most developers. In this scenario, the average sales were 11,625 total units, averaging 44 copies/day. Approximately 23% of apps sold less than 1000 units from launch (ranging from 12 to 370 days in the App Store). Further, 56% of apps sold less than or equal to 10,000 units, while 90% sold less than 100,000 units, with the remaining 10% achieving sales of 127,000 – 3,000,000 units.

WebView 小技巧

apple 把 webview 封装的实在太简单了.

留给开发者只有寥寥几个api,甚至连scrollview的基本操作都没法完成.

不过下面这个方法

stringByEvaluatingJavaScriptFromString:String

还是给了些希望.

搜索后发现果然javascript提供了一系列移动页面的操作

  • window.pageYOffset   获得当前页面的偏移
  • window.scrollTo(x,y) 将页面偏移到指定位置

这样我的需求就实现了.当然js能做的事情太多了,给webview换一套css也不是不可能的.

好像 Reeder 以及 便携百科ipad 就是这么做到的吧. 这两款软件都做得相当完美,体验上也无可挑剔.作者确实费了不少心血.

用心的产品才能引起共鸣.

Apple 发疯了

Apple 发布了 os4.0

新的Game Center -> openFeint , Plus+ 等第三方平台怎么活下去。矛盾开始了

iAD -> 公开指责 google ,又一个敌人

以及 3.3.1

3.3.1 — Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs. Applications must be originally written in Objective-C, C, C++, or JavaScript as executed by the iPhone OS WebKit engine, and only code written in C, C++, and Objective-C may compile and directly link against the Documented APIs (e.g., Applications that link to Documented APIs through an intermediary translation or compatibility layer or tool are prohibited).

这下 unity 和 即将来临的flash也被一棍打死了。

unity3d 论坛火爆讨论

Adobe 目前在twitter上的声明 :AdobeWe are looking into the new SDK language. We continue to develop Packager for iPhone OS which will debut in Flash #CS5

当然cocos2d 也赶紧来了个声明,哈哈不关我的事