發表文章

目前顯示的是有「cocoa」標籤的文章

把我們的iPhone變成網頁伺服器!

圖片
來到新公司已經兩個月了,終於可以感覺到對工作有點上手,終於可以來分享最近的技術心得了:)這兩個月真的把我操成一個ruby人了,看了好多好多ruby的gem,rack, sinatra, redis-objects, 寄iOS notifications的gem, 等等好多,也寫了好多ruby code。 雖然這些都很值得一說,但是對我來說更有趣的是我最近的專案"your iPhone as a web server"。原始碼在 https://github.com/fumin/rubymotion-zeromq 與 https://github.com/fumin/world 。 這個軟體簡單來說就是讓使用者可以“只透過瀏覽器”就可以與自己的iPhone交流。舉凡瀏覽iPhone裡的照片,閱讀iPhone裡的筆記,下載檔案等等,以後不再需要經由拖曳到dropbox,上傳到免空,或是寄給自己沒有標題的郵件來完成了! 整個系統的架構大致為使用者經由瀏覽器連接到web server,web server代理連到broker server,broker server再與與之有socket連接的iPhone做request,回來的response再循原路回到瀏覽器。 有趣的是整個系統都是用ruby寫的喔,連iPhone程式本身!因為有了rubymotion:)其他使用的核心技術還包含了zeromq,web server則是使用sinatra搭配unicorn,並且host在hiroku上。 http://www.rubymotion.com/ http://www.zeromq.org/ http://www.sinatrarb.com/

單點讓UIView同時縮放與旋轉

UITouch *touch1 = [sortedTouches objectAtIndex:0]; //觸控移動的點         CGPoint beginPoint1 = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch1);         CGPoint currentPoint1 = [touch1 locationInView:self.superview];         double layerX = self.center.x;//指定UIView的中心點         double layerY = self.center.y;//指定UIView的中心點         double x1 = beginPoint1.x - layerX;         double y1 = beginPoint1.y - layerY;         double x2 = layerX - layerX;         double y2 = layerY - layerY;         double x3 = currentPoint1.x - layerX;         double y3 = currentPoint1.y - layerY;         double x4 = layerX - layerX;         double y4 = layerY - layerY;                  do...

Objective-C的數學運算函式

在寫程式的時候,常常需要用到一些數學運算,這邊整理一下常用的。 pow(x,y) : x 的 y 次方 sqrt(x) : x的平方根 floor(x) : 小於 x 的最大整數 ceil(x) : 大於 x 的最小整數 exp(x) : e 的 x 次方 log(x) : 以 e 為底 x 的對數值 log10(x) : 以10為底 x 的對數值 abs(x) : 整數 x 的絕對值 fabs(x) : 浮點數 x 的絕對值 srand(x), rand() : 產生亂數

如何讓app只能在iphone 4以上運行的方法

設定必要的相容性,一定要有陀螺儀,因這個只有iphone 4以上才有的晶片。 <key> UIRequiredDeviceCapabilities </key> <array>     <string> gyroscope </string> </array> 以下是其他網友被退件後,apple回覆的方法 : Yes, it's possible to make an App only for iPhone 4. However in this case you MUST make use of the UIRequiredDeviceCapabilities to limit the App to some iPhone4-only feature like camera-flash. (My iPhone4 App got rejected because I did not limit it to certain features. I just had added to the description: "Runs only on iPhone 4". The rejection letter told me that I must use the UIRequiredDeviceCapabilities if I want to limit the App to a certain hardware. Resubmitted the App with UIRequiredDeviceCapabilities set and the App got approved right away)

ARMv6 與 ARMv7

現在研究如何讓app只限定在iphone 4上運行,剛好找到下列的文章。 What is ARMv7? It's a new CPU architecture of iPhone. Make some apps run faster. What does ARMv7 to do with me? Here is a post by most_Unique found on iPhoneCake. Most apps today use FAT binary which cracked on ARMv7 device will not work on ARMv6 device. ARMv7 = iPhone 3GS/4, iPod 3G, iPad ARMv6 = iPhone 2G/3G, iPod 1G/2G Most crackers, like me, have 3GS. This means when cracking any app it will not work on ARMv6 devices. We have tried to fix it but haven't yet been able to. Now to my understanding apps cracked with ARMv6 device will work on any device. But since most of us have already upgraded some of you with older devices are just stuck. Sorry. So if an app crashes and you have one of these old devices it is likely the app has FAT binary and that is the problem.

讓你的iOS app可以與你的 Rails server溝通

圖片
今天在fb的社團看到網友分享這個類庫,真的實在太方便了~~ NSRails  is a light-weight Objective-C framework (iOS or OS X) for simple but powerful communication with your Rails server.  The comments above were posted using the demo iOS app bundled with the source.  Download it  and  get started! http://nsrails.com/

NSTableView SelectedRow

圖片
原本一直以為NSTableView跟UITableView的作法會全部一樣,就一直試著要做選取某一列 但是就一直沒有辦法正確的把值選到,今天晚上光找這個解法就找了很久,後來終於有試 出來了,原本這個選取的動作跟iphone/ipad比起來簡易了許多,難怪這個資料不怎麼好找。 一開始我們要先把TableView建立一個IBoutlet到h檔中,這樣我們就可以去對tableview操作。 接下來就如下圖,我們把tableview建立連結到File'sOwner,我們事先已經建了一個方法叫做 - (IBAction)tableViewSelected:(id)sender; 把這兩個接口連結後,我們就可以對tableview點選去做一些事情了。 以下是使用方法的參考 - (IBAction)tableViewSelected:(id)sender {       NSLog(@"the user clicked row %ld",[tableViewMenu selectedRow ]); // selectedRow 這個方法就是指選到tableView的某一row了。 } // end tableViewSelected

CoreData 筆記 (2)

當開發Mac App並使用Coredata的話,要對某一筆資料做更新,可以使用下列的方法。 事先在xib檔中把arraycontroller建立一個iboutlet的連線到h檔中,這時候我們就可以直接 使用arraycontroller中的資料了。 - (IBAction)RestLearnRecord:(id)sender {// 重置資料          //將arraycontroller的陣列取出     NSMutableArray *ctarray = [CategoryArray arrangedObjects];     //選取某一個索引上的物件,並給於對應的型態  Category *inData = [ctarray objectAtIndex:SelectIndexs];     //直接對此物件做操作     [inData setRemember:[NSNumber numberWithInt:100]];     [inData setTotally:[NSNumber numberWithInt:900]];         NSError *error = nil;       //修改完資料後,要執行存檔的動作,這時候畫面上的資料就會被更新了     if (![[self managedObjectContext] save:&error]) { //此行一定要執行         NSLog(@"Failed to save - error: %@", [error localizedDescription]);     }else {         [tableViewMenu reloadData];     }    ...

Core Data 學習筆記

圖片
做到現終於了解Coredata是怎樣的操作了,之前一直以為會跟ios上的操作不一樣,但是後來 看完了Apple官方文件,發現到Apple其實真得很不錯,從Mac到ios上的用法都大致相同,很 多地方其實可以用ios的一些原理,反推回去,不過還是有些許的部份是不同的。 在Mac上的Coredata,是標榜著" write less code ",這可是這Coredata當時的發展目標,在Mac 上確實真的看到了,他真的要寫的code很少,幾乎是不太要寫到什麼代碼。很快就可以完成 一個資料庫的加入、移除、修改,這三大基本動作,就連關聯式資料庫的一些維護動作,他 也全都在背後全做好了。 到目前的階段大致上需要的功能都有了,接下來可以專心的想如何開發出Mac App了。 以下是基本操作的代碼 在這個測試檔中,使用的Entity Class就叫做"Entity",使用的Model也是叫做Entity,裡面只有 一個欄位就叫做"name",標記為紅色的部份就是需要自已替換上的部份。 - (IBAction)fetchRequest:(id)sender {//取出所有資料          NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];     NSEntityDescription *entity = [NSEntityDescription entityForName:@" Entity " inManagedObjectContext:[self managedObjectContext]];     [fetchRequest setEntity:entity];          NSError *error = nil;     NSArray *fetchedObjects = [ [self managedObjectContext] executeFetchRequest:fetchRequest error:...