Maha 0.9.2

Read later (Instapaper/Read it later) + Mobilizer (Google/Instapaper) + Translation (Google)

Read later + Mobilizer + Translation

Custom background

Custom background

Upload Image to img.ly

I am not sure if I did something wrong or not,
but everytime when I submit a should-be-no-problem request to img.ly,
it returns 406 without any further information.
That response code wasn’t documented and I cannot find anything from google,so I decided to figure that out.
First, I tried clients on my mac which uploads to img.ly:
Numbu uses the old API, boo.
Twitter for mac (no longer Tweetie for mac….) uses the old API too, but it treats the old API as the new one,
submitting OAuth Echo infos to http://img.ly/api/upload which doesn’t make sense at all,
it should not work at all, but it dose.
So I am not going to follow it.
The last one is Twitterific, luckily it dose what I want.
It submits to http://img.ly/api/2/upload.xml which is what I am trying to do.
After comparing my failed request and Twitterific’s success request, here are points that lead me to fail:
  • Content-Type: image/jpeg , I used ASIHttpRequest which uses application/octet-stream as the default content-type, so the solution is
    [request setData:UIImageJPEGRepresentation(picture, 0.8) withFileName:@"filename.jpg" andContentType:@"image/jpeg" forKey:@"media"];
  • User-agent, this is super weird. Using “Maha 1.0 (iPhone Simulator; iPhone OS 4.2; en_US)” as the user agent, which is the default, makes me fail. But if I copy Twitterific’s user agent “Twitterrific/4.0 CFNetwork/454.11.5 Darwin/10.6.0 (i386)”, it works like a charm. So my solution is
    [request addRequestHeader:@"User-Agent" value:@"Maha/1.0 CFNetwork/454.11.5 Darwin/10.6.0 (i386)"];

And everything works now, tho I don’t know what was the real problem.

不讓app hang起來的頹廢方法

終於用了一次之前 @moming2k 很喜歡的dispatch_async
好棒好棒
簡單點來說吧,首先要

#include <dispatch/dispatch.h>

之後會hang住的東西丟到裡面去就ok了

dispatch_queue_t gQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(gQueue, ^{
	////丟到這裡來
});

如果你喜歡的話可以做一條queue

dispatch_queue_t myQueue = dispatch_queue_create("com.mycompany.myqueue", 0);

不過一般來說用default的都ok吧

MGTwitterEngine on iOS

對於我這種根基不太好的人來說要setup這個簡直是超麻煩耶

一直都是在github下載然後drag進去project就好了,但這個需要YAJLOAuthConsumerTouchJSON

然後參考了後終於弄好了,姑且紀錄一下希望幫到人

首先,OAuthConsumerTouchJSON都不難入手,跟平常一樣對待就ok了,問題是YAJL

這個直接上來說不能在iOS上跑耶

所以應該用這個:https://github.com/gabriel/yajl-objc

跟著instructions一路set好就沒問題了,

然後去MGTwitterEngineGlobalHeader.h改一下

#define YAJL_AVAILABLE 1
 
#define TOUCHJSON_AVAILABLE 1

很好很不錯!

Setting.app裡面的app settings要注意耶

我看到plist裡面有個叫DefaultValue就以為會幫我handle埋default的情況於是好興奮的去用了
結果原來是,未設定過的話 [[NSUserDefaults standardUserDefaults] whateverForKey:]會return nil
仲要係若無其事的感覺,因為我沒看doc的關係抵死XD

於是以下是解決方法:http://www.btjones.com/2010/05/nsuserdefaults-nil-setting-problem/

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
30
31
32
- (void)setupDefaults {
    //get the plist location from the settings bundle
    NSString *settingsPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Settings.bundle"];
    NSString *plistPath = [settingsPath stringByAppendingPathComponent:@"Root.plist"];
 
    //get the preference specifiers array which contains the settings
    NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    NSArray *preferencesArray = [settingsDictionary objectForKey:@"PreferenceSpecifiers"];
 
    //use the shared defaults object
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 
    //for each preference item, set its default if there is no value set
    for(NSDictionary *item in preferencesArray) {
 
        //get the item key, if there is no key then we can skip it
        NSString *key = [item objectForKey:@"Key"];
        if (key) {
 
            //check to see if the value and default value are set
            //if a default value exists and the value is not set, use the default
            id value = [defaults objectForKey:key];
            id defaultValue = [item objectForKey:@"DefaultValue"];
            if(defaultValue &amp;&amp; !value) {
                [defaults setObject:defaultValue forKey:key];
            }
        }
    }
 
    //write the changes to disk
    [defaults synchronize];
}

適當的在application:didFinishLaunchingWithOptions: 裡面觸發就很好了

iphone os 3 和iOS4的 CLLocation getDistanceFrom: 和 distanceFromLocation:

終於遇到第一次轉版本上的問題,其實內容都沒什麼大不了,只是method名轉了

google到了的各種解決方法中最喜歡這個:

http://0xced.blogspot.com/2010/09/cllocation-getdistancefrom-vs.html

1
2
3
4
5
6
7
main.m
 
#import <objc/runtime.h>
Method getDistanceFrom = class_getInstanceMethod([CLLocation class], @selector(getDistanceFrom:));
class_addMethod([CLLocation class], @selector(distanceFromLocation:),
method_getImplementation(getDistanceFrom),
method_getTypeEncoding(getDistanceFrom));

好帥